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 var latlng = convertLatLngToZoomPrecision(e.target.getLatLng());
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 endpoint.value = endpoint.cachedReverseGeocode.value;
70 input.val(endpoint.value);
75 endpoint.value = value;
81 setInputValueFromLatLng(latlng);
84 } else if (endpoint.value) {
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;
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;
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 }));
110 setLatLng(L.latLng(json[0]));
112 endpoint.value = json[0].display_name;
113 input.val(json[0].display_name);
119 function getReverseGeocode() {
120 var latlng = endpoint.latlng.clone();
121 var reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?lat=" + latlng.lat + "&lon=" + latlng.lng + "&format=json";
123 endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) {
124 delete endpoint.geocodeRequest;
125 if (!json || !json.display_name) {
129 endpoint.value = json.display_name;
130 input.val(json.display_name);
131 endpoint.cachedReverseGeocode = { latlng: latlng, value: endpoint.value };
135 function setLatLng(ll) {
137 .attr("data-lat", ll.lat)
138 .attr("data-lon", ll.lng);
139 endpoint.latlng = ll;
145 function removeLatLng() {
147 .removeAttr("data-lat")
148 .removeAttr("data-lon");
149 delete endpoint.latlng;
152 function setInputValueFromLatLng(latlng) {
153 input.val(latlng.lat + ", " + latlng.lng);
156 function convertLatLngToZoomPrecision(latlng) {
157 var precision = OSM.zoomPrecision(map.getZoom());
159 return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision));