繁体   English   中英

地理定位 API 在我的手机上无法使用,但在台式机上可以

[英]Geolocation API is not working on my mobile phone but on desktop does

我使用 OpenStreetMap 开发了 map,它应该可以找到我当前的 position。 在桌面上一切正常,但是当我切换到手机时,他没有要求 GPS 权限并且它不起作用。

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

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

已解决,Geolocation 仅适用于 HTTPS 并且我从手机作为 HTTP 站点访问该站点

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM