Alex Rivera | Logout

android webview geolocation

Asked 2011-03-16T18:02:28.387
113

I have to retrieve a user's location in a WebView. I do this with the following Javascript:

function getLocation() {
   navigator.geolocation.getCurrentPosition(displayLocation, handleError);
}

But the permission request popup never opens.

I've set these settings:

ws.setJavaScriptEnabled(true);
ws.setGeolocationEnabled(true);
ws.setJavaScriptCanOpenWindowsAutomatically(true);

What is the correct way to access a user's location from within a WebView?

Edit
Report

1 Answer

5

This is an example of showing alert dialog to promote for user permission to use his/her location:

    final Context context = this;

     @Override
     public void onGeolocationPermissionsShowPrompt(final String origin, final GeolocationPermissions.Callback callback) {
        Log.i(TAG, "onGeolocationPermissionsShowPrompt()");

        final boolean remember = false;
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle("Locations");
        builder.setMessage("Would like to use your Current Location ")
        .setCancelable(true).setPositiveButton("Allow", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
              // origin, allow, remember
              callback.invoke(origin, true, remember);
           }
        }).setNegativeButton("Don't Allow", new DialogInterface.OnClickListener() {
           public void onClick(DialogInterface dialog, int id) {
              // origin, allow, remember
              callback.invoke(origin, false, remember);
           }
        });
        AlertDialog alert = builder.create();
        alert.show();
     }
answered 2012-04-02T18:01:24.417

Your Answer