【问题标题】:Webview google maps current location cannot retrieve and navigation for opening the google maps app failedWebview 谷歌地图当前位置无法检索,打开谷歌地图应用的导航失败
【发布时间】:2019-01-14 15:03:02
【问题描述】:

我为我的应用程序创建了一个谷歌地图 webview,它无法获取当前位置,在我设置目的地和起始位置后,导航不打算谷歌地图有人可以帮我解决这个问题吗?

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

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

【问题讨论】:

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


    【解决方案1】:

    您的代码几乎是正确的:获取当前位置自定义 WebChromeClientonGeolocationPermissionsShowPrompt() 需要。但是onGeolocationPermissionsShowPrompt() 应该更复杂,以允许 API 6.0+ 上的地理定位权限:它应该在运行时请求权限,并且主机 Activity 应该在onRequestPermissionsResult() 中处理响应,如this EricRobertBrewer 的回答中所述:

    对于您的源代码,它可以是:

    // 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_LOCATIONAndroidManifest.xml 上的权限:

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

    【讨论】:

    • 无法获取地理位置,点击导航按钮后出现网页不可用
    • 在给定两个地点的来源和目的地后仍然找不到当前位置导航按钮仍然显示网页不可用
    • 现在它已经可以接收当前位置,但是单击 web 视图后的导航按钮无法将数据意向谷歌地图
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多