How can I show Google Maps into a WebView?

4.1k Views Asked by At

When I click the button my app shows a map: it opens the google map perfectly in the browser safari.

Now I just want to show the map into a UIWebView.

Here is the code I use to show the map:

@IBAction func newAction(sender: UIBarButtonItem) {
    if (UIApplication.sharedApplication().canOpenURL(NSURL(string:"comgooglemaps://")!)) {
        UIApplication.sharedApplication().openURL(NSURL(string:
            "http://maps.google.com/maps?daddr=" + self.longitud + "," + self.latitud + "&saddr=" + self.lat1 + "," + self.long1 + "&views=traffic")!)
    } else {
        mostrarAlerta("Por favor descarga e instala google map desde el AppleStore")
    }
}
1

There are 1 best solutions below

6
On BEST ANSWER

Just display UIWebView on the IBAction? You're going to want something like this:

@IBAction func newAction(sender: UIBarButtonItem) {

    // set up webview
    let webView = UIWebView(frame: self.view.frame) // or pass in a CGRect frame of your choice

    // add webView to current view
    self.view.addSubview(webView)
    self.view.bringSubviewToFront(webView)

    // load the Google Maps URL
    webView.loadRequest(NSURLRequest(URL: NSURL(string: "http://maps.google.com/maps?daddr=" + self.longitud + "," + self.latitud + "&saddr=" + self.lat1 + "," + self.long1 + "&views=traffic")!))
}