如何将Google地图显示为WebView?

时间:2015-06-11 14:14:16

标签: ios swift uiwebview

当我点击按钮时,我的应用会显示地图:它会在浏览器的Safari中完美地打开Goog​​le地图。

现在我只想将地图显示为UIWebView

以下是我用来显示地图的代码:

@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 个答案:

答案 0 :(得分:3)

只需在IBAction上显示UIWebView?你会想要这样的东西:

@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")!))
}