Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Embedded Google Map can't get current location in WebView

I followed this tutorial: http://android-er.blogspot.com/2013/03/embed-google-map-in-webview.html

I'm trying to just use the Google Map in the WebView, but it can't get my current location. I've enabled JavaScript on the WebView. What else do I have to enable?

Does anyone know why that might be? Shouldn't it prompt me to use my current location?

Note that I am not interested in using a MapView as an alternative whatsoever. I'm trying to find out what I need to set on the WebView or maybe on the device's location services?

like image 563
user5243421 Avatar asked May 10 '13 15:05

user5243421


3 Answers

You should permit the web view to access your location by overriding the method onGeolocationPermissionsShowPrompt like this:

webView.setWebChromeClient(new WebChromeClient(){

        @Override
        public void onGeolocationPermissionsShowPrompt(String origin, GeolocationPermissions.Callback callback) {
            callback.invoke(origin, true, false);
        }
    });
like image 157
Kumait Avatar answered Oct 21 '22 23:10

Kumait


On API 5.x and below, you will need

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

in your AndroidManifest.xml.

But to allow permissions for geolocation on API 6.0+, you have to request the permission at runtime.

Request location permission

To do this, use

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

and receive the result:

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

in your activity.

like image 39
EricRobertBrewer Avatar answered Oct 21 '22 23:10

EricRobertBrewer


You can try GreenDroid with Google Maps.

Checkt it out: https://github.com/cyrilmottier/GreenDroid

like image 1
agustinm20 Avatar answered Oct 22 '22 00:10

agustinm20