Javascript 如何使用传单查找用户?
我正在尝试找到一个用户,并使用传单将地图设置到此位置:Javascript 如何使用传单查找用户?,javascript,leaflet,Javascript,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', osmAttributi
<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>
var映射;
函数initMap(){
map=newl.map('map',{zoomControl:false});
var osmUrl='http://{s}.tile.openstreetmap.org/mapnik_tiles/{z}/{x}/{y}.png',
OSMattAttribute='Map data©;2012 OpenStreetMap贡献者',
osm=新的L.TileLayer(osmUrl,{maxZoom:18,attribute:osmAttribution});
地图。设置视图(新L.LatLng(51.930156,7.189230),7)。添加层(osm);
}
函数locateUser(){
locate({setView:true});
}
在执行浏览器时请求许可,但什么也没发生?我的代码怎么了?您的map变量的范围有问题。我在这里发布了一个修复代码的示例: 单击“查找我!”时,您应该会收到浏览器地理位置弹出窗口
希望这能对您有所帮助。这里有一个快速的解决方法。我推荐这个插件
或者,您可以将所有代码放在getLocation()函数下,并在加载网页时调用该函数,您应该已经全部设置好了。我使用这种方式,一旦浏览器加载它,就会弹出一个问题来共享数据。我们不知道传单是什么,也不知道你们在做什么。这一行代码听起来不错,但你真的需要给我们多一点。也许是到你的代码的链接,以及到目前为止你所尝试的信息。另外,去接受一些答案——你有9个月前的问题,你还没有接受答案!我已经在我的第一篇文章中编辑了整个源代码。传单是一个地图库。这里的细节:你能粘贴你的全部代码吗?您是否包括了所有必需的传单文件?如何检测用户的位置?您的功能正在执行吗?另一点需要注意的是,传单定位功能仅在https网站上有效。您知道我如何在此处添加一些选项,特别是:locateOptions:{maxZoom:15}这是您只需像map一样添加的文档。locate({maxZoom:15,…})传单定位插件是否与Ionic(Android/iOS)一起工作?
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');