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;
30 map.removeLayer(endpoint.marker);
33 function markerDragListener(e) {
34 var latlng = e.target.getLatLng();
37 setInputValueFromLatLng(latlng);
38 endpoint.value = input.val();
39 dragCallback(e.type === "drag");
42 function inputKeydownListener() {
43 input.removeClass("is-invalid");
46 function inputChangeListener(e) {
47 // make text the same in both text boxes
48 var value = e.target.value;
49 endpoint.setValue(value);
52 endpoint.setValue = function (value, latlng) {
53 endpoint.value = value;
54 delete endpoint.latlng;
55 input.removeClass("is-invalid");
60 setInputValueFromLatLng(latlng);
62 } else if (endpoint.value) {
67 function getGeocode() {
68 var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
69 var geocodeUrl = OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox;
71 if (endpoint.geocodeRequest) endpoint.geocodeRequest.abort();
72 endpoint.geocodeRequest = $.getJSON(geocodeUrl, function (json) {
73 delete endpoint.geocodeRequest;
74 if (json.length === 0) {
75 input.addClass("is-invalid");
76 alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
80 setLatLng(L.latLng(json[0]));
82 input.val(json[0].display_name);
88 function setLatLng(ll) {
95 function setInputValueFromLatLng(latlng) {
96 var precision = OSM.zoomPrecision(map.getZoom());
98 input.val(latlng.lat.toFixed(precision) + ", " + latlng.lng.toFixed(precision));