HTML5 Geolocation not working in GWT JSNI

91 Views Asked by At

I have used the navigator.geolocation() in the GWT JSNI(javascript native interface) as below and it worked well before, but it does not recently.

private native void getGeolocation()/*-{
// Using HTML5 geolocation.
    if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(function(position) {
        var pos = {
            lat: position.coords.latitude,
            lng: position.coords.longitude
        };
    console.log('Pos: ' + pos);
    }, function() {
      console.log('error geolocation service');
    });
} else {
    // Browser doesn't support Geolocation
    console.log('geolocation not support');
}
}-*/;

I will get the 'error geolocation service' log without the browser asked me for the location permission. However, if I executed the part of the JS code in the console directly, the browser will then ask for the location permission.

It seems that the navigator.geolocation does not work in the JSNI. Or did I miss anything there?

Thanks

1

There are 1 best solutions below

0
Adam On

You can try to use GTW's Geolocation. It is still experimental API but I think it's better than using JSNI.

Experimental API: This API is still under development and is subject to change.

This may not be supported on all browsers.

Geolocation geolocation = Geolocation.getIfSupported();
if(geolocation != null) {
    geolocation.getCurrentPosition(new Callback<Position, PositionError>() {
        @Override
        public void onSuccess(Position result) {
            if(result.getCoordinates() != null) {
                Window.alert("Latitude: " + Double.toString(result.getCoordinates().getLatitude()));
                Window.alert("Longitude: " + Double.toString(result.getCoordinates().getLongitude()));
            }
            else
                Window.alert("No coordinates available");
        }
        
        @Override
        public void onFailure(PositionError reason) {
            Window.alert(reason.getMessage());
        }
    });
}