这个密码对我来说很管用。
当我用服务器上的代码单击“查找我”时,我会得到这个
错误“找不到位置。”
这可能是因为你不使用
https
以及一个不支持地理位置不安全的来源(如Chrome)或你阻止了gelocation的浏览器。
编辑:
要从注释中回答您的问题,下面是用于在页面加载时查找位置的经过调整的代码,而不是单击“查找我”按钮:
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Geolocation</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.mapbox.com/mapbox.js/v3.1.1/mapbox.js'></script>
<link href='https://api.mapbox.com/mapbox.js/v3.1.1/mapbox.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<div id='map'></div>
<a href='#' id='geolocate' class='ui-button'>Find me</a>
<script>
L.mapbox.accessToken = 'pk.eyJ1IjoidGltbGlzdGVuIiwiYSI6ImNqaWs5eWltbTAybG8za21zNjVuZjg5MW4ifQ.xCKtim61H1YXAkU5KT9-FQ';
var map = L.mapbox.map('map', 'mapbox.streets');
var myLayer = L.mapbox.featureLayer().addTo(map);
if (!navigator.geolocation) {
geolocate.innerHTML = 'Geolocation is not available';
} else {
map.locate();
}
map.on('locationfound', function(e) {
map.fitBounds(e.bounds);
myLayer.setGeoJSON({
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [e.latlng.lng, e.latlng.lat]
},
properties: {
'title': 'Here I am!',
'marker-color': '#ff8888',
'marker-symbol': 'star'
}
});
geolocate.parentNode.removeChild(geolocate);
});
map.on('locationerror', function() {
geolocate.innerHTML = 'Position could not be found';
});
</script>
</body>
</html>