我目前浏览了一些有关如何更改注释的图钉的教程。但是我所遇到的只是xcode 7中的错误(主要是所有断点,它无法正常工作)。我想做的是使用资产文件夹中的Cloud,并替换普通的Annotation。从指南中我已经看到人们只是在使用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
最佳答案
尝试这个...
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
}
关于ios - 将图像添加到MKAnnotationViewClass(更改注释针),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34689901/