Похоже, вы хотите работать с метками места и аннотациями. При создании MapView
убедитесь, что он соответствует MKMapViewDelegate
:
extension ViewController: MKMapViewDelegate {
func mapView(_ mapView: MKMapView, didAdd views: [MKAnnotationView]) {
// once annotationView is added to the map, get the last one added unless it is the user's location:
if let annotationView = views.last {
// show callout programmatically:
mapView.selectAnnotation(annotationView.annotation!, animated: false)
// zoom to all annotations on the map:
mapView.showAnnotations(mapView.annotations, animated: true)
}
}
}
Затем вы можете Geolocate адрес из строки (который будет адрес выписанный как: 123 Фиктивный St., Нью-Йорк, Нью-Йорк .. ..
func createGeoLocationFromAddress(_ address: String, mapView: MKMapView) {
let completion:CLGeocodeCompletionHandler = {(placemarks: [CLPlacemark]?, error: Error?) in
if let placemarks = placemarks {
for placemark in placemarks {
mapView.removeAnnotations(mapView.annotations)
// Instantiate annotation
let annotation = MKPointAnnotation()
// Annotation coordinate
annotation.coordinate = (placemark.location?.coordinate)!
annotation.title = placemark.thoroughfare! + ", " + placemark.subThoroughfare!
annotation.subtitle = placemark.subLocality
mapView.addAnnotation(annotation)
mapView.showsPointsOfInterest = true
self.centerMapOnLocation(placemark.location!, mapView: mapView)
}
} else {
}
}
CLGeocoder().geocodeAddressString(address, completionHandler: completion)
}
func centerMapOnLocation(_ location: CLLocation, mapView: MKMapView) {
let regionRadius: CLLocationDistance = 1000
let coordinateRegion = MKCoordinateRegionMakeWithDistance(location.coordinate, regionRadius * 2.0, regionRadius * 2.0)
mapView.setRegion(coordinateRegion, animated: true)
}
Тогда вы можете разместить аннотацию на карте, просто позвонив
createGeoLocationFromAddress(addressString, mapView: mapKit)
Это должно работать. Надеюсь, что это помогает
Вы пытались реализовать * FourSquare *? – AtWork
Нет, я не был, но мне трудно было быстро с этим быстро? – greg