]> git.openstreetmap.org Git - rails.git/blob - app/assets/javascripts/index/directions-endpoint.js
Swap cached reverse geocodes when reversing directions
[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     if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
61     delete endpoint.geocodeRequest;
62     input.removeClass("is-invalid");
63
64     var coordinatesMatch = value.match(/^\s*([+-]?\d+(?:\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(?:\.\d*)?)\s*$/);
65     var latlng = coordinatesMatch && L.latLng(coordinatesMatch[1], coordinatesMatch[2]);
66
67     if (latlng && endpoint.cachedReverseGeocode && endpoint.cachedReverseGeocode.latlng.equals(latlng)) {
68       setLatLng(latlng);
69       endpoint.value = endpoint.cachedReverseGeocode.value;
70       input.val(endpoint.value);
71       changeCallback();
72       return;
73     }
74
75     endpoint.value = value;
76     removeLatLng();
77     input.val(value);
78
79     if (latlng) {
80       setLatLng(latlng);
81       setInputValueFromLatLng(latlng);
82       getReverseGeocode();
83       changeCallback();
84     } else if (endpoint.value) {
85       getGeocode();
86     }
87   };
88
89   endpoint.swapCachedReverseGeocodes = function (otherEndpoint) {
90     var g0 = endpoint.cachedReverseGeocode;
91     var g1 = otherEndpoint.cachedReverseGeocode;
92     delete endpoint.cachedReverseGeocode;
93     delete otherEndpoint.cachedReverseGeocode;
94     if (g0) otherEndpoint.cachedReverseGeocode = g0;
95     if (g1) endpoint.cachedReverseGeocode = g1;
96   };
97
98   function getGeocode() {
99     var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
100     var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox;
101
102     endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) {
103       delete endpoint.geocodeRequest;
104       if (json.length === 0) {
105         input.addClass("is-invalid");
106         alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
107         return;
108       }
109
110       setLatLng(L.latLng(json[0]));
111
112       endpoint.value = json[0].display_name;
113       input.val(json[0].display_name);
114
115       changeCallback();
116     });
117   }
118
119   function getReverseGeocode() {
120     var latlng = endpoint.latlng.clone();
121     var reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?lat=" + latlng.lat + "&lon=" + latlng.lng + "&format=json";
122
123     endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) {
124       delete endpoint.geocodeRequest;
125       if (!json || !json.display_name) {
126         return;
127       }
128
129       endpoint.value = json.display_name;
130       input.val(json.display_name);
131       endpoint.cachedReverseGeocode = { latlng: latlng, value: endpoint.value };
132     });
133   }
134
135   function setLatLng(ll) {
136     input
137       .attr("data-lat", ll.lat)
138       .attr("data-lon", ll.lng);
139     endpoint.latlng = ll;
140     endpoint.marker
141       .setLatLng(ll)
142       .addTo(map);
143   }
144
145   function removeLatLng() {
146     input
147       .removeAttr("data-lat")
148       .removeAttr("data-lon");
149     delete endpoint.latlng;
150   }
151
152   function setInputValueFromLatLng(latlng) {
153     input.val(latlng.lat + ", " + latlng.lng);
154   }
155
156   function convertLatLngToZoomPrecision(latlng) {
157     var precision = OSM.zoomPrecision(map.getZoom());
158
159     return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision));
160   }
161
162   return endpoint;
163 };