X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/a1051b418e7a838e5dc14f8491f97de5510d1437..6c779d1ca5bd0b68a05f3c3dc593fcb2af97a222:/app/assets/javascripts/index/directions-endpoint.js diff --git a/app/assets/javascripts/index/directions-endpoint.js b/app/assets/javascripts/index/directions-endpoint.js index 6cb94f39f..11a70c62f 100644 --- a/app/assets/javascripts/index/directions-endpoint.js +++ b/app/assets/javascripts/index/directions-endpoint.js @@ -34,7 +34,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch }; function markerDragListener(e) { - var latlng = convertLatLngToZoomPrecision(e.target.getLatLng()); + const latlng = L.latLng(OSM.cropLocation(e.target.getLatLng(), map.getZoom())); if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort(); delete endpoint.geocodeRequest; @@ -66,7 +66,12 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch if (latlng && endpoint.cachedReverseGeocode && endpoint.cachedReverseGeocode.latlng.equals(latlng)) { setLatLng(latlng); - endpoint.value = endpoint.cachedReverseGeocode.value; + if (endpoint.cachedReverseGeocode.notFound) { + endpoint.value = value; + input.addClass("is-invalid"); + } else { + endpoint.value = endpoint.cachedReverseGeocode.value; + } input.val(endpoint.value); changeCallback(); return; @@ -96,13 +101,14 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch }; function getGeocode() { - var viewbox = map.getBounds().toBBoxString(); // ,,, - var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox; + const viewbox = map.getBounds().toBBoxString(), // ,,, + geocodeUrl = OSM.NOMINATIM_URL + "search?" + new URLSearchParams({ q: endpoint.value, format: "json", viewbox }); endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) { delete endpoint.geocodeRequest; if (json.length === 0) { input.addClass("is-invalid"); + // eslint-disable-next-line no-alert alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value })); return; } @@ -117,12 +123,14 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch } function getReverseGeocode() { - var latlng = endpoint.latlng.clone(); - var reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?lat=" + latlng.lat + "&lon=" + latlng.lng + "&format=json"; + const latlng = endpoint.latlng.clone(), + { lat, lng } = latlng, + reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?" + new URLSearchParams({ lat, lon: lng, format: "json" }); endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) { delete endpoint.geocodeRequest; if (!json || !json.display_name) { + endpoint.cachedReverseGeocode = { latlng: latlng, notFound: true }; return; } @@ -153,11 +161,5 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch input.val(latlng.lat + ", " + latlng.lng); } - function convertLatLngToZoomPrecision(latlng) { - var precision = OSM.zoomPrecision(map.getZoom()); - - return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision)); - } - return endpoint; };