X-Git-Url: https://git.openstreetmap.org./rails.git/blobdiff_plain/d9b09046f2245a201c462dee382ecb940c0a5293..ce42b685b2084fd34270756c88db3d9ff401c2be:/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 59f0b929d..b3d03cdca 100644 --- a/app/assets/javascripts/index/directions-endpoint.js +++ b/app/assets/javascripts/index/directions-endpoint.js @@ -55,7 +55,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch endpoint.setValue(value); } - endpoint.setValue = function (value, latlng) { + endpoint.setValue = function (value) { endpoint.value = value; removeLatLng(); input.removeClass("is-invalid"); @@ -64,9 +64,13 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort(); delete endpoint.geocodeRequest; + var coordinatesMatch = value.match(/^\s*([+-]?\d+(?:\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(?:\.\d*)?)\s*$/); + var latlng = coordinatesMatch && L.latLng(coordinatesMatch[1], coordinatesMatch[2]); + if (latlng) { setLatLng(latlng); setInputValueFromLatLng(latlng); + getReverseGeocode(); changeCallback(); } else if (endpoint.value) { getGeocode(); @@ -93,6 +97,20 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch }); } + function getReverseGeocode() { + var reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?lat=" + endpoint.latlng.lat + "&lon=" + endpoint.latlng.lng + "&format=json"; + + endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) { + delete endpoint.geocodeRequest; + if (!json || !json.display_name) { + return; + } + + endpoint.value = json.display_name; + input.val(json.display_name); + }); + } + function setLatLng(ll) { input .attr("data-lat", ll.lat)