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
}
}
„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
Das Problem ist, dass ich keine Bilder von verschiedenen Anmerkungen habe. Ich bleibe auf einem traditionellen Pin – SWOON
Okay, haben Sie auch einen Haltepunkt gesetzt, um zu sehen, ob diese Zeile ausgeführt wird? – matt