如何按城市或​​ bbox 限制 Leaflet-control-geocoder 结果?

How to restrict Leaflet-control-geocoder results by city or bbox?

我试图通过使用 geocodingQueryParamsLeaflet geocoder(使用 Nominatim 提供者)的结果限制在一个城市。但建议和结果仍然适用于全世界。我尝试了几次修改 (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 还允许 countrycity 参数,但这些参数是 structured query parameters,仅在没有查询字符串时使用。以下将 NOT 按预期工作:

var geocoder=L.Control.geocoder({
    geocoder: new L.Control.Geocoder.Nominatim({
        geocodingQueryParams: {
            "country": "FR",
            "city": "Paris"
        }
    })
});