用户位置(蓝点)不断变成红色引脚

时间:2016-07-28 11:46:39

标签: annotations mkmapview mapkit cllocationmanager mkpinannotationview

我希望将用户位置设为蓝点,同时在mapview上也有引脚。我希望这些引脚有一个带有信息按钮的注释。 我可以获得用户位置的蓝点,并使引脚具有标题,例如标题和副标题。然而,当我将信息按钮添加到红色引脚时,用户位置(蓝点)变成红色引脚。

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

 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 个答案:

答案 0 :(得分:1)

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

所以你只需检查并返回nil,如下所示......

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
}