【发布时间】:2019-03-21 02:54:38
【问题描述】:
我对 MapKit 还很陌生,一旦选择了地点标记,我就会尝试显示信息和方向。我正在展示当地医院和紧急服务。如何获取当前选择的地标的信息。我希望能够显示有关所选标记的几行信息。例如姓名、地址、电话号码,可能还有一个方向按钮。我想将当前选择的标记坐标保存到一个变量中。
class MapKitViewController: UIViewController, MKMapViewDelegate {
let locationManager = CLLocationManager()
let regionInMeters: Double = 10000
var previousLocation: CLLocation?
let geoCoder = CLGeocoder()
var directionsArray: [MKDirections] = []
func setupLocationManager() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
}
func centerViewOnUserLocation() {
if let location = locationManager.location?.coordinate {
let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
mapView.setRegion(region, animated: true)
}
}
func checkLocationServices() {
if CLLocationManager.locationServicesEnabled() {
setupLocationManager()
performSearch()
checkLocationAuthorization()
} else {
// Show alert letting the user know they have to turn this on.
}
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation)
-> MKAnnotationView? {
let identifier = "marker"
var view: MKMarkerAnnotationView
if let dequeuedView = mapView.dequeueReusableAnnotationView(
withIdentifier: identifier)
as? MKMarkerAnnotationView {
dequeuedView.annotation = annotation
view = dequeuedView
} else {
view =
MKMarkerAnnotationView(annotation: annotation,
reuseIdentifier: identifier)
view.markerTintColor = UIColor.blue
view.canShowCallout = true
view.calloutOffset = CGPoint(x: -5, y: 5)
view.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
return view
}
func mapView(_: MKMapView, annotationView:
MKAnnotationView, calloutAccessoryControlTapped: UIControl) {
print("Control tapped")
}
func mapView(_ mapView: MKMapView, didSelect view: MKAnnotationView) {
}
【问题讨论】:
-
您所说的“地标”是什么?它是注释标记还是地标?
-
我的意思是图像中的红色大头针,结果显示在地图上。
标签: swift mapkit mapkitannotation