Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open Apple Maps programmatically

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 using coordination information.

like image 987
Philipp Januskovecz Avatar asked Nov 18 '15 18:11

Philipp Januskovecz


People also ask

Is there an Apple Maps API?

The Apple Maps Server API is a web-based API similar to the MapKit JS API, and uses the same authorization infrastructure. If you already have a MapKit JS identifier and private key, you can skip these steps.

Is Apple Maps API free?

If you are using MapKit for native app development, there is no cost beyond your Apple Developer Program membership. There are rate limits per device for some of the APIs, such as geocoding, but you'll only encounter them if you're sending off requests at more than the speed a normal user could reasonably use your app.

How do I open Apple maps in Swift?

Create the functionCreate a new Swift file from Xcode and call it OpenMapDirections, then copy/paste the following code in it: We will use the above code to present an alert controller with 2 actions: one for opening Google Maps, the other to open Apple Maps. Here we pass the coordinate for both directly.

What is MapKit in iOS?

MapKit is a powerful API available on iOS devices that makes it easy to display maps, mark locations, enhance with custom data and even draw routes or other shapes on top.


2 Answers

Swift 4 and above

Use this version to get the coordinates for the Address, there is also a way to open it directly with the address, but that is susceptible to errors

import CoreLocation

let myAddress = "One,Apple+Park+Way,Cupertino,CA,95014,USA"
let geoCoder = CLGeocoder()
geoCoder.geocodeAddressString(myAddress) { (placemarks, error) in
    guard let placemarks = placemarks?.first else { return }
    let location = placemarks.location?.coordinate ?? CLLocationCoordinate2D()
    guard let url = URL(string:"http://maps.apple.com/?daddr=\(location.latitude),\(location.longitude)") else { return }
    UIApplication.shared.open(url)
}

Apple has a Documentation about the Map URL Scheme. Look here: https://developer.apple.com/library/archive/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html#//apple_ref/doc/uid/TP40007899-CH5-SW1

like image 74
Jonas Deichelmann Avatar answered Nov 15 '22 14:11

Jonas Deichelmann


You can just pass your address information as URL parameters in the URL with which you open the maps app. Say you wanted the maps app to open centered on The White House.

UIApplication.sharedApplication().openURL(NSURL(string: "http://maps.apple.com/?address=1600,PennsylvaniaAve.,20500")!)

The Maps app opens with the ugly query string in the search field but it shows the right location. Note that the city and state are absent from the search query, it's just the street address and the zip.

A potentially better approach, depending on your needs, would be to get the CLLocation of the address info you have using CLGeocoder.

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.
  }
}
like image 45
geraldWilliam Avatar answered Nov 15 '22 12:11

geraldWilliam