在Swift(MapKit)中使用自定义标记图片

5

我使用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
}

一个选项是您可以根据需要设置图像大小,然后显示它。 - Dharmesh Kheni
2个回答

5

尝试使用

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

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
          }
     }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接