我正在尝试找到一个用户,并使用leaflet将地图设置到这个位置:
<script>
var map;
function initMap(){
map = new L.Map('map',{zoomControl : false});
var osmUrl = 'http://{s}.tile.openstreetmap.org/mapnik_tiles/{z}/{x}/{y}.png',
osmAttribution = 'Map data © 2012 OpenStreetMap contributors',
osm = new L.TileLayer(osmUrl, {maxZoom: 18, attribution: osmAttribution});
map.setView(new L.LatLng(51.930156,7.189230), 7).addLayer(osm);
}
function locateUser(){
map.locate({setView : true});
}
</script>在执行浏览器时请求权限,但随后什么也没有发生?我的代码出了什么问题?
发布于 2012-05-18 21:05:38
您的map变量的作用域有问题。我在这里发布了一个修复代码的示例:http://jsfiddle.net/XwbsU/3/
当您点击'Find me!‘时,您应该会收到浏览器地理位置弹出窗口。
希望这对你有帮助。
发布于 2013-08-13 20:39:08
这里有一个快速的技巧。我推荐这个插件https://github.com/domoritz/leaflet-locatecontrol
var loadMap = function (id) {
var HELSINKI = [60.1708, 24.9375];
var map = L.map(id);
var tile_url = 'http://{s}.tile.osm.org/{z}/{x}/{y}.png';
var layer = L.tileLayer(tile_url, {
attribution: 'OSM'
});
map.addLayer(layer);
map.setView(HELSINKI, 19);
map.locate({setView: true, watch: true}) /* This will return map so you can do chaining */
.on('locationfound', function(e){
var marker = L.marker([e.latitude, e.longitude]).bindPopup('Your are here :)');
var circle = L.circle([e.latitude, e.longitude], e.accuracy/2, {
weight: 1,
color: 'blue',
fillColor: '#cacaca',
fillOpacity: 0.2
});
map.addLayer(marker);
map.addLayer(circle);
})
.on('locationerror', function(e){
console.log(e);
alert("Location access denied.");
});
};
loadMap('map');发布于 2021-01-01 01:33:34
或者,您可以将所有代码放在getLocation()函数下,并在加载网页时调用该函数,这样就完成了所有的设置。我使用这种方法,一旦浏览器加载,它就会弹出一个问题来共享数据。
https://stackoverflow.com/questions/10563789
复制相似问题