Ios Mapview Swift的自定义注释

Ios Mapview Swift的自定义注释,ios,swift,mkannotation,mkannotationview,Ios,Swift,Mkannotation,Mkannotationview,我看了这篇文章,我仍然没有收到一个自定义pin 自定义批注-->这包括设置我的图像 import UIKit import MapKit class CustomPointAnnotation: MKPointAnnotation { var pinCustomImageName: UIImage! } 视图控制器: 我想返回当前位置,直到选择一个按钮放置pin func mapView(_ mapView: MKMapView, viewFor annotation: M

我看了这篇文章,我仍然没有收到一个自定义pin

自定义批注-->这包括设置我的图像

 import UIKit
 import MapKit

 class CustomPointAnnotation: MKPointAnnotation {
     var pinCustomImageName: UIImage!
 }
视图控制器:

我想返回当前位置,直到选择一个按钮放置pin

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    //current Location
    if !(annotation is CustomPointAnnotation) {
        return nil
    }
    let reuseIdentifier = "pin"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier)
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        annotationView!.canShowCallout = true
        
    } else {
        annotationView!.annotation = annotation
    }
    if let annotationView = annotationView {
        annotationView.image = UIImage(named: "Skyscraper")
        annotationView.canShowCallout = true
    }
   
    return annotationView
}

func addPin() {
    pointAnnotation = CustomPointAnnotation()
    pointAnnotation.pinCustomImageName = UIImage(named: "Skyscraper")
    pointAnnotation.coordinate = currentLocation.coordinate
    pointAnnotation.title = "First Building"
    pointAnnotation.subtitle = "Latitude: \(currentLocation.coordinate.latitude), \ 
     (currentLocation.coordinate.longitude)"
    mapView.addAnnotation(pointAnnotation)
}

代码没有什么严重的问题。但可能有两件事会引起问题,包括:

  • 您是否为地图视图设置了代理(IB或编程方式)?否则,将永远不会调用您的
    地图视图(uuquo:viewFor:)
    。添加断点或调试
    print
    语句进行确认

  • 您是否确认
    UIImage(名为:“摩天大楼”)
    正在成功检索图像?确保这不会返回
    nil


  • 注意,如果只有iOS 11及更高版本,您可以稍微简化此代码。自iOS 11以来,在这样的简单场景中,我们不再需要
    mapView(uuxAE:viewFor:)
    。我建议将annotation view配置代码放在annotation view子类中(它所属的位置),并避免将视图控制器与
    viewFor
    实现混在一起

    因此,当您确实了解当前问题时,建议采用以下流程:

  • 为注释和注释视图定义类:

    class CustomAnnotation: MKPointAnnotation {
        var pinCustomImage: UIImage!
    }
    

  • viewDidLoad
    中注册此注释视图类:

    mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
    
  • 删除
    mapView(\uu:viewFor:)
    implementation

  • 现在,当您将
    CustomAnnotation
    添加到地图的注释列表时,它将正确渲染


    但我建议先解决你目前的问题。在这些更基本的问题得到解决之前,改进您的实现是没有意义的。

    代码没有严重错误。但可能有两件事会引起问题,包括:

  • 您是否为地图视图设置了代理(IB或编程方式)?否则,将永远不会调用您的
    地图视图(uuquo:viewFor:)
    。添加断点或调试
    print
    语句进行确认

  • 您是否确认
    UIImage(名为:“摩天大楼”)
    正在成功检索图像?确保这不会返回
    nil


  • 注意,如果只有iOS 11及更高版本,您可以稍微简化此代码。自iOS 11以来,在这样的简单场景中,我们不再需要
    mapView(uuxAE:viewFor:)
    。我建议将annotation view配置代码放在annotation view子类中(它所属的位置),并避免将视图控制器与
    viewFor
    实现混在一起

    因此,当您确实了解当前问题时,建议采用以下流程:

  • 为注释和注释视图定义类:

    class CustomAnnotation: MKPointAnnotation {
        var pinCustomImage: UIImage!
    }
    

  • viewDidLoad
    中注册此注释视图类:

    mapView.register(CustomAnnotationView.self, forAnnotationViewWithReuseIdentifier: MKMapViewDefaultAnnotationViewReuseIdentifier)
    
  • 删除
    mapView(\uu:viewFor:)
    implementation

  • 现在,当您将
    CustomAnnotation
    添加到地图的注释列表时,它将正确渲染


    但我建议先解决你目前的问题。在这些更基本的问题得到解决之前,优化您的实现是没有意义的。

    这非常有效,谢谢。您知道如何将自定义动画添加到管脚,例如在添加时它会上下反弹吗?以及如何使用user.defaults保存此pin供将来使用?如果要使用反弹重新制作动画,您必须自己完成。例如,您可以访问
    自定义注释视图
    。或者你也可以在你的
    MKMapViewDelegate
    中(尽管从概念上讲,这似乎是一个错误的地方,IMHO)。将pin保存在持久存储中,保存到
    Codable
    中,然后你可以将注释数组编码成JSON或plist,然后你可以对持久存储进行写入和读取(例如,应用程序支持目录)。(UserDefaults不是应用程序存储的正确位置。)但如果你这样做,你可能只想在注释中存储图像名称,而不是整个图像,正如我在该示例中所示。这非常有效。谢谢你。你知道如何将自定义动画添加到pin中,例如添加后它会上下反弹吗?以及如何保存此pin以供将来使用。默认值?重新设置动画n使用弹跳时,您必须自己进行。例如,您可以转到
    CustomAnnotationView
    。或者您也可以在
    MKMapViewDelegate
    中进行弹跳(尽管从概念上讲,这似乎是一个错误的地方,IMHO).将pin重新保存在永久性存储中,保存到
    Codable
    ,然后您可以将注释数组编码为JSON或plist,然后您可以将其写入永久性存储并从中读取(例如,应用程序支持目录)。(UserDefaults不是应用程序存储的正确位置。)但如果这样做,您可能只希望在注释中存储图像名称,而不是整个图像,如我在该示例中所示。