【问题标题】:Change pin image on MKMapView in Swift在 Swift 中更改 MKMapView 上的图钉图像
【发布时间】:2015-03-18 07:44:20
【问题描述】:

我正在尝试在 Swift 中更改 MKMapView 上的图钉图像,但不幸的是它不起作用。知道我做错了什么吗?我在这里看到了一些示例,但没有奏效。

import UIKit
import MapKit

class AlarmMapViewController: UIViewController {
    @IBOutlet weak var map: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        showAlarms()
        map.showsUserLocation = true
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }

    func showAlarms(){

        map.region.center.latitude = 49
        map.region.center.longitude = 12
        map.region.span.latitudeDelta = 1
        map.region.span.longitudeDelta = 1

        for alarm in Alarms.sharedInstance.alarms {

            let location = CLLocationCoordinate2D(
                latitude: Double(alarm.latitude),
                longitude: Double(alarm.longtitude)
            )

            let annotation = MKPointAnnotation()
            annotation.setCoordinate(location)
            annotation.title = alarm.name
            annotation.subtitle = alarm.description
            mapView(map, viewForAnnotation: annotation).annotation = annotation
            map.addAnnotation(annotation)
        }
    }

    @IBAction func zoomIn(sender: AnyObject) {
    }

    @IBAction func changeMapType(sender: AnyObject) {
    }

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

        if annotation is MKUserLocation {
            //return nil so map view draws "blue dot" for standard user location
            return nil
        }

        let reuseId = "pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId) as? MKPinAnnotationView
        if pinView == nil {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.image = UIImage(named:"GreenDot")!

        }
        else {
            pinView!.annotation = annotation
        }

        return pinView
    }
}

GreenDot 图片可用于其他地方。

【问题讨论】:

    标签: ios swift


    【解决方案1】:

    别忘了设置:

    map.delegate = self
    

    并确保您的 UIViewController 实现 MKMapViewDelegate 协议。
    如果您忘记执行此操作,则不会为您的地图调用 mapView:viewForAnnotation: 的实现。

    此外,pinView!.animatesDrop = true 似乎破坏了自定义图像。您必须将其设置为 false,或使用 MKAnnotationView(它没有 animatesDrop 属性)。

    如果您想实现自定义放置动画,请参阅this related question

    【讨论】:

    • 好的,我这样做了,现在我可以使用 pinView!.pinColor = .Green 更改 pinColor,但是当我尝试使用 pinView!.image = UIImage(named:"GrayDot") 更改图像时!这仍然不起作用..
    • 你试过用MKAnnotationView代替MKPinAnnotationView吗?
    • 对,我刚刚用 MKAnnotationView 做到了
    • 太棒了!我已经用更多细节更新了我的答案。
    【解决方案2】:

    MKPinAnnotationView 总是使用 pin 图像,不能永远使用。您必须改用 MKAnnotationView。 请注意,因为属性 animatesDrop 它不是有效的 MKAnnotationView 的属性,所以请重新划线。

    【讨论】:

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