【问题标题】:Map Annotations not show in ios10ios 10 中未显示地图注释
【发布时间】:2016-07-29 15:52:21
【问题描述】:

在 iOS7、iOS8、iOS9 地图屏幕显示自定义注释,但我在 iOS10 中运行应用程序地图注释未显示。

我不明白 iOS10 与 iOS7、8、9 中 Map Annotations 的区别

【问题讨论】:

    标签: ios10


    【解决方案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 类):

        if let car = annotation as? Car{
            let img = car.image
            annotationView.image = img
        }else{
            annotationView.image = nil // clear it.
        }
    

    “汽车”在哪里:

    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
     }
    

    【讨论】:

      【解决方案2】:

      我自己也遇到过这个。我的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下又出现了。

      【讨论】:

        【解决方案3】:

        我遇到了同样的问题 - 自定义注释图像没有显示在 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 上再次显示。

        【讨论】:

          【解决方案4】:

          正如其他人所说,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
              }
          

          【讨论】:

            猜你喜欢
            • 2023-03-23
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多