首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法设置自定义针图像在MapView,Swift 3

无法设置自定义针图像在MapView,Swift 3
EN

Stack Overflow用户
提问于 2017-08-25 21:54:07
回答 1查看 1.8K关注 0票数 0

迷你地图代码:

代码语言:javascript
复制
import UIKit
import MapKit

@IBOutlet weak var ProfileMapView: MKMapView!

func mapView(_ ProfileMapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

    if annotation.isMember(of: MKUserLocation.self) {
        return nil
    }

    let reuseId = "ProfilePinView"

    var pinView = ProfileMapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
    if pinView == nil {
        pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
    }
    pinView!.canShowCallout = true
    pinView!.image = UIImage(named: "CustomPinImage")
    return pinView
}

override func viewDidLoad() {
    super.viewDidLoad()
    let LightHouseLocation = CoordinatesTemplate
    // Drop a pin
    let lightHouse = MKPointAnnotation()
    lightHouse.coordinate = LightHouseLocation
    lightHouse.title = barNameTemplate
    lightHouse.subtitle = streetNameTemplate
    ProfileMapView.addAnnotation(lightHouse)
}

为什么不动呢?为什么我不能让我的自定义图像作为引脚图像工作?它为我在另一个视图控制器上工作。先谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-25 22:29:01

您的问题是,您缺少添加您的UIViewController作为您的MKMapView的委托,所以您需要在您的viewDidLoad中添加这一行,正如我在我的评论中所说的。

代码语言:javascript
复制
self.ProfileMapView.delegate = self

我还建议您使用扩展模式来提高代码的可读性。

代码语言:javascript
复制
extension YourViewController : MKMapViewDelegate{

    func mapView(_ ProfileMapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {

        if annotation.isMember(of: MKUserLocation.self) {
            return nil
        }

        let reuseId = "ProfilePinView"

        var pinView = ProfileMapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
        if pinView == nil {
            pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)}
        pinView!.canShowCallout = true
        pinView!.image = UIImage(named: "CustomPinImage")

        return pinView

    }
}
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45889861

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档