swift2 在swift4中使用latlong在iPhone和iPad中打开iPhone系统Map

hfwmuf9z  于 2022-11-06  发布在  Swift
关注(0)|答案(2)|浏览(214)

我有纬度和经度。我想设置按钮点击打开系统苹果默认Map与设置选定的纬度和经度,也应该在Map默认设置。

sg24os4d

sg24os4d1#

在Button操作处粘贴以下代码

let directionsURL = "http://maps.apple.com/?saddr=35.6813023,139.7640529&daddr=35.4657901,139.6201192"
guard let url = URL(string: directionsURL) else {
    return
}
if #available(iOS 10.0, *) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
} else {
    UIApplication.shared.openURL(url)
}
uoifb46i

uoifb46i2#

这个代码可以为你工作-

import UIKit
import MapKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        showMapForMyIphone()
    }

    func showMapForMyIphone() {

        let latitude: CLLocationDegrees = "Enter your latitude"
        let longitude: CLLocationDegrees = "Enter your longitude"

        let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
        let regionSpan = MKCoordinateRegionMakeWithDistance(coordinates, regionDistance, regionDistance)
        let options = 
        [
            MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center),
            MKLaunchOptionsMapSpanKey: NSValue(mkCoordinateSpan: regionSpan.span)
        ]

        //This will enable a placemerk for your map
        let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
        let mapItem = MKMapItem(placemark: placemark)
        mapItem.name = "Place Name"
        mapItem.openInMaps(launchOptions: options)
    }

相关问题