我正在创建一个已经拥有基于ios mapkit的登记功能的应用程序。 目前它只显示城市和国家,但我的客户想要更多。 Het希望能够在像Instagram这样的地方办理登机手续..(以地点名称为例,Restoname ..)
我想知道这是否可以使用mapkit库? 如果是这样的话,任何人都有代码例子。 ??
答案 0 :(得分:0)
听起来你想要使用地方标记和注释。创建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)
这应该有效。希望它有所帮助