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 function getGeocode() {
90 var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
91 var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox;
93 endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) {
94 delete endpoint.geocodeRequest;
95 if (json.length === 0) {
96 input.addClass("is-invalid");
97 alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
101 setLatLng(L.latLng(json[0]));
103 endpoint.value = json[0].display_name;
104 input.val(json[0].display_name);
110 function getReverseGeocode() {
111 var latlng = endpoint.latlng.clone();
112 var reverseGeocodeUrl = OSM.NOMINATIM_URL + "reverse?lat=" + latlng.lat + "&lon=" + latlng.lng + "&format=json";
114 endpoint.geocodeRequest = $.getJSON(reverseGeocodeUrl, function (json) {
115 delete endpoint.geocodeRequest;
116 if (!json || !json.display_name) {
120 endpoint.value = json.display_name;
121 input.val(json.display_name);
122 endpoint.cachedReverseGeocode = { latlng: latlng, value: endpoint.value };
126 function setLatLng(ll) {
128 .attr("data-lat", ll.lat)
129 .attr("data-lon", ll.lng);
130 endpoint.latlng = ll;
136 function removeLatLng() {
138 .removeAttr("data-lat")
139 .removeAttr("data-lon");
140 delete endpoint.latlng;
143 function setInputValueFromLatLng(latlng) {
144 input.val(latlng.lat + ", " + latlng.lng);
147 function convertLatLngToZoomPrecision(latlng) {
148 var precision = OSM.zoomPrecision(map.getZoom());
150 return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision));