将图像添加到MKAnnotationViewClass(更改注释引脚)

时间:2016-01-09 05:06:06

标签: ios mkmapview mapkit xcode7 mkpointannotation

我目前正在查看有关如何更改注释引脚的几个教程。但我遇到的只不过是xcode 7中的错误(主要是所有的断点而且它没有正常工作)。我要做的是使用我的资产文件夹中的云,并替换正常的注释。从导游我看到人们只是使用png文件,我不确定他们应该在哪里?但是,对代码的任何建议都可以让我替换它吗?

干杯谢谢

继承我的代码

let locationManager = CLLocationManager()

override func viewDidLoad() {
    super.viewDidLoad()

    let longPressRecognizer = UILongPressGestureRecognizer(target: self, action: "handleLongPress:")
    longPressRecognizer.minimumPressDuration = 1.0
    longPressRecognizer.delaysTouchesBegan = true
    longPressRecognizer.delegate = self
    self.mapView.addGestureRecognizer(longPressRecognizer)

    self.locationManager.delegate = self

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

    self.locationManager.requestWhenInUseAuthorization()

    self.locationManager.startUpdatingLocation()

    self.mapView.showsUserLocation = true

}


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


func handleLongPress(getstureRecognizer : UIGestureRecognizer) {
    if getstureRecognizer.state != .Began { return }

    let touchPoint = getstureRecognizer.locationInView(self.mapView)
    let touchMapCoordinate = mapView.convertPoint(touchPoint, toCoordinateFromView: mapView)

    let annotation = MKPointAnnotation()
    annotation.coordinate = touchMapCoordinate
    mapView.addAnnotation(annotation)


}


func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    let location = locations.last
    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
    let region = MKCoordinateRegion(center: center, span:  MKCoordinateSpan(latitudeDelta: 10, longitudeDelta: 10))

    self.mapView.setRegion(region, animated: true)
    self.locationManager.stopUpdatingLocation()
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    if !(annotation is MKPointAnnotation) {
        return nil
    }

    let reuseId = "Cloud"

    var CloudAnnotation = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseId)
    if CloudAnnotation == nil {
        CloudAnnotation = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId)
        CloudAnnotation!.image = UIImage(named:"cloud.png")
        CloudAnnotation!.canShowCallout = true
    }
    else {
        CloudAnnotation!.annotation = annotation
    }

    return CloudAnnotation
}


func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("Errors: " + error.localizedDescription)
}

和MKAnnotationViewClass

import UIKit
import MapKit

class CustomPointAnnotation: MKPointAnnotation {
    var pinCustomImageName:String!
}

我查看的以前的主题信息

Change pin image on MKMapView in Swift

Swift different images for Annotation

1 个答案:

答案 0 :(得分:1)

试试这个......

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

    if annotation is MKPointAnnotation {

        let identifier = "stopAnnotation"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier)
        if pinView == nil {
            //println("Pinview was nil")

            //Create a plain MKAnnotationView if using a custom image... 
            pinView = MKAnnotationView(annotation: annotation, reuseIdentifier: identifier)

            pinView!.canShowCallout = true
            pinView.image = UIImage(named: "stopIcon")
        }
        else {
            //Unrelated to the image problem but...
            //Update the annotation reference if re-using a view...
            pinView.annotation = annotation
        }

        return pinView          
    }
    return nil
}
相关问题