2016-11-21 47 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 =圖像配的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