Skip to content

Commit 79a2a16

Browse files
committed
fix: ricerca raggio modulo Mappa
1 parent 9abf36a commit 79a2a16

1 file changed

Lines changed: 65 additions & 8 deletions

File tree

modules/mappa/edit.php

Lines changed: 65 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,30 @@ function caricaMappa() {
153153
L.control.layers(baseLayers).addTo(map);
154154
}
155155

156+
function calculateZoomForRadius(radius) {
157+
if (radius >= 50000) return 4;
158+
if (radius >= 25000) return 5;
159+
if (radius >= 10000) return 6;
160+
if (radius >= 5000) return 7;
161+
if (radius >= 2500) return 8;
162+
if (radius >= 1000) return 9;
163+
if (radius >= 500) return 10;
164+
if (radius >= 250) return 11;
165+
if (radius >= 100) return 12;
166+
return 13;
167+
}
168+
169+
function updateMapViewWithCircle(latlng, radius) {
170+
setTimeout(function() {
171+
if (circle) {
172+
map.fitBounds(circle.getBounds(), {
173+
padding: [-10, -10],
174+
maxZoom: 18
175+
});
176+
}
177+
}, 100);
178+
}
179+
156180
function initGeocomplete() {
157181
$.ajax({
158182
url: "https://nominatim.openstreetmap.org/search.php?q=" + encodeURI(input("gaddress").get()) + "&format=jsonv2",
@@ -164,33 +188,66 @@ function initGeocomplete() {
164188
input("gaddress").set(data[0].display_name);
165189

166190
var latlng = L.latLng(data[0].lat, data[0].lon);
167-
map.setView(latlng, 16);
191+
var radius = $("#range").val().toEnglish();
168192

169193
L.marker(latlng).addTo(map)
170194
.bindPopup("You are here").openPopup();
171195

172-
// Aggiungi cerchio per indicare l'accuratezza
196+
173197
if (circle) {
174198
map.removeLayer(circle);
175199
}
176200

177201
circle = L.circle(latlng, {
178-
radius: $("#range").val().toEnglish()
202+
radius: radius
179203
}).addTo(map);
180204

205+
updateMapViewWithCircle(latlng, radius);
206+
181207
reload_pointers();
182208
}
183209
});
184210
}
185211

186-
// Avvio ricerca indirizzo premendo Invio
212+
187213
$("#gaddress, #range").on("keypress", function(e){
188214
if(e.which == 13){
189215
e.preventDefault();
190216
initGeocomplete();
191217
}
192218
});
193219

220+
$("#gaddress").on("blur", function(){
221+
if($("#gaddress").val().trim() !== ""){
222+
initGeocomplete();
223+
}
224+
});
225+
226+
$("#range").on("blur input", function(){
227+
updateCircleRadius();
228+
});
229+
230+
function updateCircleRadius() {
231+
if (circle && $("#lat").val() && $("#lng").val()) {
232+
var lat = $("#lat").val();
233+
var lng = $("#lng").val();
234+
var radius = $("#range").val().toEnglish();
235+
var latlng = L.latLng(lat, lng);
236+
237+
map.removeLayer(circle);
238+
239+
circle = L.circle(latlng, {
240+
radius: radius
241+
}).addTo(map);
242+
243+
updateMapViewWithCircle(latlng, radius);
244+
245+
if (typeof reload_pointers === 'function') {
246+
reload_pointers();
247+
}
248+
}
249+
}
250+
194251
// Funzione per ottenere e visualizzare la geolocalizzazione
195252
function getLocation() {
196253
if (navigator.geolocation) {
@@ -205,7 +262,7 @@ function onLocationFound(position) {
205262
var lng = position.coords.longitude;
206263

207264
var latlng = L.latLng(lat, lng);
208-
map.setView(latlng, 16);
265+
var radius = $("#range").val().toEnglish();
209266

210267
if (circle) {
211268
map.removeLayer(circle);
@@ -214,12 +271,12 @@ function onLocationFound(position) {
214271
L.marker(latlng).addTo(map)
215272
.bindPopup("You are here").openPopup();
216273

217-
// Aggiungi cerchio per indicare l'accuratezza
218274
circle = L.circle(latlng, {
219-
//radius: position.coords.accuracy
220-
radius: $("#range").val().toEnglish()
275+
radius: radius
221276
}).addTo(map);
222277

278+
updateMapViewWithCircle(latlng, radius);
279+
223280
// Invia richiesta per ottenere l'indirizzo
224281
$.getJSON('https://nominatim.openstreetmap.org/reverse', {
225282
lat: lat,

0 commit comments

Comments
 (0)