【问题标题】:Custom Annotation Using MKPointAnnotation使用 MKPointAnnotation 自定义注释
【发布时间】:2017-09-28 00:18:26
【问题描述】:

这就是我想要做的:

对将填充地图的所有位置使用单个自定义注释。我将自定义图像保存在资产中,但无法使其工作。

我的代码如下:

ViewDidLoad()

    if let locationDict = snap.value as? Dictionary<String, AnyObject> {

        let lat = locationDict["LATITUDE"] as! CLLocationDegrees
        let long = locationDict["LONGITUDE"] as! CLLocationDegrees
        let title = locationDict["NAME"] as! String
        let center = CLLocationCoordinate2D(latitude: lat, longitude: long)
        _ = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.10, longitudeDelta: 0.10))

        let annotation = MKPointAnnotation()
        annotation.coordinate = CLLocationCoordinate2DMake(lat, long)
        annotation.title = title.capitalized // if you need title, add this line
        self.mapView.addAnnotation(annotation)
    }

对将填充地图的所有位置使用单个自定义注释。我在资产中保存了自定义图像,但无法使其工作(viewDidLoad 中的详细信息)

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

        var annotationView: MKAnnotationView?


        if annotation.isKind(of: MKUserLocation.self) {
            annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "User")
            annotationView?.image = UIImage(named: "icon")
        }



        return annotationView
    }

【问题讨论】:

标签: ios swift mkannotationview mkpointannotation


【解决方案1】:

你应该实现委托方法:

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

    if annotation.isKind(of: MKUserLocation.self) {
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "User")
        annotationView.image = UIImage(named: "icon")
        return annotationView
    }

    let reuseId = "Image"
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId)
    if annotationView == nil {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        annotationView?.canShowCallout = true
        annotationView?.image = UIImage(named: "<<image name>>")
    }
    else {
        annotationView?.annotation = annotation
    }

    return annotationView
}

【讨论】:

  • 如何使用我现在拥有的代码?显然,复制它是行不通的,因为我已经有一个自定义注释。我不明白如何合并这两个代码来工作。
  • @ATrueNovice 我可以看看你的“自定义注释”代码吗?
  • 我刚刚将它添加到原始问题中。我有一个针对用户的特定注释和另一个针对所有位置的注释。我的 ViewDidLoad 中有 Location Annotation,View For Annotation 中有用户注释。我尝试移动位置注释,但是由于我在视图中解开firebase数据的字典确实加载了,它在其他任何地方都不起作用。
【解决方案2】:

你应该像这样实现委托方法。

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

        if annotation is MKUserLocation {
            return nil
        }

        var pinView : MKAnnotationView? = nil
        let identifer = "pin"

        pinView = mapView .dequeueReusableAnnotationView(withIdentifier: identifer)
        if pinView == nil {
            pinView = MKAnnotationView.init(annotation: annotation, reuseIdentifier: identifer)
        }

        pinView?.canShowCallout = true
        pinView?.calloutOffset = CGPoint(x: -5, y: 5)
        pinView?.rightCalloutAccessoryView = UIButton.init(type: .detailDisclosure) as UIView

        let type = (annotation as! CustomAnnotation).type

        if type == -1 {
            pinView?.image = UIImage(named: "Img_gray_pin")?.resized(toWidth: 20)
        } else if type == 0 {
            pinView?.image = UIImage(named: "Img_gray_pin")?.resized(toWidth: 20)
        } else if type == 1{
            pinView?.image = UIImage(named: "Img_blue_pin")?.resized(toWidth: 20)
        } else if type == 2 {
            pinView?.image = UIImage(named: "Img_orange_pin")?.resized(toWidth: 25)
        } else if type == 3 {
            pinView?.image = UIImage(named: "Img_red_pin")?.resized(toWidth: 30)
        } else {
            pinView?.image = UIImage(named: "Img_gray_pin")?.resized(toWidth: 20)
        }

        return pinView
    }

然后创建CustomAnnotation Class

import MapKit
import UIKit

class CustomAnnotation: NSObject, MKAnnotation {

    let title: String?
    let subtitle: String?
    let coordinate: CLLocationCoordinate2D
    let tag     : Int
    let type    : Int

    init(title: String, address: String, coordinate: CLLocationCoordinate2D, tag : Int, type : Int) {
        self.title = title
        self.subtitle = address
        self.coordinate = coordinate
        self.tag = tag
        self.type = type

        super.init()
    }
}

【讨论】:

    猜你喜欢
    • 2015-09-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多