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());
40 setInputValueFromLatLng(latlng);
41 endpoint.value = input.val();
42 dragCallback(e.type === "drag");
45 function inputKeydownListener() {
46 input.removeClass("is-invalid");
49 function inputChangeListener(e) {
50 // make text the same in both text boxes
51 var value = e.target.value;
52 endpoint.setValue(value);
55 endpoint.setValue = function (value, latlng) {
56 endpoint.value = value;
58 input.removeClass("is-invalid");
63 setInputValueFromLatLng(latlng);
65 } else if (endpoint.value) {
70 function getGeocode() {
71 var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
72 var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox;
74 if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
75 endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) {
76 delete endpoint.geocodeRequest;
77 if (json.length === 0) {
78 input.addClass("is-invalid");
79 alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
83 setLatLng(L.latLng(json[0]));
85 input.val(json[0].display_name);
91 function setLatLng(ll) {
93 .attr("data-lat", ll.lat)
94 .attr("data-lon", ll.lng);
101 function removeLatLng() {
103 .removeAttr("data-lat")
104 .removeAttr("data-lon");
105 delete endpoint.latlng;
108 function setInputValueFromLatLng(latlng) {
109 input.val(latlng.lat + ", " + latlng.lng);
112 function convertLatLngToZoomPrecision(latlng) {
113 var precision = OSM.zoomPrecision(map.getZoom());
115 return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision));