我正在尝试在屏幕加载时自动启动地图。
可以这样做吗?在视图上启动此功能确实出现。
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if let anno = view.annotation {
let place = MKPlacemark(coordinate: anno.coordinate)
let destination = MKMapItem(placemark: place)
destination.name = "Vehicle Sighting"
let regionDistance: CLLocationDistance = 1000
let regionSpan = MKCoordinateRegionMakeWithDistance(anno.coordinate, regionDistance, regionDistance)
let options = [MKLaunchOptionsMapCenterKey: NSValue (mkCoordinate: regionSpan.center), MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span), MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving] as [String : Any]
MKMapItem.openMaps(with: [destination], launchOptions: options)
}
}
答案 0 :(得分:1)
尝试使用这个功能,你只需要传递坐标和地名,这个工作,我在几个项目中使用它
static func openMapsAppWithLocation(coordinates:CLLocationCoordinate2D,placeName:String)
{
let regionDistance:CLLocationDistance = 10000
let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
let options = [
MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
]
let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = placeName
mapItem.openInMaps(launchOptions: options)
}
希望这有帮助
答案 1 :(得分:0)
使用@Reinier答案的替代版本。
这显示了它如何被实现到视图控制器之前的动作中,而不需要转到下一个控制器来启动视图中的加载。我如何从邮政编码
获得坐标var addressCode = [[String: Double]]()
@IBAction func onMapPressed(_ sender: Any) {
let address = postCode
let geocoder = CLGeocoder()
geocoder.geocodeAddressString(address) {
placemarks, error in
let placemark = placemarks?.first
let lat = placemark?.location?.coordinate.latitude
let lon = placemark?.location?.coordinate.longitude
// let locationTitle = self.places]
let latLon = ["latitude":lat!, "longitude": lon!]
self.addressCode.append(latLon)
for location in self.addressCode {
let annotation = MKPointAnnotation()
annotation.coordinate = CLLocationCoordinate2D(latitude: location["latitude"]!, longitude: location["longitude"]!)
self.openMapsAppWithLocation(coordinates: annotation.coordinate, placeName: self.makeLabel.text!)
}
}
}