我正在创建一个已经具有基于 ios mapkit 的签到功能的应用程序。目前它只显示城市和国家,但我的客户想要更多。Het 希望能够像 instagram 一样在地方签到。(地名,例如Restoname..)
我想知道地图套件库是否可以做到这一点?如果是这样,是否有人对此有一个代码示例。??
听起来您想使用地标和注释。创建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)
}
}
}
然后,您可以从字符串中对地址进行地理定位(这将是写出的地址:123 Fake St.,New York,NY ....
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)
这应该有效。希望对你有帮助