Open Apple Maps programmatically

前端 未结 3 1334
没有蜡笔的小新
没有蜡笔的小新 2021-01-02 08:12

I want to open the Apple Maps App in my own Swift App, but I have only zipcode, city & street. I have NO Coordinates. I researched a lot, but there were only ways with u

3条回答
  •  情深已故
    2021-01-02 09:09

    Using Swift 4 and Xcode 9

    At the top:

    import CoreLocation
    

    Then:

    let geocoder = CLGeocoder()
    
    let locationString = "London"
    
    geocoder.geocodeAddressString(locationString) { (placemarks, error) in
        if let error = error {
            print(error.localizedDescription)
        } else {
            if let location = placemarks?.first?.location {
                let query = "?ll=\(location.coordinate.latitude),\(location.coordinate.longitude)"
                let urlString = "http://maps.apple.com/".appending(query)
                if let url = URL(string: urlString) {
                    UIApplication.shared.open(url, options: [:], completionHandler: nil)
                }
            }
        }
    }
    

提交回复
热议问题