6

我遵循了本教程:http ://android-er.blogspot.com/2013/03/embed-google-map-in-webview.html

我正在尝试仅在 WebView 中使用 Google 地图,但它无法获取我当前的位置。我在 WebView 上启用了 JavaScript。我还必须启用什么?

有谁知道为什么会这样?它不应该提示我使用我当前的位置吗?

请注意,我对使用 MapView 作为替代方案不感兴趣。我试图找出我需要在 WebView 或设备的位置服务上设置什么?

4

4 回答 4

4

您应该通过覆盖以下方法来允许 Web 视图访问您的位置onGeolocationPermissionsShowPrompt

webView.setWebChromeClient(new WebChromeClient(){

        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
        }
    });
于 2013-07-26T10:18:40.207 回答
3

在 API 5.x 及更低版本上,您将需要

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

在你的AndroidManifest.xml.

但是要在 API 6.0+ 上允许地理定位权限,您必须在运行时请求权限

请求位置许可

为此,请使用

private String mGeolocationOrigin;
private GeolocationPermissions.Callback mGeolocationCallback;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // other setup

    myWebView.setWebChromeClient(new MyWebChromeClient());
}

private WebChromeClient mWebChromeClient = new WebChromeClient() {
    @Override
    public void onGeolocationPermissionsShowPrompt(String origin,
                                                   GeolocationPermissions.Callback callback) {
        // Geolocation permissions coming from this app's Manifest will only be valid for devices with API_VERSION < 23.
        // On API 23 and above, we must check for permission, and possibly ask for it.
        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) {
            // 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 {
                // ask the user for permissions
                ActivityCompat.requestPermissions(MainActivity.this, new String[] {permission}, RP_ACCESS_LOCATION);
                mGeolocationOrigin = origin;
                mGeolocationCallback = callback;
            }
        }
    }
}

并收到结果:

@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) {
                mGeolocationCallback.invoke(mGeolocationOrigin, allow, false);
            }
            break;
    }
}

在你的活动中。

于 2016-09-19T22:59:52.630 回答
1

您可以使用 Google 地图尝试 GreenDroid。

检查出来:https ://github.com/cyrilmottier/GreenDroid

于 2013-05-20T00:35:41.250 回答
0

你必须启用android.permission.ACCESS_FINE_LOCATIONand android.permission.INTERNET,

创建LocationManager实例和LocationListener实例

LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
LocationListener locationListener = new MyLocationListener();  
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, locationListener);

并添加onLocationChanged(Location loc)在其中loc生成经度和纬度的方法(String long = loc.getLongitude(); String lat = loc.getLatitude();

现在用于long, lat生成您的mapPath字符串并继续生成WebView

您可以将其用于参考:http ://www.rdcworld-android.blogspot.in/2012/01/get-current-location-coordinates-city.html

于 2013-05-19T06:47:06.390 回答