简体   繁体   中英

How to Open Google Maps from Webview click

I have an app with a webview. The webview links to a mobile webpage where all links work correctly. When I click on a link in this webview, the link opens up in google maps inside the webview. Instead, I want the link to open up the native google maps app. Can anyone tell me how to do this? There is literally nothing online about it. Thank you!

EDIT: Based on Matiash's response. here is the code I have added:

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("www.MYURL.com")) {
                return false;
            }

            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }

You should add a WebViewClient as the other response indicates, but the correct method to override is shouldOverrideUrlLoading() .

Return true to indicate that the link has been handled, otherwise false .

i used this code cause ihad maps links and phone numbers links :

 @Override
    public boolean shouldOverrideUrlLoading(WebView wv, String url) {
        if(url.startsWith("tel:")) {
            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse(url));
            startActivity(intent);
            return true;
        }

        if(url.startsWith("https://maps.google")){
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
        wv.loadUrl(url);
        return false;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM