How to show native google map in webview in android?

17,683

Solution 1

Please check this.It does not open native Google Map app in WebView instead it loads http://maps.google.com/maps?~~ in WebView.

May Help.

Solution 2

Try this:

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;
    }
});
Share:
17,683
captaindroid
Author by

captaindroid

all work and no play makes Jack a dull boy ♫

Updated on June 27, 2022

Comments

  • captaindroid
    captaindroid about 2 years

    I have the following code to show 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");
        }
    }
    

    I want to open following code in 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);
    

    How can i do this?

  • captaindroid
    captaindroid about 12 years
    displays just a white screen !!
  • AnujAroshA
    AnujAroshA about 12 years
    Did you get a Google Map API key and the related XML code from here code.google.com/android/maps-api-signup.html
  • captaindroid
    captaindroid about 12 years
    I dont use MapActivity here, why do i need Google Map API key ? If needed where should i put the api key ? My Complete class code is above.
  • AnujAroshA
    AnujAroshA about 12 years
    I gave the code for a normal Activity class. It is working if you have given the android.permission.INTERNET in the Manifest. The above link is for WebView. That's all I can help.