0

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

我似乎无法找到我要出错的地方。它与最后一个函数有关,因为这是将info按钮放到注释中的函数。但它也选择了用户当前的位置,并把它变成一个针因某种原因:(

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 
} 

回答

2

viewForAnnotation委托方法将被映射包括用户位置上所有的注释被调用。

所以你只是检查并返回为零,如下所示...

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 
} 
+0

这就是工作感谢的人:) –

+0

随着快乐...虽然张贴问题主要至少对代码部分至少要有一些标准 –

相关问题