1 OSM.DirectionsEndpoint = function Endpoint(map, input, iconUrl, dragCallback, changeCallback) {
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 } else if (endpoint.value) {
47 endpoint.getGeocode();
51 endpoint.getGeocode = function () {
52 endpoint.awaitingGeocode = true;
54 var viewbox = map.getBounds().toBBoxString(); // <sw lon>,<sw lat>,<ne lon>,<ne lat>
56 $.getJSON(OSM.NOMINATIM_URL + "search?q=" + encodeURIComponent(endpoint.value) + "&format=json&viewbox=" + viewbox, function (json) {
57 endpoint.awaitingGeocode = false;
58 if (json.length === 0) {
59 input.addClass("is-invalid");
60 alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
64 setLatLng(L.latLng(json[0]));
66 input.val(json[0].display_name);
72 function setLatLng(ll) {
79 function setInputValueFromLatLng(latlng) {
80 var precision = OSM.zoomPrecision(map.getZoom());
82 input.val(latlng.lat.toFixed(precision) + ", " + latlng.lng.toFixed(precision));