Users location (blue dot) keeps turning into red pin

91 Views Asked by At

I would like to have the users location as a blue dot while having pins also on the mapview. I would like the pins to have an annotation that has a an info button. I can get the blue dot for users location and the enable the pins to have an annotation such as a title an subtitle. However when I go to add the info button to the red pins the users location (blue dot) turns into a red pin.

I can't seem to spot where I'm going wrong. It has something to do with the last function because this is the function that puts a the info button onto the annotation. But it also selects the users current location and turns it into a pin for some reason :(

 class GetToTheStart: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!




let myLocMgr = CLLocationManager()


    override func viewDidLoad() {
    super.viewDidLoad()


    myLocMgr.desiredAccuracy = kCLLocationAccuracyBest
    myLocMgr.requestWhenInUseAuthorization()
    myLocMgr.startUpdatingLocation()
    myLocMgr.delegate = self




    mapView.delegate = self


  var zoo = CLLocationCoordinate2DMake(53.3562, -6.3053)

    var zoopin = MKPointAnnotation()
    zoopin.coordinate = zoo
    zoopin.title = "dublin zoo"
    zoopin.subtitle = "hello this is the zoo"
    mapView.addAnnotation(zoopin)
    }



    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    // get most recient coordinate
    let myCoor = locations[locations.count - 1]

    //get lat & long
    let myLat = myCoor.coordinate.latitude
    let myLong = myCoor.coordinate.longitude
    let myCoor2D = CLLocationCoordinate2D(latitude: myLat, longitude: myLong)

    //set span
    let myLatDelta = 0.05
    let myLongDelta = 0.05
    let mySpan = MKCoordinateSpan(latitudeDelta: myLatDelta, longitudeDelta: myLongDelta)

    let myRegion = MKCoordinateRegion(center: myCoor2D, span: mySpan)

    self.mapView.showsUserLocation = true        
    }





    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    let reuseIdentifier = "pin"
    var pin =       mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
    if pin == nil {
        pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        pin!.pinColor = .Red
        pin!.canShowCallout = true
        pin!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
    } else {
        pin!.annotation = annotation
    }
    return pin
}
1

There are 1 best solutions below

1
Naveen Kumar H S On BEST ANSWER

viewForAnnotation delegate method will be called by all the annotation on the map including userLocation.

So you just check and return nil for it as shown below...

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {  
    if annotation is MKUserLocation {
    //return nil so map view draws "blue dot" for standard user location
        return nil
    }
    let reuseIdentifier = "pin"
    var pin = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseIdentifier) as? MKPinAnnotationView
    if pin == nil {
        pin = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier)
        pin!.pinColor = .Red
        pin!.canShowCallout = true
        pin!.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
     } else {
        pin!.annotation = annotation
     }
     return pin
}