【问题标题】:HTML5 Geolocation not working in GWT JSNIHTML5 地理位置在 GWT JSNI 中不起作用
【发布时间】:2019-10-23 13:27:51
【问题描述】:

我在 GWT JSNI(javascript 本机接口)中使用了 navigator.geolocation(),如下所示,以前效果很好,但最近不行。

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');
}
}-*/;

在浏览器未要求我提供位置权限的情况下,我将获得“错误地理定位服务”日志。但是,如果我直接在控制台执行这部分JS代码,那么浏览器会要求位置权限。

似乎 navigator.geolocation 在 JSNI 中不起作用。还是我错过了什么?

谢谢

【问题讨论】:

  • 您使用的是 https 吗?在大多数浏览器中,位置在 http 中不起作用。
  • 你试过$wnd.navigator.geolocation吗? (和$wnd.console顺便说一句)

标签: html gwt geolocation navigator jsni


【解决方案1】:

您可以尝试使用 GTW 的Geolocation。它仍然是实验性 API,但我认为它比使用 JSNI 更好。

实验性 API:此 API 仍在开发中,可能会发生变化。

并非所有浏览器都支持此功能。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2012-08-04
    相关资源
    最近更新 更多