Javascript 传单getBounds()返回的经度大于180
我提出了一个问题,作者在Github上关闭了它,但我仍然没有结论。经度范围从-180到180。但有时,传单会从getBounds()返回474.2578215这样的经度,当然在我的数据库中不会返回任何内容 有人告诉我:这是故意的行为。当您将地图放大太远和/或拖动到世界的另一个副本时,会发生这种情况,默认情况下,getBounds经度不会被包裹。您可以使用LatLng wrap方法来获取所需内容,例如bounds.getsoutwest().wrap() 嗯。因此,我在其中添加了wrap方法,并返回了正确的数据,但现在地图上不会显示任何标记。这可能是由于标记位置不在该高数字范围内(传单认为是边界坐标…) 我不确定缩放或拖动是否是问题的原因。刷新页面时问题仍然存在,用户在刷新页面时不会发生缩放或拖动事件。我在init中限制了缩放:minZoom:6,maxZoom:13 我还应该注意,这段代码(未更改)过去工作得很好。这是我的密码:Javascript 传单getBounds()返回的经度大于180,javascript,map,leaflet,Javascript,Map,Leaflet,我提出了一个问题,作者在Github上关闭了它,但我仍然没有结论。经度范围从-180到180。但有时,传单会从getBounds()返回474.2578215这样的经度,当然在我的数据库中不会返回任何内容 有人告诉我:这是故意的行为。当您将地图放大太远和/或拖动到世界的另一个副本时,会发生这种情况,默认情况下,getBounds经度不会被包裹。您可以使用LatLng wrap方法来获取所需内容,例如bounds.getsoutwest().wrap() 嗯。因此,我在其中添加了wrap方法,并返
$(document).ready(function(){ initmap(); });
var map;
var plotlist;
var plotlayers=[];
function initmap(){
// set up the map
map = new L.Map('map');
//get our map
var osmUrl='http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
var osmAttrib='© <a href = "http://www.openstreetmap.org/copyright"> OpenStreetMap </a> contributors';
var osm = new L.TileLayer(osmUrl, {minZoom: 6, maxZoom: 13,
attribution: osmAttrib});
map.setView(new L.LatLng(<?=$slat;?>, <?=$slng;?>),9);
map.attributionControl.setPrefix('');
map.addLayer(osm);
getGameMarkers();
map.on('moveend', onMapMove);
}
function onMapMove(e){
getGameMarkers();
}
function getGameMarkers(){
var center = map.getCenter();
var zoo = map.getZoom();
var bounds = map.getBounds();
console.log(bounds);
var min = bounds.getSouthWest().wrap();
var max = bounds.getNorthEast().wrap();
$.ajax({type: "GET", url: "./ajax/markers.php", dataType: "json", data: "clat="+center.lat+"&clng="+center.lng+"&zoom="+zoo+"&minlat="+min.lat+"&minlng="+min.lng+"&maxlat="+max.lat+"&maxlng="+max.lng+cookiestr,
success: function(data){
if (data.showmap == 1) {
plotlist = data.map_data;
removeMarkers();
for (i=0;i<plotlist.length;i++) {
var iconic = String(plotlist[i].icon);
var plotmark = new L.marker([plotlist[i].lat,plotlist[i].lng], {icon: L.icon({iconUrl: iconic, iconSize: [32, 32]}) }).bindPopup(plotlist[i].html);
map.addLayer(plotmark);
plotlayers.push(plotmark);
}
$("#content").html(data.html);
}else {
$("#map_content").show();
$("#map_content").html(data.main_content);
$("#content").html(data.side_content);
}
}
});
}
控制台中并没有错误,正如我所说的,有时候一切都能正常工作(当getBounds并没有返回一个疯狂的大LON时)。那么我到底做错了什么,更重要的是,我该如何解决它呢 我从未经历过这些不正确的界限,但通过编程限制它们是否至少可以作为临时解决方案 至于没有显示的标记-是否在移动地图时发生?如果是这样,很可能是由“moveend”事件重叠引起的。我不知道平均有多少个点,但如果数据下载未完成并添加标记,下一次调用“moveend”将删除所有现有标记并开始替换它们(同时它将覆盖“plotlist”,这可能会在以后导致错误)。在完成所有操作并绘制标记后,可以尝试将它们添加到全局列表中,也可以仅移动不在新集中的标记
旁注——为什么不使用geojson来创建这些层呢?使用“pointToLayer”和“onEachFeature”,您可以自定义标记/弹出窗口,并在末尾有一个层,而不是一长串标记。这是因为worldCopyJump默认设置为false。将其设置为true,标记将正确显示,因为世界不会重叠 我看到了类似的情况,只是我通过回调处理了一个map click事件,名为
event.latlng.lng
。添加worldCopyJump:true
并没有为我解决这个问题-我仍然看到经度值大于180
最后,我将该方法调用到latlng
对象:
event.latlng.wrap().lng
这解决了问题。
'worldCopyJump':true
对我有用
我可以看到这个
Lat:
液化天然气:
身体{
填充:0;
保证金:0;
}
html,body,#map{
身高:80%;
}
洛雷姆先生{
字体:斜体;
颜色:#AAA;
}
var map=L.map('map',{'worldCopyJump':true});
地图.setView([31.623116,84.497856],8);L.tileLayer('http://{s}.tile.thunderforest.com/cycle/{z}/{x}/{y}.png',{maxZoom:18}).addTo(map);
//var marker=L.marker([31.2,97]).addTo(map);
var theMarker={};
地图上('点击')功能(e){
var coord=e.latlng;
var lat=坐标lat;
地图移除层(市场);
var lng=coord.lng;
标记器=L.标记器([lat,lng])。添加到(地图);
document.getElementById(“LatClick”).innerHTML=lat;
document.getElementById(“LOnClick”).innerHTML=lng;
});
这些边界来自于拖动地图,并通过传单返回。所以我不确定我是否可以通过编程来改变这一点。标记不会在初始化或拖动时显示。我有console.log,它遍历循环并打印出每个标记,所以我知道事件至少正在发生。我曾经使用谷歌地图,所以我一直在尽可能地保持编程与上一个实现类似(以防我需要跳回)。我也不是很精通geojson,所以我不知道如何使用它。
event.latlng.wrap().lng