2017-01-23 10 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 
} 

私の地図ビューのカスタムピン画像をすばやく表示する必要があります。カスタムイメージはピンをロードしていません。マップビューに読み込む必要がある「注釈」という名前の画像があります。その後、Mapview swift iosでカスタムピンイメージを設定するにはどうすればよいですか?

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 
} 

私はそれがそのようなきれいに見えると思うあなたはまた、カスタムMKAnnotationViewを作成して、内部の画像を設定することができ

+0

viewForAnnotationのデリゲートメソッドは、あなたのために呼び出されます。 @KiritModi。 –

+0

私はそうは思わない。それは地図だけを表示しています。助けてください。 – Rakesh

+0

あなたはコードをスワイプしています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

関連する問題