简体   繁体   中英

Google Maps link not opening from webView on Android App

I am loading a webView in my app that displays some buttons, each button starts a route that is generated using google maps.

The problem is that when I click "Navigate in app" nothing happens.

I was having the URL_UNKOWN_SCHEME error but solved it by adding and @override

My code looks like this:

package com.example.listarehtgps2;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    WebView web;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        web = findViewById(R.id.webview);

        WebSettings webSettings = web.getSettings();
        webSettings.setJavaScriptEnabled(true);

        web.loadUrl("webViewURL replaced for privacy reasons");

        web.setWebViewClient(new WebViewClient() {
                 @Override
                 public boolean shouldOverrideUrlLoading(WebView view, String url) {
                     // When user clicks a hyperlink, load in the existing WebView
                     if(url.contains("intent:")) {
                         Uri gmmIntentUri = Uri.parse(url);
                         Intent mapIntent = new Intent(Intent.ACTION_VIEW, gmmIntentUri);
                         startActivity(mapIntent);
                         return true;
                     }
                     view.loadUrl(url);
                     return true;
                 }
             }
            );

        }
    }


I can not find any solution to open the link in the google maps app. I managed to solve the UNKWN_URL_SCHEME but this is frustrating now.

Thanks in advance!

The URL_UNKOWN_SCHEME is because it's not recognising the scheme part of your URI (the bit up to :), are your URIs simply starting with "intent:" ?

To open a map at a location, try switching to the geo scheme, so a URI something like "geo:0.01,0.01?q=London" (along ofc with changing your url.contains check to look for that). You could also insert a mapIntent.setPackage("com.google.android.apps.maps") before the call to startActivity(mapIntent) if you want Google maps to take priority over any other map application the user may have installed.

If however you want navigation, use a URI starting with google.navigation such as "google.navigation:London" or "google.navigation:0.01,0.01", equally for street view you can use a URI starting with google.streetview

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