Programmatically Open Maps App in iOS 6

Programmatically open Maps app in iOS 6

Found the answer to my own question. Apple documents its maps URL format here. It looks like you can essentially replace maps.google.com with maps.apple.com.

Update: It turns out that the same is true in MobileSafari on iOS 6; tapping a link to http://maps.apple.com/?q=... opens the Maps app with that search, the same way http://maps.google.com/?q=... did on previous versions. This works and is documented in the page linked above.

UPDATE: This answers my question relating to the URL format. But nevan king's answer here (see below) is an excellent summary of the actual Maps API.

Open Apple Maps programmatically

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

How to open maps App programmatically with coordinates in swift?

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

Swift 5:

let latitude: CLLocationDegrees = Double(K.latitude)!
let longitude: CLLocationDegrees = Double(K.longitude)!
let regionDistance:CLLocationDistance = 10000
let coordinates = CLLocationCoordinate2DMake(latitude, longitude)
let regionSpan = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: 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 = K.companyName
mapItem.openInMaps(launchOptions: options)

Open Google Maps App in iOS 6

[[UIApplication sharedApplication] canOpenURL:
[NSURL URLWithString:@"comgooglemaps://"]];

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:
@"comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic"]];

reference: https://developers.google.com/maps/documentation/ios/urlscheme

Programmatically launch Apple Maps without directions or a place mark

Use the following code in your application to open just Apple Maps

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://maps.apple.com/?q="]];

How to open iOS 6 maps from a mobile website?

Instead of using the previous Google domain maps.google.com you use maps.apple.com. This will remain compatible with older devices running 5.x and below.

It looks like this:

http://maps.apple.com/maps?q=cupertino

Most of the parameters are the same, but see the Apple docs for more details:

Apple URL Scheme Reference: Map Links

How do I open the Maps App in Apple Watch with a specific Location?

If you display a map with only one location pin, when the user taps the map, it will open the map app to that location. This is the only way currently to open the map app on the watch. Confirmed by Apple in Apple Developer Forums

"There is no url to call at this time. The user tapping on a
WKInterfaceMap object is the only way to launch the Maps app from
yours."

Apple Dev Forums



Related Topics



Leave a reply



Submit