用户位置(蓝点)不断变成红色图钉

Users location (blue dot) keeps turning into red pin

我希望将用户位置显示为蓝点,同时在地图视图上也显示图钉。我希望引脚有一个带有信息按钮的注释。 我可以获得用户位置的蓝点,并使引脚具有注释,例如标题和副标题。但是,当我将信息按钮添加到红色图钉时,用户位置(蓝点)变成了红色图钉。

我似乎无法发现我哪里出错了。它与最后一个函数有关,因为这是将信息按钮放在注释上的函数。但由于某种原因,它也会选择用户当前位置并将其转换为图钉:(

 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
}

viewForAnnotation 委托方法将被地图上的所有注释调用,包括 userLocation.

所以您只需检查 return 为零,如下所示...

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
}