作者:卢晓影_611 | 来源:互联网 | 2023-05-21 12:38
我有经度和经度,我想打开我的地图应用程序.我从这里试过这段代码.
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)
}
此功能可以成功打开地图,但不会显示任何图钉.它还显示了我不想要的用户位置.我只想在地图上找到所提供纬度和经度的图钉.
1> Dharmesh..:
这段代码对我来说很好.
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)
}
}
只是想指出它是"纬度"和"经度"不是"替代"和"长期",虽然"tute"更有趣
别忘了:导入MapKit
当一个或多个`MKMapItem`被添加到地图时,似乎忽略了`MKLaunchOptionsMapSpanKey`:http://stackoverflow.com/a/32484331/422288
如果用户已删除地图应用程序,我们如何检查该案例???
2> Joe C..:
如果您只是想给用户提供行车路线,这里是最简单形式的最新Swift语法:
let coordinate = CLLocationCoordinate2DMake(theLatitude,theLongitude)
let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil))
mapItem.name = "Target location"
mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving])
同样在swift 3中,最后一行现在是... mapItem.openInMaps(launchOptions:[MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving])
3> Daniel Saidi..:
该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方法.
如果您将"saddr ="留空,则应用会将"您的位置"设置为默认值.像"http:// maps.apple.com/maps?saddr=&daddr=\(to.latitude),,(to.longitude)"之类的东西
4> dimpiax..:
你可以调用MKMapItem
在那里传递项目的类函数,如果你想要传递两个以上的项目,它只适用于源/目的地的第一个和最后一个.
斯威夫特4
let source = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lng)))
source.name = "Source"
let destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lng)))
destination.name = "Destination"
MKMapItem.openMaps(with: [source, destination], launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])
5> Wayne Huang..:
这对我来说是一种魅力
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)