How to open maps App programmatically with coordinates in swift?

前端 未结 9 2213
忘了有多久
忘了有多久 2020-11-30 18:40

I have latitude and longitude that I want to open into my map application. I tried this code from HERE.

    func goToMap(){

    var lat1 : NSString = self.v         


        
相关标签:
9条回答
  • 2020-11-30 19:20

    You can use below code to show PIN on lat, long in to Apple map.

    let coordinates = CLLocationCoordinate2DMake(-37.848854,144.990295)
    
    let regionSpan =   MKCoordinateRegionMakeWithDistance(coordinates, 1000, 1000)
    
    let placemark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
    
    let mapItem = MKMapItem(placemark: placemark)
    
    mapItem.name = “Desired place”
    
    mapItem.openInMaps(launchOptions:[
    MKLaunchOptionsMapCenterKey: NSValue(mkCoordinate: regionSpan.center)
    ] as [String : Any])
    
    0 讨论(0)
  • 2020-11-30 19:20

    I know all answers are complete but here I got an answer which is easier to copy paste & also gives the user options to routing with Apple Maps, Google Map & Waze.

    Working with Swift 5+

    https://stackoverflow.com/a/60930491/6449292

    Might help someone...

    0 讨论(0)
  • 2020-11-30 19:21

    If you just want to give the user driving directions, here's the latest Swift syntax in its simplest form:

    let coordinate = CLLocationCoordinate2DMake(theLatitude,theLongitude)
    let mapItem = MKMapItem(placemark: MKPlacemark(coordinate: coordinate, addressDictionary:nil))
    mapItem.name = "Target location"
    mapItem.openInMaps(launchOptions: [MKLaunchOptionsDirectionsModeKey : MKLaunchOptionsDirectionsModeDriving])
    
    0 讨论(0)
  • 2020-11-30 19:25

    This code is working fine for me.

    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)
    
    }
    

    For 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)
        }
    }
    
    0 讨论(0)
  • 2020-11-30 19:28

    You could call class function of MKMapItem passing items there, it uses only first and last for source / destination appropriately, if you want pass more than two items.

    Swift 5, 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])
    

    or using extension:

    extension MKMapItem {
      convenience init(coordinate: CLLocationCoordinate2D, name: String) {
        self.init(placemark: .init(coordinate: coordinate))
        self.name = name
      }
    }
    
    let source = MKMapItem(coordinate: .init(latitude: lat, longitude: lng), name: "Source")
    let destination = MKMapItem(coordinate: .init(latitude: lat, longitude: lng), name: "Destination")
    
    MKMapItem.openMaps(
      with: [source, destination], 
      launchOptions: [MKLaunchOptionsDirectionsModeKey: MKLaunchOptionsDirectionsModeDriving])
    
    0 讨论(0)
  • 2020-11-30 19:28

    Update to the practical Daniel Saidi answer. This example is for telling only the destination coordinates. Maps will get as origin the user current position.

    let url = URL(string: "http://maps.apple.com/maps?saddr=&daddr=\(lat),\(lon)")
    UIApplication.shared.open(url!)
    
    
    0 讨论(0)
提交回复
热议问题