Skip to content

Commit

Permalink
Fixed displacing points specified from context menu
Browse files Browse the repository at this point in the history
Fixes #2982 by updating directions.js so it doesn't perform unnecessary geocoding anymore for the previously selected point (either source or destination), when entering second point (either source or destination) by improving endpoint.setValue (now updates value only on change), endpoint.setLatLng (by extending ways of updating input values) and by allowing changing of endpoint's value (result of geocoding) only after retrieving results.
  • Loading branch information
nenad-vujicic committed Jun 17, 2024
1 parent 1a5adb2 commit 77a8796
Showing 1 changed file with 29 additions and 14 deletions.
43 changes: 29 additions & 14 deletions app/assets/javascripts/index/directions.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,9 @@ OSM.Directions = function (map) {
var dragging = (e.type === "drag");
if (dragging && !chosenEngine.draggable) return;
if (dragging && awaitingRoute) return;
endpoint.setLatLng(e.target.getLatLng());

endpoint.setLatLng(e.target.getLatLng(), true);

dragCallback(dragging);
});

Expand All @@ -83,16 +85,22 @@ OSM.Directions = function (map) {
// make text the same in both text boxes
var value = e.target.value;
endpoint.setValue(value);
input.val(value);
});

endpoint.setValue = function (value, latlng) {
endpoint.value = value;
delete endpoint.latlng;
input.removeClass("is-invalid");
input.val(value);
new_latlng_value = latlng;
if (value === endpoint.value) {
new_latlng_value = endpoint.latlng;
}
else {
endpoint.value = value;
delete endpoint.latlng;
input.removeClass("is-invalid");
}

if (latlng) {
endpoint.setLatLng(latlng);
if (new_latlng_value) {
endpoint.setLatLng(new_latlng_value);
} else {
endpoint.getGeocode();
}
Expand All @@ -118,17 +126,24 @@ OSM.Directions = function (map) {
return;
}

endpoint.setLatLng(L.latLng(json[0]));

input.val(json[0].display_name);
endpoint.setLatLng(L.latLng(json[0]), false, json[0].display_name);
endpoint.value = json[0].display_name;

geocodeCallback();
});
};

endpoint.setLatLng = function (ll) {
var precision = OSM.zoomPrecision(map.getZoom());
input.val(ll.lat.toFixed(precision) + ", " + ll.lng.toFixed(precision));
endpoint.setLatLng = function (ll, value_from_ll, value) {
if (typeof value_from_ll !== 'undefined' && value_from_ll !== null) {
if (value_from_ll) {
var precision = OSM.zoomPrecision(map.getZoom());
input.val(ll.lat.toFixed(precision) + ", " + ll.lng.toFixed(precision));
}
else {
input.val(value);
}
}

endpoint.hasGeocode = true;
endpoint.latlng = ll;
endpoint.marker
Expand Down Expand Up @@ -385,7 +400,7 @@ OSM.Directions = function (map) {
var pt = L.DomEvent.getMousePosition(oe, map.getContainer()); // co-ordinates of the mouse pointer at present
pt.y += 20;
var ll = map.containerPointToLatLng(pt);
endpoints[type === "from" ? 0 : 1].setLatLng(ll);
endpoints[type === "from" ? 0 : 1].setLatLng(ll, true);
getRoute(true, true);
});

Expand Down

0 comments on commit 77a8796

Please sign in to comment.