Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save matthijsotterloo/3def603f224d83f2b8f620f2174c535e to your computer and use it in GitHub Desktop.
Save matthijsotterloo/3def603f224d83f2b8f620f2174c535e to your computer and use it in GitHub Desktop.
Open location in Apple Maps using Swift.
func openMaps() {
let geocoder = CLGeocoder()
let str = "1600 Pennsylvania Ave. 20500" // A string of the address info you already have
geocoder.geocodeAddressString(str) { (placemarksOptional, error) -> Void in
if let placemarks = placemarksOptional {
print("placemark| \(placemarks.first)")
if let location = placemarks.first?.location {
let query = "?ll=\(location.coordinate.latitude),\(location.coordinate.longitude)"
let path = "http://maps.apple.com/" + query
if let url = NSURL(string: path) {
UIApplication.sharedApplication().openURL(url)
} else {
// Could not construct url. Handle error.
}
} else {
// Could not get a location from the geocode request. Handle error.
}
} else {
// Didn't get any placemarks. Handle error.
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment