1 OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, geocodeCallback) {
4 endpoint.marker = L.marker([0, 0], {
10 shadowUrl: OSM.MARKER_SHADOW,
17 endpoint.marker.on("drag dragend", function (e) {
18 var latlng = e.target.getLatLng();
21 setInputValueFromLatLng(latlng);
22 endpoint.value = input.val();
23 dragCallback(e.type === "drag");
26 input.on("keydown", function () {
27 input.removeClass("is-invalid");
30 input.on("change", function (e) {
31 // make text the same in both text boxes
32 var value = e.target.value;
33 endpoint.setValue(value);
36 endpoint.setValue = function (value, latlng) {
37 endpoint.value = value;
38 delete endpoint.latlng;
39 input.removeClass("is-invalid");
44 setInputValueFromLatLng(latlng);
46 endpoint.getGeocode();
50 endpoint.getGeocode = function () {
51 // if no one has entered a value yet, then we can't geocode, so don't
53 if (!endpoint.value) {
57 endpoint.awaitingGeocode = true;
59 var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
61 $.getJSON(OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox, function (json) {
62 endpoint.awaitingGeocode = false;
63 endpoint.hasGeocode = true;
64 if (json.length === 0) {
65 input.addClass("is-invalid");
66 alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
70 setLatLng(L.latLng(json[0]));
72 input.val(json[0].display_name);
78 function setLatLng(ll) {
79 endpoint.hasGeocode = true;
86 function setInputValueFromLatLng(latlng) {
87 var precision = OSM.zoomPrecision(map.getZoom());
89 input.val(latlng.lat.toFixed(precision) + ", " + latlng.lng.toFixed(precision));