2016-11-21 7 views
0

別のアノテーションと検索でマップビューを作成しました。私はピンに応じて別の画像を属性することはできません。私はまた別の懸念を抱いています。私が検索をすると、それを新しい場所に置くピンを削除します。以下は若いコーダーのコードです。事前にお手数おかけします。画像のアノテーションが異なる

import MapKit 
import UIKit 
import CoreLocation 

class MapViewController: UIViewController, UISearchBarDelegate, MKMapViewDelegate { 

    @IBOutlet weak var MapView: MKMapView! 

    var searchController:UISearchController! 
    var annotation:MKAnnotation! 
    var localSearchRequest:MKLocalSearchRequest! 
    var localSearch:MKLocalSearch! 
    var localSearchResponse:MKLocalSearchResponse! 
    var error:NSError! 
    var pointAnnotation:MKPointAnnotation! 
    var pinAnnotationView:MKPinAnnotationView! 

    var coordinates: [[Double]]! 
    var name:[String]! 

    let regionRadius: CLLocationDistance = 1000 

    @IBAction func showSearchBar(_ sender: Any) { 

     searchController = UISearchController(searchResultsController: nil) 
     searchController.hidesNavigationBarDuringPresentation = false 
     self.searchController.searchBar.delegate = self 
     present(searchController, animated: true, completion: nil) 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 


     self.MapView.delegate = self 

     var Tirta = CustomPointAnnotation() 
     Tirta.coordinate = CLLocationCoordinate2DMake(-8.415162, 115.315360) 
     Tirta.title = "Tirta Empul" 
     Tirta.imageName = "PaConseil.png" 

     var Goa = CustomPointAnnotation() 
     Goa.coordinate = CLLocationCoordinate2DMake(-8.551313, 115.468865) 
     Goa.title = "Goa Lawah" 
     Goa.imageName = "PaMecontent.png" 

     MapView.addAnnotation(Tirta) 
     MapView.addAnnotation(Goa) 

     // 3 
     let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: -8.670458199999999, longitude: 115.2126293), span: MKCoordinateSpan(latitudeDelta: 2, longitudeDelta: 2)) 
     self.MapView.setRegion(region, animated: true) 
    } 

    func MapView(mapView: MKMapView!, viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 

     print("delegate called") 

     if !(annotation is CustomPointAnnotation) { 
      return nil 
     } 

     let reuseId = "test" 

     var AnnotationView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) 
     if AnnotationView == nil { 
      AnnotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
      AnnotationView?.canShowCallout = true 
     } 
     else { 
      AnnotationView?.annotation = annotation 
     } 

     func didReceiveMemoryWarning() { 
      super.didReceiveMemoryWarning() 

     } 

     let CustomPointAnnotation = annotation as! CustomPointAnnotation 
     AnnotationView?.image = UIImage(named:CustomPointAnnotation.imageName) 

     return AnnotationView 
    } 

    class CustomPointAnnotation: MKPointAnnotation { 
     var imageName: String! 
    } 

    func searchBarSearchButtonClicked(_ searchBar: UISearchBar){ 
     //1 
     searchBar.resignFirstResponder() 
     dismiss(animated: true, completion: nil) 
     if self.MapView.annotations.count != 0{ 
      annotation = self.MapView.annotations[0] 
      self.MapView.removeAnnotation(annotation) 
     } 
     //2 
     localSearchRequest = MKLocalSearchRequest() 
     localSearchRequest.naturalLanguageQuery = searchBar.text 
     localSearch = MKLocalSearch(request: localSearchRequest) 
     localSearch.start { (localSearchResponse, error) -> Void in 

      if localSearchResponse == nil{ 
       let alertController = UIAlertController(title: nil, message: "Place Not Found", preferredStyle: UIAlertControllerStyle.alert) 
       alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.default, handler: nil)) 
       self.present(alertController, animated: true, completion: nil) 
       return 
      } 
      //3 
      self.pointAnnotation = MKPointAnnotation() 
      self.pointAnnotation.title = searchBar.text 
      self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude:  localSearchResponse!.boundingRegion.center.longitude) 


      self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil) 
      self.MapView.centerCoordinate = self.pointAnnotation.coordinate 
      self.MapView.addAnnotation(self.pinAnnotationView.annotation!) 
     } 

    } 
} 


import Foundation 
import MapKit 

class CustomPointAnnotation: MKPointAnnotation { 

    var coordinates: CLLocationCoordinate2D 
    var name: String! 
    var imageName: UIImage! 

    init(coordinates: CLLocationCoordinate2D) { 
     self.coordinates = coordinates 
    } 
} 
+0

私はあなたがAnnotationView .image = UIImage(命名:CustomPointAnnotation.imageName) 'と言って見ることができる「私はピンに応じて異なる画像を属性することはできません」'ので、どこのものですか。間違っている? – matt

+0

問題は、私は異なる注釈の画像を持っていないということです。私は従来のピンに留まっています – SWOON

+0

さて、その行が実行されているかどうかを確認するためにブレークポイントを設定しましたか? – matt

答えて

0

私はあなたが今、その後スウィフト3.まあを使用しているように、問題は、このメソッドは実行されないされないことですXcodeのを更新したと仮定するつもりです:

func MapView(mapView: MKMapView!, 
    viewForAnnotation annotation: MKAnnotation!) -> MKAnnotationView! { 

。 ..その理由は、それが正しい "署名"ではないということです。この方法は、現在like thisが定義されています

func mapView(_ mapView: MKMapView, 
    viewFor annotation: MKAnnotation) -> MKAnnotationView? 
+0

もう一つの問題は、あなたが間違って 'func didReceiveMemoryWarning()'を他のメソッドの中に "埋め込んでしまった"ということです。 – matt

+0

ありがとうございます – SWOON

関連する問題