pick nominatim result as per zoom level, sending current zoom as part of nominatim request

This commit is contained in:
Sanjay Bhangar 2025-08-11 18:14:51 +02:00
commit 905258c962
2 changed files with 25 additions and 24 deletions

View file

@ -10,6 +10,7 @@ Ox.load(['UI', 'Geo'], function() {
var $map = Ox.MapEditor({
addPlace: function(place, callback) {
place.id = Ox.encodeBase26((places.length ? Ox.max(places.map(p => Ox.decodeBase26(p.id))) : 0) + 1)
place.editable = true;
console.log("addPlace", place.id, place)
places.push(place)
$storage("places", places)

View file

@ -767,34 +767,24 @@ Ox.Map = function(options, self) {
}
async function getPlaceByLatLng(latlng, bounds, callback) {
// gets the largest place at latlng that would fit in bounds
// gets the place at latlng appropriate for current zoom level
var callback = arguments.length == 3 ? callback : bounds,
bounds = arguments.length == 3 ? bounds : null;
self.$loadingIcon && self.$loadingIcon.start();
var results = await reverseGeocode(latlng);
self.$loadingIcon && self.$loadingIcon.stop();
if (results.features.length) {
if (bounds) {
Ox.forEach(results.features, function(result, i) {
if (
i == results.length - 1 ||
canContain(bounds, result.bounds)
) {
callback(new Ox.MapPlace(parseGeodata(result)));
return false; // break
}
});
} else {
callback(new Ox.MapPlace(parseGeodata(results.features[0])));
}
}
if (!results.length) {
// We only have one result based on current zoom level
var feature = results.features[0];
callback(new Ox.MapPlace(parseGeodata(feature)));
triggerGeocodeEvent({
latLng: latlng,
results: results
results: results.features
});
} else {
Ox.Log('Map', 'geocode failed:', status);
Ox.Log('Map', 'geocode failed: no results');
callback(null);
}
}
@ -895,20 +885,30 @@ Ox.Map = function(options, self) {
async function reverseGeocode(config) {
const features = [];
// Use current map zoom level directly as Nominatim zoom level
const currentMapZoom = Math.round(self.map.getZoom());
console.log('Reverse geocoding: using map zoom =', currentMapZoom, 'for Nominatim zoom');
try {
const request = `${self.options.nominatim}/reverse?lat=${
config.lat
}&lon=${
config.lng
}&format=geojson&polygon_geojson=1&addressdetails=1`;
config.lat
}&lon=${
config.lng
}&format=geojson&polygon_geojson=1&addressdetails=1&zoom=${currentMapZoom}&extratags=1`;
const response = await fetch(request);
const geojson = await response.json();
for (const feature of geojson.features) {
features.push(converNominatimFeature(feature));
if (geojson.features && geojson.features.length > 0) {
const feature = converNominatimFeature(geojson.features[0]);
feature.zoom = currentMapZoom;
features.push(feature);
}
} catch (e) {
console.error(`Failed to reverseGeocode with error: ${e}`);
}
return {
features
};