在iOS7,iOS8,iOS9地图界面显示自定义注释,但我在iOS10地图注释中运行app未显示。
我不明白iOS10与iOS7,8,9中差异的地图注释
答案 0 :(得分:1)
我使用MKpinAnnotationView进行了类似的问题。 无论如何,对于iOS10 / swift,此代码适用于图像:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
let Identifier = "CAR"
var annotationView: MKAnnotationView?
if let dequeued = mapView.dequeueReusableAnnotationView(withIdentifier: Identifier) {
annotationView = dequeued
}
else {
let av = MKAnnotationView(annotation: annotation, reuseIdentifier: Identifier)
annotationView = av
}
annotationView!.annotation = annotation
annotationView!.image = UIImage(named: "Audia4")
annotationView!.canShowCallout = true
return annotationView
}
如果您想要自定义图像:(假设您有遵守MKannotation协议的Car classe):
if let car = annotation as? Car{
let img = car.image
annotationView.image = img
}else{
annotationView.image = nil // clear it.
}
“Car”是:
class Car: NSObject, MKAnnotation {
var lat: CLLocationDegrees?
var long: CLLocationDegrees?
var T: String?
var subT: String?
var imgName: String?
...
init(lat: CLLocationDegrees, long: CLLocationDegrees,
title T: String, subtitle ST: String, imgName: String ) {
self.lat = lat
self.long = long
self.T = T
self.subT = ST
self.imgName = imgName
}
var coordinate: CLLocationCoordinate2D {
let coords = CLLocationCoordinate2D(latitude: lat!, longitude: long!)
return coords
}
var title : String? {
return T
}
var subtitle: String? {
return subT
}
var image: UIImage? {
let img = UIImage(named: self.imgName!)
return img
}
答案 1 :(得分:1)
我自己遇到了这个问题。我的MKAnnotationView
的自定义子类依赖于在我的覆盖initWithFrame:
和iOS 10中完成的一些初始化,通过调用initWithAnnotation:reuseIdentifier:
初始化annotationView最终将调用到initWithFrame:。但是,从iOS 10开始,永远不会调用initWithFrame:
。结果是我的自定义注释的框架为(0, 0, 0, 0)
并且没有显示。添加
[self setFrame:CGRectMake(0, 0, someWidth, someHeight];
在initWithAnnotation:reuseIdentifier:
内部使注释再次出现在iOS10下。
答案 2 :(得分:0)
我遇到了同样的问题 - 自定义注释图像没有在iOS 10上显示,但之前在版本上工作过。
对我来说问题是我使用了MKPinAnnotationView
。
切换自:
pin = [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinid];
pin.image = image;
到
pin = [[MKAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pinid];
pin.image = image;
自定义图像在iOS 10上再次显示。
答案 3 :(得分:0)
正如其他人所说,MKPinAnnotationView似乎不适用于iOS10。您需要将MKPinAnnotationViews更改为MKAnnotationViews,我只需切换它就没有问题。 示例Swift代码:
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
var view: MKAnnotationView
view = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
view.canShowCallout = true
view.frame = mapView.frame
view.image = UIImage(named: "<nameHere>")
return view
}