]> git.openstreetmap.org Git - rails.git/blobdiff - app/assets/javascripts/index/directions-endpoint.js
Reverse-geocode on endpoint marker drag end
[rails.git] / app / assets / javascripts / index / directions-endpoint.js
index b475b9a05e728904b14b1b50bd03e6fe60a15cf8..604dcc2b6c6b8ed3a0751531dec9a0f452be92ed 100644 (file)
@@ -42,6 +42,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
     setLatLng(latlng);
     setInputValueFromLatLng(latlng);
     endpoint.value = input.val();
+    if (e.type === "dragend") getReverseGeocode();
     dragCallback(e.type === "drag");
   }
 
@@ -70,6 +71,7 @@ OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, ch
     if (latlng) {
       setLatLng(latlng);
       setInputValueFromLatLng(latlng);
+      getReverseGeocode();
       changeCallback();
     } else if (endpoint.value) {
       getGeocode();
@@ -96,6 +98,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)