diff --git a/examples/maps/map_editor/js/example.js b/examples/maps/map_editor/js/example.js index 317c05b3..a6e33a48 100644 --- a/examples/maps/map_editor/js/example.js +++ b/examples/maps/map_editor/js/example.js @@ -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) diff --git a/source/UI/js/Map/Map.js b/source/UI/js/Map/Map.js index aeb633f8..aaa74f36 100644 --- a/source/UI/js/Map/Map.js +++ b/source/UI/js/Map/Map.js @@ -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 };