]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions-endpoint.js
Reverse-geocode on endpoint marker drag end
[rails.git] / app / assets / javascripts / index / directions-endpoint.js
1 OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, changeCallback) {
2   var endpoint = {};
3
4   endpoint.marker = L.marker([0, 0], {
5     icon: L.icon({
6       iconUrl: iconUrl,
7       iconSize: [25, 41],
8       iconAnchor: [12, 41],
9       popupAnchor: [1, -34],
10       shadowUrl: OSM.MARKER_SHADOW,
11       shadowSize: [41, 41]
12     }),
13     draggable: true,
14     autoPan: true
15   });
16
17   endpoint.enable = function () {
18     endpoint.marker.on("drag dragend", markerDragListener);
19     input.on("keydown", inputKeydownListener);
20     input.on("change", inputChangeListener);
21   };
22
23   endpoint.disable = function () {
24     endpoint.marker.off("drag dragend", markerDragListener);
25     input.off("keydown", inputKeydownListener);
26     input.off("change", inputChangeListener);
27
28     if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
29     delete endpoint.geocodeRequest;
30     removeLatLng();
31     delete endpoint.value;
32     input.val("");
33     map.removeLayer(endpoint.marker);
34   };
35
36   function markerDragListener(e) {
37     var latlng = convertLatLngToZoomPrecision(e.target.getLatLng());
38
39     if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
40     delete endpoint.geocodeRequest;
41
42     setLatLng(latlng);
43     setInputValueFromLatLng(latlng);
44     endpoint.value = input.val();
45     if (e.type === "dragend") getReverseGeocode();
46     dragCallback(e.type === "drag");
47   }
48
49   function inputKeydownListener() {
50     input.removeClass("is-invalid");
51   }
52
53   function inputChangeListener(e) {
54     // make text the same in both text boxes
55     var value = e.target.value;
56     endpoint.setValue(value);
57   }
58
59   endpoint.setValue = function (value) {
60     endpoint.value = value;
61     removeLatLng();
62     input.removeClass("is-invalid");
63     input.val(value);
64
65     if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
66     delete endpoint.geocodeRequest;
67
68     var coordinatesMatch = value.match(/^\s*([+-]?\d+(?:\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(?:\.\d*)?)\s*$/);
69     var latlng = coordinatesMatch && L.latLng(coordinatesMatch[1], coordinatesMatch[2]);
70
71     if (latlng) {
72       setLatLng(latlng);
73       setInputValueFromLatLng(latlng);
74       getReverseGeocode();
75       changeCallback();
76     } else if (endpoint.value) {
77       getGeocode();
78     }
79   };
80
81   function getGeocode() {
82     var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
83     var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox;
84
85     endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) {
86       delete endpoint.geocodeRequest;
87       if (json.length === 0) {
88         input.addClass("is-invalid");
89         alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
90         return;
91       }
92
93       setLatLng(L.latLng(json[0]));
94
95       input.val(json[0].display_name);
96
97       changeCallback();
98     });
99   }
100
101   function getReverseGeocode() {
102     var reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?lat=" + endpoint.latlng.lat + "&lon=" + endpoint.latlng.lng + "&format=json";
103
104     endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) {
105       delete endpoint.geocodeRequest;
106       if (!json || !json.display_name) {
107         return;
108       }
109
110       endpoint.value = json.display_name;
111       input.val(json.display_name);
112     });
113   }
114
115   function setLatLng(ll) {
116     input
117       .attr("data-lat", ll.lat)
118       .attr("data-lon", ll.lng);
119     endpoint.latlng = ll;
120     endpoint.marker
121       .setLatLng(ll)
122       .addTo(map);
123   }
124
125   function removeLatLng() {
126     input
127       .removeAttr("data-lat")
128       .removeAttr("data-lon");
129     delete endpoint.latlng;
130   }
131
132   function setInputValueFromLatLng(latlng) {
133     input.val(latlng.lat + ", " + latlng.lng);
134   }
135
136   function convertLatLngToZoomPrecision(latlng) {
137     var precision = OSM.zoomPrecision(map.getZoom());
138
139     return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision));
140   }
141
142   return endpoint;
143 };