简体   繁体   English

Swift2 iOS9-如何在我的代码中获取自定义图像而不是默认注释红色图钉

[英]Swift2 iOS9 - how to get custom image instead of default annotation red pin in my code

Here is my code. 这是我的代码。 I have tried all the solutions available but I am not able to put image I wish to instead of the default annotation red pin in my code. 我已经尝试了所有可用的解决方案,但无法在代码中放入希望的图像,而不是默认的注释红色图钉。

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
}

在此处输入图片说明

Any help is appreciated. 任何帮助表示赞赏。 thank you 谢谢

What do you want is to customize the default annotation? 您要自定义默认注释吗? if YES, you can refer this post How to create Custom MKAnnotationView and custom annotation title and subtitle 如果是,则可以参考此文章如何创建自定义MKAnnotationView和自定义注释标题和副标题

Here is the example in Swift 2 这是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;
    }
}

This is my custom image displaying on the screen 这是我在屏幕上显示的自定义图像

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在iOS9的swift2中获取航向数据(用于罗盘应用) - Cannot get heading data (for compass app) in swift2 on ios9 如何在swift2,iOS9中复制UIAlertController的消息? - How to make message of UIAlertController copyable in swift2, iOS9? 长按时自定义注释引脚更改为默认红色引脚 - Custom annotation pin changes to default red pin at long tap 在Swift2中使用iOS9压缩框架 - Using the iOS9 Compression Framework in Swift2 Facebook在iOS9和Swift2中使用Parse登录 - Facebook login with Parse in iOS9 and Swift2 如何将红色的Pin更改为自定义图像? iOS版 - How do I change the red Pin to a custom image? iOS 快速将自定义注释图钉图像更新为标准注释图钉 - Update custom annotation pin image to standard annotation pin in swift 如何在Mapview iOS 8中将注释图钉添加为自定义图像 - How to add annotation pin as custom image in Mapview ios 8 如何使用swift2在地图视图中的第二个注释/ pin中添加不同的按钮动作 - How to add different button action to the second annotation /pin in map view using swift2 ios9 swift2 ekeventviewcontroller完成的条形按钮消失 - ios9 swift2 ekeventviewcontroller done bar button disappear
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM