如何打开地图应用程序以swift坐标编程方式?

我有我想要打开我的地图应用程序的经度和纬度。 我从这里试过这个代码。

func goToMap(){ var lat1 : NSString = self.venueLat var lng1 : NSString = self.venueLng var latitude:CLLocationDegrees = lat1.doubleValue var longitude:CLLocationDegrees = lng1.doubleValue var coordinate = CLLocationCoordinate2DMake(latitude, longitude) var placemark : MKPlacemark = MKPlacemark(coordinate: coordinate, addressDictionary:nil) var mapItem:MKMapItem = MKMapItem(placemark: placemark) mapItem.name = "Target location" let launchOptions:NSDictionary = NSDictionary(object: MKLaunchOptionsDirectionsModeDriving, forKey: MKLaunchOptionsDirectionsModeKey) var currentLocationMapItem:MKMapItem = MKMapItem.mapItemForCurrentLocation() MKMapItem.openMapsWithItems([currentLocationMapItem, mapItem], launchOptions: launchOptions) } 

此function成功打开地图,但不显示任何引脚。 也显示了我不想要的用户位置。 我只想在地图上提供一个纬度和经度。

这段代码对我来说工作正常。

 func openMapForPlace() { let lat1 : NSString = self.venueLat let lng1 : NSString = self.venueLng let latitude:CLLocationDegrees = lat1.doubleValue let longitude:CLLocationDegrees = lng1.doubleValue let regionDistance:CLLocationDistance = 10000 let coordinates = CLLocationCoordinate2DMake(latitude, longitude) 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 = "\(self.venueName)" mapItem.openInMapsWithLaunchOptions(options) } 

对于swift 3.0:

 import UIKit import MapKit class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() openMapForPlace() } func openMapForPlace() { let latitude: CLLocationDegrees = 37.2 let longitude: CLLocationDegrees = 22.9 let regionDistance:CLLocationDistance = 10000 let coordinates = CLLocationCoordinate2DMake(latitude, longitude) 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 = "Place Name" mapItem.openInMaps(launchOptions: options) } } 

如果你只是想给用户提供方向,下面是最简单的Swift语法:

 let coordinate = CLLocationCoordinate2DMake(theLatitude,theLongitude) let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil)) mapItem.name = "Target location" mapItem.openInMapsWithLaunchOptions([MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving]) 

上面的MKMapItem方法可以很好地控制地图中显示的信息。

否则,下面的代码也很好用:

 // Open and show coordinate let url = "http://maps.apple.com/maps?saddr=\(coord.latitude),\(coord.longitude)" UIApplication.shared.openURL(URL(string:url)!) // Navigate from one coordinate to another let url = "http://maps.apple.com/maps?saddr=\(from.latitude),\(from.longitude)&daddr=\(to.latitude),\(to.longitude)" UIApplication.shared.openURL(URL(string:url)!) 

但是,上面的代码不允许你发送一个自定义名称的地方。 相反,它会显示地址。

上面的代码也可以让你从任何源坐标导航,我不知道你是否可以用MKMapItem方法。

这对我来说是一种魅力

 let coordinate = CLLocationCoordinate2DMake(theLatitude, theLongitude) let region = MKCoordinateRegionMake(coordinate, MKCoordinateSpanMake(0.01, 0.02)) let placemark = MKPlacemark(coordinate: coordinate, addressDictionary: nil) let mapItem = MKMapItem(placemark: placemark) let options = [ MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: region.center), MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: region.span)] mapItem.name = theLocationName mapItem.openInMaps(launchOptions: options) 
Interesting Posts