Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show native google map in webview in android?

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?

like image 258
captaindroid Avatar asked Apr 01 '12 06:04

captaindroid


2 Answers

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.

like image 114
Arun Badole Avatar answered Nov 13 '22 13:11

Arun Badole


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;
    }
});
like image 32
AnujAroshA Avatar answered Nov 13 '22 13:11

AnujAroshA