1 OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, changeCallback) {
4 endpoint.marker = L.marker([0, 0], {
10 shadowUrl: OSM.MARKER_SHADOW,
17 endpoint.enable = function () {
18 endpoint.marker.on("drag dragend", markerDragListener);
19 input.on("keydown", inputKeydownListener);
20 input.on("change", inputChangeListener);
23 endpoint.disable = function () {
24 endpoint.marker.off("drag dragend", markerDragListener);
25 input.off("keydown", inputKeydownListener);
26 input.off("change", inputChangeListener);
28 if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
29 delete endpoint.geocodeRequest;
31 delete endpoint.value;
33 map.removeLayer(endpoint.marker);
36 function markerDragListener(e) {
37 const latlng = L.latLng(OSM.cropLocation(e.target.getLatLng(), map.getZoom()));
39 if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
40 delete endpoint.geocodeRequest;
43 setInputValueFromLatLng(latlng);
44 endpoint.value = input.val();
45 if (e.type === "dragend") getReverseGeocode();
46 dragCallback(e.type === "drag");
49 function inputKeydownListener() {
50 input.removeClass("is-invalid");
53 function inputChangeListener(e) {
54 // make text the same in both text boxes
55 var value = e.target.value;
56 endpoint.setValue(value);
59 endpoint.setValue = function (value) {
60 if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
61 delete endpoint.geocodeRequest;
62 input.removeClass("is-invalid");
64 var coordinatesMatch = value.match(/^\s*([+-]?\d+(?:\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(?:\.\d*)?)\s*$/);
65 var latlng = coordinatesMatch && L.latLng(coordinatesMatch[1], coordinatesMatch[2]);
67 if (latlng && endpoint.cachedReverseGeocode && endpoint.cachedReverseGeocode.latlng.equals(latlng)) {
69 if (endpoint.cachedReverseGeocode.notFound) {
70 endpoint.value = value;
71 input.addClass("is-invalid");
73 endpoint.value = endpoint.cachedReverseGeocode.value;
75 input.val(endpoint.value);
80 endpoint.value = value;
86 setInputValueFromLatLng(latlng);
89 } else if (endpoint.value) {
94 endpoint.swapCachedReverseGeocodes = function (otherEndpoint) {
95 var g0 = endpoint.cachedReverseGeocode;
96 var g1 = otherEndpoint.cachedReverseGeocode;
97 delete endpoint.cachedReverseGeocode;
98 delete otherEndpoint.cachedReverseGeocode;
99 if (g0) otherEndpoint.cachedReverseGeocode = g0;
100 if (g1) endpoint.cachedReverseGeocode = g1;
103 function getGeocode() {
104 var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
105 var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox;
107 endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) {
108 delete endpoint.geocodeRequest;
109 if (json.length === 0) {
110 input.addClass("is-invalid");
111 alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
115 setLatLng(L.latLng(json[0]));
117 endpoint.value = json[0].display_name;
118 input.val(json[0].display_name);
124 function getReverseGeocode() {
125 var latlng = endpoint.latlng.clone();
126 var reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?lat=" + latlng.lat + "&lon=" + latlng.lng + "&format=json";
128 endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) {
129 delete endpoint.geocodeRequest;
130 if (!json || !json.display_name) {
131 endpoint.cachedReverseGeocode = { latlng: latlng, notFound: true };
135 endpoint.value = json.display_name;
136 input.val(json.display_name);
137 endpoint.cachedReverseGeocode = { latlng: latlng, value: endpoint.value };
141 function setLatLng(ll) {
143 .attr("data-lat", ll.lat)
144 .attr("data-lon", ll.lng);
145 endpoint.latlng = ll;
151 function removeLatLng() {
153 .removeAttr("data-lat")
154 .removeAttr("data-lon");
155 delete endpoint.latlng;
158 function setInputValueFromLatLng(latlng) {
159 input.val(latlng.lat + ", " + latlng.lng);