13

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.

Sulthan
  • 128,090
  • 22
  • 218
  • 270
Philipp Januskovecz
  • 868
  • 5
  • 14
  • 25
  • You could use Google's Geocoding API to convert your address to coordinates https://developers.google.com/maps/documentation/geocoding/intro – Ollie Nov 18 '15 at 19:34

4 Answers4

22

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.
  }
}
geraldWilliam
  • 4,123
  • 1
  • 23
  • 35
  • 1
    Instead of using commas to separate words in the string, you can you %20 instead of spaces so for example "1600%20Pennsylvania%20Ave.%2020500" will work for the first option of your answer and will open in maps like "1600 Pennsylvania Ave. 20500" – JCode Jun 27 '16 at 21:29
  • +1 @JCode, the query should be "encoded" so that spaces and other special characters are parsed properly – Abhishek Ghosh Oct 18 '21 at 22:40
11

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

Jonas Deichelmann
  • 3,513
  • 1
  • 30
  • 45
4

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)
            }
        }
    }
}
Zandor Smith
  • 558
  • 6
  • 25
rbaldwin
  • 4,581
  • 27
  • 38
3

Swift 5: Directly open maps app

///Opens text address in maps
static func openAddressInMap(address: String?){
    guard let address = address else {return}
    
    let geoCoder = CLGeocoder()
    geoCoder.geocodeAddressString(address) { (placemarks, error) in
        guard let placemarks = placemarks?.first else {
            return
        }
        
        let location = placemarks.location?.coordinate
        
        if let lat = location?.latitude, let lon = location?.longitude{
            let destination = MKMapItem(placemark: MKPlacemark(coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lon)))
            destination.name = address
            
            MKMapItem.openMaps(
                with: [destination]
            )
        }
    }
}