How to launch Google Maps or Apple Maps from an iOS app with Swift

In need a sample code hoe to launch Google Maps or Apple Maps with Swift


thx

I'm assuming you want a view embedded in your app. If you want to launch a seperate app then ignore this.


Google maps: Add a web view to your app and load a "map.html" file from your bundle that contains the needed html and javascript. If you are using nibs the initialization code looks something like this:


    override func awakeFromNib() {
        let mapPath = NSBundle.mainBundle().pathForResource("map",
                                                            ofType: "html")
        let mapURL = NSURL(fileURLWithPath: mapPath!, isDirectory: false)
        let map = NSURLRequest(URL: mapURL)
        webView.mainFrame.loadRequest(map)
    }


Oh, you did say iOS. I think that code should go in viewDidLoad. For Apple maps add a MapKitMapView to your app and add a controller to your code.


@objc(MapViewController)
class MapViewController: NSViewController, MKMapViewDelegate {
    @IBOutlet var mapView: MKMapView

    // startup

    override func viewDidLoad() { // 10.10 and later
        super.viewDidLoad() {
    }

    func displayMapAtLatitude(latitude: Double, longitude: Double) {
        var region = mapView.region
        region.center = CLLocationCoordinate2D(latitude: latitude, longitude: longitude)
        mapView.setRegion(region, animated: true)
    }
}

Hello Marchyman


thx for your reply. What i mean is like this:


http://www.reigndesign.com/blog/how-to-launch-google-maps-or-apple-maps-from-an-ios-app/


Only this is written in ObjectC. I have an app with markers and i can't figered out how to setup directions with GoogleMaps SDK for ios. So i want to give the users an option to choose between googemaps en apple maps to get directions from the current location to de selected marker.


thxx

To launch Apple Maps or Google Maps, the first thing you need to do is create an NSURL using the appropriate form:


https://developer.apple.com/library/ios/featuredarticles/iPhoneURLScheme_Reference/MapLinks/MapLinks.html

let targetURL = NSURL(string: "http://maps.apple.com/?q=cupertino")!


h t t p s ::// developers DOT google DOT com /maps/documentation/ios/urlscheme

let targetURL = NSURL(string: "comgooglemaps://?q=cupertino")!



You can double check that the user has the right mapping app installed by checking with your own app whether the url can be handled:

let isAvailable = UIApplication.sharedApplication().canOpenURL(targetURL)


Then to open the url, you just ask your own app to do it and it should be redirected:

UIApplication.sharedApplication().openURL(targetURL)

Thx LCS,


Now i have more options. I'm using a Alert view with this code.


@IBAction func PressedButton(sender: AnyObject) {

var choose = UIAlertView()

choose.addButtonWithTitle("Google Maps")

choose.addButtonWithTitle("Apple Maps")

choose.title = "Navigate"

choose.show()

}



Now i doný now how te link dthe buttons with the follwing code then with the current locaton to the marker:


//GoogleMaps

if (UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!)) {
UIApplication.sharedApplication().openURL(NSURL(string:
"comgooglemaps://?center=40.765819,-73.975866&zoom=14&views=traffic")!)
} else {
NSLog("Can't use Google Maps");
}


//Apple Maps

if (UIApplication.sharedApplication().canOpenURL(NSURL(string:"http://maps.apple.com")!)) {
UIApplication.sharedApplication().openURL(NSURL(string:
"http://maps.apple.com/?daddr=San+Francisco,+CA&saddr=cupertino)!)
} else {
NSLog("Can't use Apple Maps");
}



Thx

Is there any easy method available to open all the available map app in my phone. For example currently in whatsapp I don't get the option to open Mappls App.

How to launch Google Maps or Apple Maps from an iOS app with Swift
 
 
Q