[英]Open Apple Maps App With Directions from my ios App iOS 9 Swift 2.2
我一直在寻找很多教程\\帮助,但似乎没有找到任何,所以我再次来到这里....我想打开Apple地图获取路线并从用户当前位置导航到选定Pin当我在我的应用程序中按自定义leftCalloutAccessory时
我已经设置了按钮,但无法使用该功能,所以请“如果有人可以指导我或帮助我使用代码,这将是一个救生员! 谢谢
这是我的代码:
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate,UISearchBarDelegate{
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
//==========RegionLocation : =========
// Init the zoom level
let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 31.30, longitude: 34.45)
let span = MKCoordinateSpanMake(125, 125)
let region = MKCoordinateRegionMake(coordinate, span)
self.mapView.setRegion(region, animated: true)
//====================================\\
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
//Dispose of resources that can be re created.
}
//Mark : Location
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.06, longitudeDelta: 0.06))
self.mapView.setRegion(region, animated: true)
self.locationManager.stopUpdatingLocation()
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Errors: " + error.localizedDescription)
}
func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let reuseID = "pin"
var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
if(pinView == nil) {
pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
pinView!.canShowCallout = true
pinView!.animatesDrop = true
pinView!.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIButton
let smallSquare = CGSize(width: 30, height: 30)
let button = UIButton(frame: CGRect(origin: CGPointZero, size: smallSquare))
button.setBackgroundImage(UIImage(named: "Car"), forState: .Normal)
pinView?.leftCalloutAccessoryView = button
}
else
{
pinView!.annotation = annotation
}
return pinView
}
func mapView(MapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped Control: UIControl) {
if Control == annotationView.leftCalloutAccessoryView {
}
}
以下功能将打开Apple Maps应用程序,并显示从用户当前位置到坐标的指定目的地的行车路线。 目标名称显示在地图应用的收件人:字段中。
func openMapsAppWithDirections(to coordinate: CLLocationCoordinate2D, destinationName name: String) {
let options = [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving]
let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placemark)
mapItem.name = name // Provide the name of the destination in the To: field
mapItem.openInMapsWithLaunchOptions(options)
}
您可以从代码中调用此函数,如下所示:
func mapView(MapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped Control: UIControl) {
if Control == annotationView.leftCalloutAccessoryView {
if let annotation = annotationView.annotation {
// Unwrap the double-optional annotation.title property or
// name the destination "Unknown" if the annotation has no title
let destinationName = (annotation.title ?? nil) ?? "Unknown"
openMapsAppWithDirections(to: annotation.coordinate, destinationName: destinationName)
}
}
}
像这样创建扩展:
extension CLLocation {
func drive() {
let placemark = MKPlacemark(coordinate: self.coordinate, addressDictionary: addressDictionary)
let launchOptions = [
MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving
]
MKMapItem.openMaps(with: [MKMapItem.forCurrentLocation(), MKMapItem(placemark: placemark)], launchOptions: launchOptions)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.