2017-01-23 172 views
2
@IBOutlet weak var map: MKMapView! 
override func viewDidLoad() { 
    map.delegate = self 
    showAlarms() 
    super.viewDidLoad() 

    // Do any additional setup after loading the view. 
} 

func showAlarms(){ 

    map.region.center.latitude = 10.733051 
    map.region.center.longitude = 76.763042 
    map.region.span.latitudeDelta = 1 
    map.region.span.longitudeDelta = 1 


} 
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 reuseId = "pin" 
    var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView 
    if pinView == nil { 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     pinView!.canShowCallout = true 
     //pinView!.animatesDrop = true 
     pinView!.image = UIImage(named:"annotation")! 

    } 
    else { 
     pinView!.annotation = annotation 
    } 

    return pinView 
} 

我需要在swift ios中顯示我的mapview的自定義圖釘圖像。自定義圖像不會加載該引腳。我有一個名爲「註釋」的圖像,我需要在我的mapview中加載。如何在Mapview swift ios中設置自定義圖釘圖像?

+0

viewForAnnotation的委託方法是否爲您調用。 –

+0

@KiritModi。我不這麼認爲。它只顯示地圖,沒有別的。請幫忙。 – Rakesh

+0

你是swift 3中的代碼 –

回答

3
mapview.delegate = self 

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
guard !(annotation is MKUserLocation) else { 
    return nil 
} 

let annotationIdentifier = "Identifier" 
var annotationView: MKAnnotationView? 
if let dequeuedAnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: annotationIdentifier) { 
    annotationView = dequeuedAnnotationView 
    annotationView?.annotation = annotation 
} 
else { 
    annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: annotationIdentifier) 
    annotationView?.rightCalloutAccessoryView = UIButton(type: .detailDisclosure) 
} 

if let annotationView = annotationView { 

    annotationView.canShowCallout = true 
    annotationView.image = UIImage(named: "yourImagename」) 
} 
    return annotationView 
} 
0

您還可以創建自定義MKAnnotationView並設置在圖像內:

class myCustomAnnotationView : MKAnnotationView { 

    override var annotation: MKAnnotation? { 
    willSet { 
     guard let annotation = newValue else {return} 
     switch annotation { 
     case is myCustomMKAnnotation: 
      self.canShowCallout = true 
      self.image = #yourimage 
      self.centerOffset = CGPoint(x: 0, y: -self.image!.size.height/2) 
      break 
     default: 
      return 
     } 
     self.setAnimationLayer() 
    } 
} 

然後所有你需要做的是:

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { return nil } 
    guard !(annotation is MKUserLocation) else { 


    let annotationIdentifier = "Identifier" 
    if let myAnnotation = annotation as? myCustomMKAnnotation { 
     let annotation = myCustomAnnotationView(annotation: siteAnnotation, reuseIdentifier: annotationIdentifier) 
     return annotation 
    } 
    return nil 
} 

我認爲它看起來更清潔一樣,

相關問題