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 endpoint.setLatLng(e.target.getLatLng());
19 dragCallback(e.type === "drag");
22 input.on("keydown", function () {
23 input.removeClass("is-invalid");
26 input.on("change", function (e) {
27 // make text the same in both text boxes
28 var value = e.target.value;
29 endpoint.setValue(value);
32 endpoint.setValue = function (value, latlng) {
33 endpoint.value = value;
34 delete endpoint.latlng;
35 input.removeClass("is-invalid");
39 endpoint.setLatLng(latlng);
41 endpoint.getGeocode();
45 endpoint.getGeocode = function () {
46 // if no one has entered a value yet, then we can't geocode, so don't
48 if (!endpoint.value) {
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 endpoint.hasGeocode = true;
59 if (json.length === 0) {
60 input.addClass("is-invalid");
61 alert(I18n.t("javascripts.directions.errors.no_place", { place: endpoint.value }));
65 endpoint.setLatLng(L.latLng(json[0]));
67 input.val(json[0].display_name);
73 endpoint.setLatLng = function (ll) {
74 var precision = OSM.zoomPrecision(map.getZoom());
75 input.val(ll.lat.toFixed(precision) + ", " + ll.lng.toFixed(precision));
76 endpoint.hasGeocode = true;