【发布时间】:2016-08-24 14:19:51
【问题描述】:
这是我的代码。我已经尝试了所有可用的解决方案,但我无法在我的代码中放置我想要的图像而不是默认的注释红色引脚。
import MapKit
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var map: MKMapView!
override func viewDidLoad() {
super.viewDidLoad()
var location = CLLocationCoordinate2DMake(18.956449, 72.810597)
var span = MKCoordinateSpanMake(0.009, 0.009)
var region = MKCoordinateRegion(center: location, span: span)
map.setRegion(region, animated: true)
var annotation = MKPointAnnotation()
annotation.coordinate = location
annotation.title = "Wilson College"
annotation.subtitle = "Commerce College"
map.addAnnotation(annotation)
print("showing on map")
mapView(map, viewForAnnotation: annotation)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
let identifier = "MyPin"
print("inside viewForAnnotation")
if annotation.isKindOfClass(MKUserLocation) {
return nil
}
let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)
// Reuse the annotation if possible
var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
if annotationView == nil
{
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
annotationView!.canShowCallout = true
annotationView!.image = UIImage(named: "VRtest.png")
annotationView!.rightCalloutAccessoryView = detailButton
print("editing annotation")
}
else
{
annotationView!.annotation = annotation
}
return annotationView
}
感谢任何帮助。谢谢
【问题讨论】:
-
永远不要自己打电话给
viewForAnnotation。委托方法由框架调用。 -
请指导,我是业余爱好者。我已经陈述了一些打印语句来理解代码的流程。使用当前代码,我在输出框中得到这些语句,“在地图上显示”、“在 viewForAnnotation 内部”、“编辑注释”,如果我删除了这一行 - mapView(map, viewForAnnotation: annotation),那么我得到了这个声明 - 仅“在地图上显示”。
标签: ios