2017-05-27 53 views
0

我正在使用MapKit,我更改了Annotation的圖像,但問題是,用戶位置(藍點)也改變了它的圖片(我不希望那樣)。我怎樣才能恢復正常。另外,我在標註中添加了一個小按鈕(正如您可能已經猜到的那樣),該按鈕已添加到我的位置,但它不應該在那裏。我的代碼如下...(MapView)用戶位置圖像更改幫助恢復

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
     if !(annotation is MKUserLocation) { 
      print("annotation is MKUserLocation") 
     } 
     var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier") 
      if annotationView == nil{ 
       annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "identifier") 
       annotationView!.canShowCallout = true 
       annotationView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) 

       let pinImg = UIImage(named: "curz") 
       let size = CGSize(width: 50, height: 50) 
       UIGraphicsBeginImageContext(size) 
       pinImg!.draw(in: CGRect(x: 0, y: 0, width: size.width, height: size.height)) 
       let resizedImage = UIGraphicsGetImageFromCurrentImageContext() 
       UIGraphicsEndImageContext() 
       annotationView!.image = resizedImage 
       } 
     else { 
     annotationView!.annotation = annotation 
     } 
    return annotationView 
    } 

     //Var SelectedAnnotation 
     var anotacionSeleccionada : MKPointAnnotation! 

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) { 
     if control == view.rightCalloutAccessoryView { 
     anotacionSeleccionada = view.annotation as? MKPointAnnotation 
     performSegue(withIdentifier: "vista", sender: self) 
     } 
     } 


    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     if let destination = segue.destination as? ViewController { 
     destination.pin = anotacionSeleccionada 
     } 
    } 

回答

1

在你viewFor annotation函數開頭添加此行:

if annotation is MKUserLocation { 
    return nil 
} 

因爲不想將做任何事情,如果it's的MKUserLocation

+0

我是否將該行代替「if!(註解是MKUserLocation)」? –

+1

是的,你可以@DanielC。 –

+0

它的工作原理!謝謝! –