无法从WebView打开Goog​​le Maps Google Maps导航按钮

时间:2019-01-15 10:58:02

标签: android google-maps navigation android-webview

我已经完成了到目的地的当前位置,但是在显示路线后,我单击了导航按钮,它无法打开谷歌地图来导航,这可以帮助我解决此问题。我尝试了很多方法,但是仍然无法工作

package example.asus.com.map;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.GeolocationPermissions;
import android.webkit.WebChromeClient;

import android.webkit.WebViewClient;
import android.app.Activity;


/**
 * A minimal WebView app with HTML5 geolocation capability
 *
 * @author David M. Chandler
 */
public class MainActivity extends Activity {

/**
 * WebViewClient subclass loads all hyperlinks in the existing WebView
 */
// Geolocation permission request code
private static final int RP_ACCESS_LOCATION = 1001;

// global variables for the origin for permission and interface used by     
the your application to set the Geolocation permission state for an origin
private String mGeolocationOrigin;
private GeolocationPermissions.Callback mGeolocationCallback;

public class GeoWebChromeClient extends WebChromeClient {
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin, 
GeolocationPermissions.Callback callback) {
        final String permission = 
Manifest.permission.ACCESS_FINE_LOCATION;
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
                ContextCompat.checkSelfPermission(MainActivity.this, 
permission) == PackageManager.PERMISSION_GRANTED) {
            // that is you already implement, but it works only
            // we're on SDK < 23 OR user has ALREADY granted permission
            callback.invoke(origin, true, false);

        } else {
            if 
(ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this, 
permission)) {
// user has denied this permission before and selected [/]DON'T ASK ME AGAIN
                // TODO Best Practice: show an AlertDialog explaining why 
the user could allow this permission, then ask again
            } else {
                // store
                mGeolocationOrigin = origin;
                mGeolocationCallback = callback;
                // ask the user for permissions
                ActivityCompat.requestPermissions(MainActivity.this, new 
String[] {permission}, RP_ACCESS_LOCATION);
            }
        }

    }

}
public class GeoWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if(url.contains("geo:google.com/maps")) {
            Uri gmmIntentUri = Uri.parse(url);
            Intent mapIntent = new Intent(Intent.ACTION_VIEW, 
gmmIntentUri);
            mapIntent.setPackage("com.google.android.apps.maps");
            if (mapIntent.resolveActivity(getPackageManager()) != null) {
                startActivity(mapIntent);
            }
            return true;
        }
        // When user clicks a hyperlink, load in the existing WebView
        view.loadUrl(url);
        return true;

    }
}



WebView mWebView;

/**
 * Called when the activity is first created.
 */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mWebView = (WebView) findViewById(R.id.Webview);
    // Brower niceties -- pinch / zoom, follow links in place
    mWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    mWebView.getSettings().setBuiltInZoomControls(true);
    mWebView.setWebViewClient(new GeoWebViewClient());
    // Below required for geolocation
    mWebView.getSettings().setJavaScriptEnabled(true);
    mWebView.getSettings().setGeolocationEnabled(true);
    mWebView.setWebChromeClient(new GeoWebChromeClient());
    // Load google.com
    mWebView.loadUrl("http://www.google.com/maps");

}

@Override
public void onBackPressed() {
    // Pop the browser back stack or exit the activity
    if (mWebView.canGoBack()) {
        mWebView.goBack();
    } else {
        super.onBackPressed();
    }
}
private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView mWebView, String url) 
{
        return false;
    }

}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] 
permissions, @NonNull int[] grantResults) {

    super.onRequestPermissionsResult(requestCode, permissions, 
grantResults);
    switch (requestCode) {
        case RP_ACCESS_LOCATION:
            boolean allow = false;
            if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                // user has allowed these permissions
                allow = true;
            }
            if (mGeolocationCallback != null) {
                // use stored callback and origin for allowing Geolocation 
permission for WebView
                mGeolocationCallback.invoke(mGeolocationOrigin, allow, 
false);
            }
            break;
    }

}
}

当点击导航按钮

时,可以获取位置和目的地去google map

0 个答案:

没有答案