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 = convertLatLngToZoomPrecision(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;
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) {
90 .attr("data-lat", ll.lat)
91 .attr("data-lon", ll.lng);
98 function removeLatLng() {
100 .removeAttr("data-lat")
101 .removeAttr("data-lon");
102 delete endpoint.latlng;
105 function setInputValueFromLatLng(latlng) {
106 input.val(latlng.lat + ", " + latlng.lng);
109 function convertLatLngToZoomPrecision(latlng) {
110 var precision = OSM.zoomPrecision(map.getZoom());
112 return L.latLng(latlng.lat.toFixed(precision), latlng.lng.toFixed(precision));