【问题标题】:Geolocation API is not working on my mobile phone but on desktop doesGeolocation API 在我的手机上不起作用,但在桌面上可以
【发布时间】:2021-05-02 16:39:55
【问题描述】:

我使用 OpenStreetMap 开发了一张地图,应该可以定位我的当前位置。 在台式机上一切正常,但是当我切换到手机时,他没有要求 GPS 权限并且它不起作用。

创建地图后(在手机上显示错误):

function init() {
    createMap('map');
    navigator.geolocation.watchPosition(success, error, options);
}

function createMap(id) {
    map = new OpenLayers.Map(id);
    map.addLayer(new OpenLayers.Layer.OSM());
}

function success(position) {
    const latitude = position.coords.latitude;
    const longitude = position.coords.longitude;

    console.log(latitude + ', ' + longitude);

    addMarker(latitude, longitude);
    setCenter(latitude, longitude);
}

function setCenter(latitude, longitude) {
    map.setCenter(
        new OpenLayers.LonLat(longitude, latitude).transform(
            new OpenLayers.Projection("EPSG:4326"),
            map.getProjectionObject())

        , 18
    );
}

function addMarker(latitude, longitude) {
    var markers = new OpenLayers.Layer.Markers("Markers");
    map.addLayer(markers);
    markers.addMarker(new OpenLayers.Marker(
        new OpenLayers.LonLat(longitude, latitude).transform(
            new OpenLayers.Projection("EPSG:4326"),
            map.getProjectionObject())
    ));
}

function error() {
    alert(`ERROR(${error.code}): ${error.message}`);
}

【问题讨论】:

  • 哪个桌面,哪个手机?错误说明了什么?

标签: javascript android api geolocation gps


【解决方案1】:

已解决,Geolocation 仅适用于 HTTPS,我正在通过手机作为 HTTP 站点访问该站点

【讨论】:

    猜你喜欢
    • 2018-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-03
    • 1970-01-01
    相关资源
    最近更新 更多