【发布时间】: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