Webview谷歌地图当前位置无法检索,打开谷歌地图应用程序的导航失败

时间:2019-01-14 15:03:03

标签: android google-maps android-studio android-webview

我已经为我的应用创建了一个Google Maps Webview,它无法获取当前位置,并且在设置了目的地和起始位置之后,导航并非意图用于Google Maps的人可以帮助我解决这个问题吗?

package example.asus.com.map;

import android.content.Intent;
import android.net.Uri;
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
 *
 *  
 */
public class MainActivity extends Activity {

/**
 * WebViewClient subclass loads all hyperlinks in the existing WebView
 */
public class GeoWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        // When user clicks a hyperlink, load in the existing WebView
        view.loadUrl(url);
        return true;
    }
}

/**
 * WebChromeClient subclass handles UI-related calls
 * Note: think chrome as in decoration, not the Chrome browser
 */
public class GeoWebChromeClient extends WebChromeClient {
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin,

GeolocationPermissions.Callback callback) {
        // Always grant permission since the app itself requires location
        // permission and the user has therefore already granted it
        callback.invoke(origin, true, false);
    }
}

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 webView, String url) {
        return false;
    }
}
}

能够获取当前位置
能够导航到当前位置到目的地

1 个答案:

答案 0 :(得分:0)

您的代码几乎是正确的:需要获取WebChromeClient定制的onGeolocationPermissionsShowPrompt()的当前位置。但是onGeolocationPermissionsShowPrompt()应该更复杂,以允许在API 6.0+上进行地理定位的权限:它应在运行时请求该权限,并且主机活动应按onRequestPermissionsResult()处理响应,如{ {3}}:

对于您的源代码,它可能类似于:

// 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);
            }
        }
    }
} 
在这种情况下,

onRequestPermissionsResult()可能是:

@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;
    }
}

此外,您的应用还应该在android.permission.ACCESS_FINE_LOCATION上声明AndroidManifest.xml权限:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />