【问题标题】:Swift - Add MKAnnotationView To MKMapViewSwift - 将 MKAnnotationView 添加到 MKMapView
【发布时间】:2014-08-19 11:36:29
【问题描述】:

我正在尝试将MKAnnotationView 添加到MKMapView,但我做不到...谁能帮帮我?

这是我的代码:

override func viewDidLoad() {
    super.viewDidLoad()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.startUpdatingLocation()
    var latitude:CLLocationDegrees = locationManager.location.coordinate.latitude
    var longitude:CLLocationDegrees = locationManager.location.coordinate.longitude
    var homeLati: CLLocationDegrees = 40.01540192
    var homeLong: CLLocationDegrees = 20.87901079
    var latDelta:CLLocationDegrees = 0.01
    var longDelta:CLLocationDegrees = 0.01
    var theSpan:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, longDelta)
    var myHome:CLLocationCoordinate2D = CLLocationCoordinate2DMake(homeLati, homeLong)
    var myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var theRegion:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, theSpan)
    self.theMapView.setRegion(theRegion, animated: true)
    self.theMapView.mapType = MKMapType.Hybrid
    self.theMapView.showsUserLocation = true
    ///Red Pin
    var myHomePin = MKPointAnnotation()
    myHomePin.coordinate = myHome
    myHomePin.title = "Home"
    myHomePin.subtitle = "Bogdan's home"
    self.theMapView.addAnnotation(myHomePin)

    var anView:MKAnnotationView = MKAnnotationView()
    anView.annotation = myHomePin
    anView.image = UIImage(named:"xaxas")
    anView.canShowCallout = true
    anView.enabled = true
}

【问题讨论】:

  • 请参阅stackoverflow.com/questions/24523702/… 以获取使用 MKPinAnnotationView 的示例。您可以轻松地将其更改为普通的 MKAnnotationView 并设置图像。您不需要自定义注释类来满足您的目的(MKPointAnnotation 很好)。
  • 另请注意,您不应在调用 startUpdatingLocation 后立即尝试访问 locationManager.location。它可能还没有准备好。使用 didUpdateLocations 委托方法。您还需要调用 requestAlwaysAuthorization/requestWhenInUseAuthorization(请参阅stackoverflow.com/questions/24062509/…)。
  • @Anna 它可以工作,但我无法设置我尝试在 Images.xcassets 中添加的 pinView!.image = UIImage(contentsOfFile: "xaxas")pinView!.image = UIImage(named: "xaxas") 但未成功...
  • UIImage(named:"xaxas") 应该可以工作,并确保拼写完全正确正确,包括大写/小写。尝试将图像与其他项目文件而不是 Images.xcassets 一起放置。在使用自定义图像时,创建一个普通的 MKAnnotationView 而不是 MKPinAnnotationView 也很重要。

标签: mkmapview swift mkannotation mkannotationview


【解决方案1】:

您需要实现viewForAnnotation 委托方法并从那里返回MKAnnotationView

这是一个例子:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {
    if (annotation is MKUserLocation) {
        //if annotation is not an MKPointAnnotation (eg. MKUserLocation),
        //return nil so map draws default view for it (eg. blue dot)...
        return nil
    }

    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {
        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.image = UIImage(named:"xaxas")
        anView.canShowCallout = true
    }
    else {
        //we are re-using a view, update its annotation reference...
        anView.annotation = annotation
    }

    return anView
}

请记住,当您想使用自己的自定义图像时,您需要创建一个 plain MKAnnotationViewMKPinAnnotationView 只能用于标准引脚注释。

还请注意,您不应在调用startUpdatingLocation 后立即尝试访问locationManager.location。它可能还没有准备好。

使用didUpdateLocations 委托方法。

您还需要致电requestAlwaysAuthorization/requestWhenInUseAuthorization(见Location Services not working in iOS 8)。

【讨论】:

  • 从行中删除 ! 感叹号 - if !(annotation is MKPointAnnotation)。否则它将返回nil 用于自定义注释而不是MKPointAnnotation
  • @Kampai,我建议改为使用if (annotation is MKUserLocation),因此它只返回nil 用于地图视图的用户位置注释。
  • 是的,没错,否则MKPointAnnotation 除了用户位置也不可见。
  • 不,不是不可见。返回 nil 告诉地图视图返回注释的默认视图。对于 MKUserLocation,它是一个蓝点。对于其他任何事情,它都是一个红色别针。如果您返回一个没有图像集的非零 MKAnnotationView,它将是不可见的。
  • 添加了缺少的大括号(感谢@DaniA 指出这一点)。
【解决方案2】:

你要显示注解,然后你必须添加显示注解方法:

let annotation = MKPointAnnotation()
mapVew.showAnnotations([annotation], animated: true)

annotationMKPointAnnotation 的一个实例。

【讨论】:

    【解决方案3】:

    1- 您的地图视图应将自己委托给您的视图控制器 2-你应该实施

    func mapView(aMapView: MKMapView!, viewForAnnotation annotation: CustomMapPinAnnotation!) -> MKAnnotationView!
    

    从添加到地图的每个注释中调用此函数

    3- 子类化您的注释以在 viewForAnnotation 方法中识别它们

    4-在viewForAnnotation中,添加

    if annotation.isKindOfClass(CustomMapPinAnnotation)
    {
      // change the image here
    var pinView = aMapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? YourSubclassedAnnotation
      pinView!.image = UIImage(contentsOfFile: "xyz")
    }
    

    【讨论】:

    • 我收到错误“使用未声明的类型 CustomMapPinAnnotation”
    • @BogdanBogdanov 如果你想使用 CustomMapPinAnnotation,你必须创建你自己的类。
    • 无论是自定义的还是默认的,我都需要用动画放置图钉。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多