Reputation: 3
I've currently looked through the few tutorials on how to change the pin for the annotation. But all I've came across was nothing but errors within xcode 7 (Mainly all the breakpoints and it not working correctly). What I'm trying to do is use a Cloud that I have in my assets folder, and have that replace the normal Annotation. From the guides i've seen people are just using png files in which i'm not exactly sure where they should be located? But nonetheless any suggestions to the code that would allow me to replace it?
Cheers and Thanks
Heres my code
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)
}
and for the MKAnnotationViewClass
import UIKit
import MapKit
class CustomPointAnnotation: MKPointAnnotation {
var pinCustomImageName:String!
}
Previous threads that I looked at for information
Change pin image on MKMapView in Swift
Swift different images for Annotation
Upvotes: 0
Views: 954
Reputation: 6342
Try this...
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
}
Upvotes: 2