2016-11-21 1 views
0

Ich erstellte eine Kartenansicht mit verschiedenen Annotation und einer Suche. Ich kann kein anderes Bild nach dem Pin zuordnen. Ich habe auch eine andere Sorge, wenn ich eine Suche mache, entfernt es eine Nadel, um es auf den neuen gesuchten Platz zu setzen. Unten ist der Code eines jungen Coders. Vielen Dank für Ihre Hilfe.Bild Annotation anders

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

„Ich kann nicht ein anderes Bild nach dem Pin-Attribut kann:“ Ich kann Sie sehen, sagen 'AnnotationView .image = UIImage (genannt: CustomPointAnnotation.imageName)?' So, wo es Dinge falsch machen? – matt

+0

Das Problem ist, dass ich keine Bilder von verschiedenen Anmerkungen habe. Ich bleibe auf einem traditionellen Pin – SWOON

+0

Okay, haben Sie auch einen Haltepunkt gesetzt, um zu sehen, ob diese Zeile ausgeführt wird? – matt

Antwort

0

Ich gehe davon aus, dass Sie Xcode aktualisiert haben, so dass Sie jetzt dann Swift 3. Nun verwenden, ist das Problem, dass diese Methode nie ausgeführt wird:

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

. .. und der Grund ist, dass das nicht die richtige "Signatur" ist. Das Verfahren wird nun like this definiert:

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

Ein anderes Problem, das nichts damit zu tun hat, ist, dass Sie versehentlich Ihre 'func didReceiveMemoryWarning()' in dieser anderen Methode "vergraben" haben. – matt

+0

Genaues Dankeschön – SWOON

Verwandte Themen