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 dragCallback(e.type === "drag");
48 function inputKeydownListener() {
49 input.removeClass("is-invalid");
52 function inputChangeListener(e) {
53 // make text the same in both text boxes
54 var value = e.target.value;
55 endpoint.setValue(value);
58 endpoint.setValue = function (value, latlng) {
59 endpoint.value = value;
61 input.removeClass("is-invalid");
64 if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
65 delete endpoint.geocodeRequest;
69 setInputValueFromLatLng(latlng);
71 } else if (endpoint.value) {
76 function getGeocode() {
77 var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
78 var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox;
80 endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) {
81 delete endpoint.geocodeRequest;
82 if (json.length === 0) {
83 input.addClass("is-invalid");
84 alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
88 setLatLng(L.latLng(json[0]));
90 input.val(json[0].display_name);
96 function setLatLng(ll) {
98 .attr("data-lat", ll.lat)
99 .attr("data-lon", ll.lng);
100 endpoint.latlng = ll;
106 function removeLatLng() {
108 .removeAttr("data-lat")
109 .removeAttr("data-lon");
110 delete endpoint.latlng;
113 function setInputValueFromLatLng(latlng) {
114 input.val(latlng.lat + ", " + latlng.lng);
117 function convertLatLngToZoomPrecision(latlng) {
118 var precision = OSM.zoomPrecision(map.getZoom());
120 return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision));