简体   繁体   English

根据地图缩放级别在传单地图上设置边界

[英]set Bounds on Leaflet map according to map zoom level

I am working on a forked OSM/leaflet/overpass API application which has its own max bounds declared.我正在开发一个分叉的 OSM/leaflet/overpass API 应用程序,它有自己的最大边界声明。 That way, any request to Overpass API is restricted to bounds declared这样,对 Overpass API 的任何请求都仅限于声明的边界

var mapBounds = { south: 50.8025, west: 0.3724, north: 50.8785, east: 0.5290 };
var LBounds = L.latLngBounds([mapBounds.south, mapBounds.west], [mapBounds.north, mapBounds.east]);

The query to Overpass API is done对 Overpass API 的查询完成

...
    else queryBbox += '[bbox:' + [mapBounds.south, mapBounds.west, mapBounds.north, mapBounds.east].join(',') + ']';
...

Need to change the bounding box (mapBounds) to allow to navigate worldwide, and set them as bbox, so requests to Overpass API are for that map area only.需要更改边界框 (mapBounds) 以允许在全球范围内导航,并将它们设置为 bbox,因此对 Overpass API 的请求仅针对该地图区域。

so i've done所以我已经完成了

else queryBbox += '[bbox:' + map.getBounds().toBBoxString() + ']';

But I can't make it work .. Overpass returns no results但我不能让它工作.. Overpass 没有返回结果

addition:添加:

meanwhile i got closer, i tried to see what happens onMoveEnd与此同时,我离得更近了,我试着看看在MoveEnd上会发生什么

onMoveEnd: function () {
    console.log(queryBbox);

console record seems to return coordinates:控制台记录似乎返回坐标:

[out:json][bbox:0.38512229919433594,50.82990293001779,0.549917221069336,50.86404449323755];(nwr[tourism=museum];);out tags center qt 250;

But they are reversed但是它们反过来了

for comparison: this is how the original query looks like用于比较:这是原始查询的样子

[out:json][bbox:50.8025,0.3724,50.8785,0.529];(nwr[tourism=museum];);out tags center qt 250;

Your url is wrong, it should look like that: https://overpass-api.de/api/interpreter?data=[out:json];out%20skel%20qt;node[tourism=attraction];&bbox=0.427393913269043,50.84410451978967,0.5097913742065431,50.85710981721644 ;你的网址是错误的,应该是这样的: https : //overpass-api.de/api/interpreter?data=[out : json]; out%20skel%20qt;node[tourism=attraction]; &bbox=0.427393913269043, 50.84410451978967,0.5097913742065431,50.85710981721644

Do you build the url by your self or over a library?您是自己构建 url 还是通过库构建 url? If you use a library, look into if you have defined some wrong.如果您使用库,请查看您是否定义了错误。

To create a own request create the url and then make a http request:要创建自己的请求,请创建 url,然后发出 http 请求:

function getOverpassData(){
    var bbox = map.getBounds().toBBoxString();
    var _url = "https://overpass-api.de/api/interpreter?data=[out:json];out%20skel%20qt;node[tourism=attraction];";

    var bboxurl = _url+"&bbox="+bbox;
    console.log(bboxurl);
    // And then use $.ajax(bboxurl) or the normal js http request to get data.
}

i was able to fix my issue while reading (closer) to Leaflet's documentation.在阅读(更接近)传单的文档时,我能够解决我的问题。

toBBoxString() Returns a string with bounding box coordinates in a 'southwest_lng,southwest_lat,northeast_lng,northeast_lat' format. toBBoxString() 以“southwest_lng,southwest_lat,northeast_lng,northeast_lat”格式返回带有边界框坐标的字符串。 Useful for sending requests to web services that return geo data.用于向返回地理数据的 Web 服务发送请求。

https://leafletjs.com/reference-1.6.0.html#latlngbounds-tobboxstring https://leafletjs.com/reference-1.6.0.html#latlngbounds-tobboxstring

But the fact is, that Overpass API does not return responses in GeoJSON format ... so my hack with toBBoxString() was not suitable , aka that's why i got false responses.但事实是,Overpass API 不会以 GeoJSON 格式返回响应......所以我对 toBBoxString() 的 hack 不合适,也就是我得到错误响应的原因。 Instead, this one worked fine, all requests are now successful相反,这个工作正常,所有请求现在都成功了

else queryBbox += '[bbox:' + map.getBounds().getSouth() +',' + map.getBounds().getWest() +',' + map.getBounds().getNorth() +',' + map.getBounds().getEast() + ']';

I hope that someone might have a more elegant solution before i vote for mine我希望在我投票给我之前有人可能有更优雅的解决方案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM