Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing MKMapView User Location Annotation

I'm attempting to change the user default location annotation on the MKMapView from the blue to a custom image named geo. It's hitting the line to set it to geo when I set breakpoints, but both points (the user default, and Passenger point are default red pinpoint annotations) Am I setting this wrong, or is there certain image stipulations?

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

        if annotation.isKindOfClass(PassengerLocation) == false {

            //User location
            let userIdentifier = "UserLocation"
            var annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(userIdentifier)

            if annotationView == nil {
                annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:userIdentifier)
            }
            annotationView!.annotation = annotation
            annotationView!.canShowCallout = true

            // THIS IS NOT WORKING, DEFAULT RED PIN POINT STILL SHOWING
            annotationView!.image = UIImage(named: "geo")
            return annotationView


        }

        let identifier = "PassengerLocation"

        if let annotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(identifier) {
            annotationView.annotation = annotation

            return annotationView
        } else {
            let annotationView = MKPinAnnotationView(annotation:annotation, reuseIdentifier:identifier)
            annotationView.enabled = true
            annotationView.canShowCallout = true

            let btn = UIButton(type: .DetailDisclosure)
            annotationView.rightCalloutAccessoryView = btn

            return annotationView
        }

    }
like image 207
Jamie22 Avatar asked Apr 27 '16 22:04

Jamie22


2 Answers

This works:

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? { 
        if annotation.isEqual(mapView.userLocation) {
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "userLocation")
        annotationView.image = UIImage(named: "geo")
        return annotationView
    }
}
like image 182
123FLO321 Avatar answered Oct 21 '22 20:10

123FLO321


its working for me

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

    // want to show a custom image if the annotation is the user's location.
    guard !annotation.isKindOfClass(MKUserLocation) else {
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "userLocation")
        annotationView.image = UIImage(named: "icon_coordinates_self")
        return annotationView
        //return nil
    }

    let annotationIdentifier = "AnnotationIdentifier"

    var annotationView: MKAnnotationView?
    if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationViewWithIdentifier(annotationIdentifier) {
        annotationView = dequeuedAnnotationView
        annotationView?.annotation = annotation
    }
    else {
        let av = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier)
        av.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
        annotationView = av
    }

    if let annotationView = annotationView {
        // Configure your annotation view here
        annotationView.canShowCallout = true
        annotationView.image = UIImage(named: "Annotation_map")
    }

    return annotationView
}
like image 22
Museer Ahamad Ansari Avatar answered Oct 21 '22 21:10

Museer Ahamad Ansari