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 map.removeLayer(endpoint.marker);
31 function markerDragListener(e) {
32 var latlng = e.target.getLatLng();
35 setInputValueFromLatLng(latlng);
36 endpoint.value = input.val();
37 dragCallback(e.type === "drag");
40 function inputKeydownListener() {
41 input.removeClass("is-invalid");
44 function inputChangeListener(e) {
45 // make text the same in both text boxes
46 var value = e.target.value;
47 endpoint.setValue(value);
50 endpoint.setValue = function (value, latlng) {
51 endpoint.value = value;
52 delete endpoint.latlng;
53 input.removeClass("is-invalid");
58 setInputValueFromLatLng(latlng);
60 } else if (endpoint.value) {
65 function getGeocode() {
66 endpoint.awaitingGeocode = true;
68 var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
70 $.getJSON(OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox, function (json) {
71 endpoint.awaitingGeocode = false;
72 if (json.length === 0) {
73 input.addClass("is-invalid");
74 alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
78 setLatLng(L.latLng(json[0]));
80 input.val(json[0].display_name);
86 function setLatLng(ll) {
93 function setInputValueFromLatLng(latlng) {
94 var precision = OSM.zoomPrecision(map.getZoom());
96 input.val(latlng.lat.toFixed(precision) + ", " + latlng.lng.toFixed(precision));