Compare commits

...

2 commits

2 changed files with 25 additions and 25 deletions

View file

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

View file

@ -767,34 +767,24 @@ Ox.Map = function(options, self) {
} }
async function getPlaceByLatLng(latlng, bounds, callback) { 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, var callback = arguments.length == 3 ? callback : bounds,
bounds = arguments.length == 3 ? bounds : null; bounds = arguments.length == 3 ? bounds : null;
self.$loadingIcon && self.$loadingIcon.start(); self.$loadingIcon && self.$loadingIcon.start();
var results = await reverseGeocode(latlng); var results = await reverseGeocode(latlng);
self.$loadingIcon && self.$loadingIcon.stop(); self.$loadingIcon && self.$loadingIcon.stop();
if (results.features.length) { if (results.features.length) {
if (bounds) { // We only have one result based on current zoom level
Ox.forEach(results.features, function(result, i) { var feature = results.features[0];
if ( callback(new Ox.MapPlace(parseGeodata(feature)));
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) {
triggerGeocodeEvent({ triggerGeocodeEvent({
latLng: latlng, latLng: latlng,
results: results results: results.features
}); });
} else { } else {
Ox.Log('Map', 'geocode failed:', status); Ox.Log('Map', 'geocode failed: no results');
callback(null); callback(null);
} }
} }
@ -895,20 +885,30 @@ Ox.Map = function(options, self) {
async function reverseGeocode(config) { async function reverseGeocode(config) {
const features = []; 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 { try {
const request = `${self.options.nominatim}/reverse?lat=${ const request = `${self.options.nominatim}/reverse?lat=${
config.lat config.lat
}&lon=${ }&lon=${
config.lng config.lng
}&format=geojson&polygon_geojson=1&addressdetails=1`; }&format=geojson&polygon_geojson=1&addressdetails=1&zoom=${currentMapZoom}&extratags=1`;
const response = await fetch(request); const response = await fetch(request);
const geojson = await response.json(); 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) { } catch (e) {
console.error(`Failed to reverseGeocode with error: ${e}`); console.error(`Failed to reverseGeocode with error: ${e}`);
} }
return { return {
features features
}; };