如何用swift中的坐标以编程方式打开地图App?

Dha*_*esh 96 ios apple-maps 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)

}
Run Code Online (Sandbox Code Playgroud)

此功能可以成功打开地图,但不会显示任何图钉.它还显示了我不想要的用户位置.我只想在地图上找到所提供纬度和经度的图钉.

Dha*_*esh 146

这段代码对我来说很好.

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)

}
Run Code Online (Sandbox Code Playgroud)

对于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)
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 只是想指出它是"纬度"和"经度"不是"替代"和"长期",虽然"tute"更有趣 (5认同)
  • 别忘了:导入MapKit (4认同)
  • 当一个或多个`MKMapItem`被添加到地图时,似乎忽略了`MKLaunchOptionsMapSpanKey`:http://stackoverflow.com/a/32484331/422288 (2认同)
  • 如果用户已删除地图应用程序,我们如何检查该案例??? (2认同)

Joe*_*e C 57

如果您只是想给用户提供行车路线,这里是最简单形式的最新Swift语法:

let coordinate = CLLocationCoordinate2DMake(theLatitude,theLongitude)
let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil))
mapItem.name = "Target location"
mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving])
Run Code Online (Sandbox Code Playgroud)

  • 同样在swift 3中,最后一行现在是... mapItem.openInMaps(launchOptions:[MKLaunchOptionsDirectionsModeKey:MKLaunchOptionsDirectionsModeDriving]) (2认同)

Dan*_*idi 33

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)!)
Run Code Online (Sandbox Code Playgroud)

但是,上面的代码不允许您发送该地点的自定义名称.相反,它会显示地址.

上面的代码还允许您从任何源坐标导航,我不知道您是否可以使用MKMapItem方法.

  • 如果您将"saddr ="留空,则应用会将"您的位置"设置为默认值.像"http:// maps.apple.com/maps?saddr=&daddr=\(to.latitude),,(to.longitude)"之类的东西 (4认同)

dim*_*iax 31

你可以调用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])
Run Code Online (Sandbox Code Playgroud)


Way*_*ang 11

这对我来说是一种魅力

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)
Run Code Online (Sandbox Code Playgroud)

  • 这对我不再起作用了。我最终得到“没有可用的方向”。 (2认同)

Mal*_*007 5

您可以使用下面的代码在 Apple 地图上显示经纬度上的 PIN 码。

\n\n
let coordinates = CLLocationCoordinate2DMake(-37.848854,144.990295)\n\nlet regionSpan =   MKCoordinateRegionMakeWithDistance(coordinates, 1000, 1000)\n\nlet placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)\n\nlet mapItem = MKMapItem(placemark: placemark)\n\nmapItem.name = \xe2\x80\x9cDesired place\xe2\x80\x9d\n\nmapItem.openInMaps(launchOptions:[\nMKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center)\n] as [String : Any])\n
Run Code Online (Sandbox Code Playgroud)\n