【问题标题】:Swift2 iOS9 - how to get custom image instead of default annotation red pin in my codeSwift2 iOS9 - 如何在我的代码中获取自定义图像而不是默认注释红色引脚
【发布时间】:2016-08-24 14:19:51
【问题描述】:

这是我的代码。我已经尝试了所有可用的解决方案,但我无法在我的代码中放置我想要的图像而不是默认的注释红色引脚。

import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet var map: MKMapView!

override func viewDidLoad() {
    super.viewDidLoad()

    var location = CLLocationCoordinate2DMake(18.956449, 72.810597)

    var span = MKCoordinateSpanMake(0.009, 0.009)
    var region = MKCoordinateRegion(center: location, span: span)

    map.setRegion(region, animated: true)

    var annotation = MKPointAnnotation()
    annotation.coordinate = location
    annotation.title = "Wilson College"
    annotation.subtitle = "Commerce College"

    map.addAnnotation(annotation)
    print("showing on map")

    mapView(map, viewForAnnotation: annotation)


}



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

    let identifier = "MyPin"
    print("inside viewForAnnotation")
    if annotation.isKindOfClass(MKUserLocation) {
        return nil
    }

    let detailButton: UIButton = UIButton(type: UIButtonType.DetailDisclosure)

    // Reuse the annotation if possible
    var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)

    if annotationView == nil
    {
        annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "pin")
        annotationView!.canShowCallout = true
        annotationView!.image = UIImage(named: "VRtest.png")
        annotationView!.rightCalloutAccessoryView = detailButton
        print("editing annotation")
    }
    else
    {
        annotationView!.annotation = annotation
    }

    return annotationView
}

感谢任何帮助。谢谢

【问题讨论】:

  • 永远不要自己打电话给viewForAnnotation。委托方法由框架调用。
  • 请指导,我是业余爱好者。我已经陈述了一些打印语句来理解代码的流程。使用当前代码,我在输出框中得到这些语句,“在地图上显示”、“在 viewForAnnotation 内部”、“编辑注释”,如果我删除了这一行 - mapView(map, viewForAnnotation: annotation),那么我得到了这个声明 - 仅“在地图上显示”。

标签: ios


【解决方案1】:

你想要的是自定义默认注解吗?如果是,你可以参考这篇文章How to create Custom MKAnnotationView and custom annotation title and subtitle

这是 Swift 2 中的示例

class MapViewController: UIViewController {


    @IBOutlet weak var mapView: MKMapView!

    let initialLocation = CLLocation(latitude: 34.074094, longitude: -118.396329)
    let regionRadius: CLLocationDistance = 1000

    func centerMapOnLocation(location: CLLocation) {
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate,
            regionRadius * 2.0, regionRadius * 2.0)
        self.mapView.setRegion(coordinateRegion, animated: true)
    }

    override func viewDidLoad() {
        self.mapView.delegate = self
        self.centerMapOnLocation(initialLocation)

        let location = CLLocationCoordinate2D(latitude: 34.074094, longitude: -118.396329)
        let annotation = CustomAnnotation(coordinate: location, imageName: "pinIcon", title: "Test", subtitle: "Detail", enableInfoButton: false)

        self.mapView.addAnnotation(annotation);
    }
}

extension MapViewController: MKMapViewDelegate {
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {

        if (annotation is MKUserLocation) {
            return nil
        }

        let pinIdentifier = "pinIdentifier"
        var view: MKAnnotationView
        if (annotation.isKindOfClass(CustomAnnotation)) {
            if let dequeuedView = mapView.dequeueReusableAnnotationViewWithIdentifier(pinIdentifier)
                as MKAnnotationView? {
                    dequeuedView.annotation = annotation
                    view = dequeuedView
            } else {
                view = MKAnnotationView(annotation: annotation, reuseIdentifier: pinIdentifier)
                view.canShowCallout = true

                //Set custome image
                let customAnnotation = annotation as! CustomAnnotation
                view.image = UIImage(named:customAnnotation.imageName!)
            }

            return view
        }

        return nil
    }

}

class CustomAnnotation : NSObject, MKAnnotation {

    var coordinate: CLLocationCoordinate2D
    var imageName: String?
    var title: String?
    var subtitle: String?
    var enableInfoButton : Bool

    init(coordinate: CLLocationCoordinate2D, imageName: String, title: String, subtitle: String, enableInfoButton : Bool) {
        self.coordinate = coordinate
        self.imageName = imageName
        self.title = title
        self.subtitle = subtitle
        self.enableInfoButton = enableInfoButton;
    }
}

这是我在屏幕上显示的自定义图像

【讨论】:

  • 感谢您的帮助。答案给出了objectiveC的代码,是否有swift2的代码。如果可能,请分享 swift2 代码的链接。如果有一个工作示例项目,那将有很大帮助。
  • 成功了。 HDT,你太棒了。谢谢你的帮助。
  • 这里还有一件事,我们可以给注解做drop效果吗?我们在这段代码中编辑了什么?
  • 一个观察:与swift代码的默认红色引脚相比,我使用此代码放置的图像不是精确的点(点),只有当我放大时,它才会显示精确地点。在那种情况下可以做任何事情.....另外,还有一个补充,我们可以对注释进行下降效果吗?我们在这段代码中编辑了什么?
猜你喜欢
  • 2012-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多