我正在尝试限制 Leaflet geocoder 的结果(使用 Nominatim 提供程序)通过使用 geocodingQueryParams
到一个城市。但建议和结果仍然适用于全世界。我尝试了几种修改(geocodingQueryParams:'liberec'
/geocodingQueryParams:'city=liberec'
/geocodingQueryParams:'q=liberec'
)但没有成功。你有什么建议吗?或者是否可以在这个插件中通过 bbox 进行过滤?
我的代码:
var geocoder=L.Control.geocoder({
placeholder: 'Hledej...',
errorMessage: 'Nenašli jsme :(',
defaultMarkGeocode: false,
geocoder: new L.Control.Geocoder.Nominatim({
geocodingQueryParams:'liberec'
})
})
.on('markgeocode', function(e) {
var bbox = e.geocode.bbox;
var poly = L.polygon([
bbox.getSouthEast(),
bbox.getNorthEast(),
bbox.getNorthWest(),
bbox.getSouthWest()
]);
map.fitBounds(poly.getBounds());
}).addTo(map);
我使用的是 Leaflet 版本 1.5.1。
最佳答案
让我引用 Nominatim's documentation :
Result limitation
[...]
viewbox=<x1>,<y1>,<x2>,<y2>
The preferred area to find search results. Any two corner points of the box are accepted in any order as long as they span a real box.
另外,请注意 the geocodingQueryParams
option from Leaflet-control-geocoder's Nominatim provider需要 Object
, 不是 String
; URL查询参数通常以一组键值对的形式给出,参数名作为键,参数值作为值。
因此,你可以这样做:
var geocoder=L.Control.geocoder({
geocoder: new L.Control.Geocoder.Nominatim({
geocodingQueryParams: {
"viewbox": "14.07,49.02,24.02,54.85"
}
})
});
如果你有一个 L.LatLngBounds
的实例而不是 <x1>,<y1>,<x2>,<y2>
字符串,你可以利用 toBBoxString()
:
var bbox = L.latLngBounds(/* stuff */);
var geocoder=L.Control.geocoder({
geocoder: new L.Control.Geocoder.Nominatim({
geocodingQueryParams: {
"viewbox": bbox.toBBoxString()
}
})
});
Nominatim 还允许 country
和 city
参数,但那些是 structured query parameters , 仅在没有查询字符串时使用。以下将不按预期工作:
var geocoder=L.Control.geocoder({
geocoder: new L.Control.Geocoder.Nominatim({
geocodingQueryParams: {
"country": "FR",
"city": "Paris"
}
})
});
关于javascript - 如何按城市或 bbox 限制 Leaflet-control-geocoder 结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57286626/