swift上的自定义标记图像(MapKit)

时间:2015-05-15 14:37:39

标签: ios swift mapkit

我在MapKit上使用自定义标记

如何在自定义标记上更改图像宽度和高度?

我的代码:

func mapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! {

    if (annotation is MKUserLocation) {
        return nil
    }

    let reuseId = "test"

    var anView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if anView == nil {

        anView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        anView.image = UIImage(named:"x2.png")
        anView.canShowCallout = true
    }
    else {
        anView.annotation = annotation
    }
    return anView
}

2 个答案:

答案 0 :(得分:4)

尝试使用

anView.frame.size = CGSize(width: 30.0, height: 30.0)

答案 1 :(得分:0)

var currentLocationAppleMarker : MKAnnotationView?

self.currentLocationAppleMarker?.image = //Image for Current Location Pin

MapKit ViewForAnnotation委托应该像

 func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    
let pin = mapView.view(for: annotation) ?? MKAnnotationView(annotation: annotation, reuseIdentifier: nil)
    if annotation is MKUserLocation {
           pin.image = //Image for Current Location Pin
           return pin
         } else {
             pin.image = //Image for Other Pin
             return pin
          }
     }