2017-10-26 11 views
1

私はマップを持っており、このマップ上に私はカスタム注釈ピンを持っています。すべてのピンは同じカスタムイメージを持っています。ピンをクリックすると、この注釈の画像を変更する必要があります。これは正常に動作してSwift 3のマップキットで選択したピンイメージを変更するにはどうすればいいですか?

func mapView(_ mapView: GMSMapView, didTap marker: GMSMarker) -> Bool { 
    if marker.userData as? Int != nil { 
     let marker_tag = marker.userData as! Int 
     Datas.selectedMarkerIndex = marker_tag 

     if let selectedMarker = mapView.selectedMarker { 
      selectedMarker.icon = UIImage(named: "marker_gray") 
     } 
     mapView.selectedMarker = marker 
     marker.icon = UIImage(named: "marker_red")       
    } 
    return true 
} 

は私が前にGoogleマップを使用していました。しかし、私はMapKitでそれを行う方法を知らない。私はちょうど選択マーカー(ピン)画像を変更したいです。 どうすればいいですか?

はまた、私はこれを試みたが、 How to change non selected annotation pin images on mapkit with Swift

を働き、これは私のコードではありません。

class CustomPointAnnotation: MKPointAnnotation { 
    var pinImageName:String! 
    var userData:Int! 
} 

extension MyView: MKMapViewDelegate { 

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? { 
    let reuseIdentifier = "my_pin" 
    var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseIdentifier) 

    if annotationView == nil { 
     annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseIdentifier) 

    } else { 
     annotationView?.annotation = annotation 
    } 

    let customPointAnnotation = annotation as! CustomPointAnnotation 
    annotationView?.isDraggable = false 
    annotationView?.canShowCallout = false 
    annotationView?.layer.anchorPoint = CGPoint(x: 0.5, y: 1) 
    annotationView?.image = UIImage(named: customPointAnnotation.pinImageName) 

    return annotationView 
} 

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 
    if let anno = view.annotation as? CustomPointAnnotation {    
     let marker_tag = anno.userData! 
     print(marker_tag) 

    } 
} 

func addMarkersOnMap(){ 
    if Datas.myArray.count != 0 { 
     Datas.selectedMarkerIndex = 0 
     for i in 0..<Datas.myArray.count{ 
      let lat = Datas.myArray[i].lat 
      let lng = Datas.myArray[i].lng 

      myArray.append(CustomPointAnnotation())     
      if lat != nil {      
       let location = CLLocationCoordinate2D(latitude: lat!, longitude: lng!) 
       myArray[i].pinImageName = "marker_gray" 
       myArray[i].coordinate = location 
       myArray[i].userData = i 
       pinAnnotationView = MKPinAnnotationView(annotation: myArray[i], reuseIdentifier: "my_pin") 
       mapView.addAnnotation(pinAnnotationView.annotation!) 

     } 
} 
+0

map.delegateをselfに設定しましたか? –

+0

はい、もちろん.. – Hilalkah

答えて

3

"選択" と "タップ"?その場合は、次のコードを試してください:

func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) { 
    view.image = UIImage(named: "marker_gray") 
} 

func mapView(_ mapView: MKMapView, didDeselect view: MKAnnotationView) { 
    view.image = UIImage(named: "marker_red") 
} 
+1

ありがとう!それはとても簡単でした。外を選択する場合は、次のコードを使用します。 mapView.selectAnnotation(myArray [i]、animated:true) – Hilalkah

関連する問題