这是我的代码,我想添加一个自定义引脚(.png文件),而不是红色引脚。我试图使用MKPinAnnotationView和MKAnnotationView,但我无法添加坐标,字幕和标题。我是iOS开发新手。
override func viewDidLoad() {
super.viewDidLoad()
// Handle the text field’s user input through delegate callbacks.
commentTextField.delegate = self
coreLocationManager.delegate = self
//desired accuracy is the best accuracy, very accurate data for the location
coreLocationManager.desiredAccuracy = kCLLocationAccuracyBest
//request authorization from the user when user using my app
coreLocationManager.requestWhenInUseAuthorization()
coreLocationManager.startUpdatingLocation()
dbRef = FIRDatabase.database().reference()
struct Location {
let title: String
let latitude: Double
let longitude: Double
let subtitle: String
}
// Locations array
let locations = [
Location(title: "Dio Con Dio", latitude: 40.590130, longitude: 23.036610,subtitle: "cafe"),
Location(title: "Paradosiako - Panorama", latitude: 40.590102, longitude: 23.036180,subtitle: "cafe"),
Location(title: "Veranda", latitude: 40.607740, longitude: 23.103044,subtitle: "cafe")
]
for location in locations {
let annotation = MKPointAnnotation()
annotation.title = location.title
annotation.coordinate = CLLocationCoordinate2D(latitude: location.latitude, longitude: location.longitude)
annotation.subtitle = location.subtitle
map.addAnnotation(annotation)
}
}
6条答案
按热度按时间vfhzx4xs1#
您需要指定您的视图控制器作为Map视图的委托(在IB中或在
viewDidLoad
中以编程方式),然后(a)指定您符合MKMapViewDelegate
协议;以及(B)实现mapView(_:viewFor:)
:有关详细信息,请参阅《位置和Map编程指南:从委托对象创建注解视图。代码片段是在Objective-C中,但它描述了基本过程。
7tofc5zh2#
最后,我这样做了,靠我自己!
ViewController.swift
地点.swift
e0uiprwp3#
SWIFT 5
sh7euo9m4#
qc6wkl3g5#
我是这样补充的:
axzmvihb6#
使用此方法:
对我来说很好。(我的环境:Xcode 9.4.1和iOS 11.4.1)
示例: