如何在android中的webview中显示原生谷歌地图?

时间:2012-04-01 06:47:44

标签: android google-maps android-webview

我有以下代码来显示webview:

webview.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

WebViewActivity.java

public class WebViewActivity extends Activity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.webview);

    webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient());
    webView.loadUrl("http://maps.google.com/maps?" +"saddr=43.0054446,-87.9678884" + "&daddr=42.9257104,-88.0508355");




}

}  

我想在WebView中打开以下代码:

 final Intent intent = new Intent(Intent.ACTION_VIEW,
      Uri.parse(
               "http://maps.google.com/maps?" +
               "saddr=43.0054446,-87.9678884" +
               "&daddr=42.9257104,-88.0508355"));

            intent.setClassName(
             "com.google.android.apps.maps",
             "com.google.android.maps.MapsActivity");
      startActivity(intent);

我该怎么做?

2 个答案:

答案 0 :(得分:2)

试试这个......

    private String url = "http://maps.google.com/maps";

    webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if (url.equals("http://maps.google.com/maps")) {

                Intent intent = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?" + "saddr=43.0054446,-87.9678884" + "&daddr=42.9257104,-88.0508355"));
                intent.setClassName("com.google.android.apps.maps", "com.google.android.maps.MapsActivity");
                startActivity(intent);
            } else {
                //else part
            }

            return true;
        }
    });

答案 1 :(得分:2)

请检查this。它不会在WebView中打开原生Google地图应用,而是在WebView中加载http://maps.google.com/maps?~~

可以帮助。