Skip to content

Commit

Permalink
Fixed displacing points defined 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 previously selected point (either source or destination) when entering second point. Allows reverse geocoding only when coordinates are specified from context menu. Also, improved setting markers by positioning it to selected position (not to result of geocoding) and improved focusing map view to include markers.
  • Loading branch information
nenad-vujicic committed Jul 20, 2024
1 parent 3debc15 commit 6489d0e
Showing 1 changed file with 54 additions and 14 deletions.
68 changes: 54 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 @@ -82,17 +84,28 @@ OSM.Directions = function (map) {
input.on("change", function (e) {
// make text the same in both text boxes
var value = e.target.value;
endpoint.setValue(value);
endpoint.setValue(value, false);
});

endpoint.setValue = function (value, latlng) {
endpoint.setValue = function (value, force_reverse_geocoding, latlng) {
if (!value && !latlng) {
return;
}

if (value === endpoint.value) {
endpoint.setLatLng(endpoint.latlng, false);
return;
}

endpoint.value = value;
delete endpoint.latlng;
input.removeClass("is-invalid");
input.val(value);

if (latlng) {
endpoint.setLatLng(latlng);
endpoint.setLatLng(latlng, true);
} else if (!force_reverse_geocoding && endpoint.value.match(/^([+-]?\d+(\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(\.\d*)?)$/)) {
endpoint.setLatLng(L.latLng(endpoint.value.split(/[/,]/)), false);
} else {
endpoint.getGeocode();
}
Expand All @@ -118,17 +131,29 @@ OSM.Directions = function (map) {
return;
}

endpoint.setLatLng(L.latLng(json[0]));
if (endpoint.value.match(/^([+-]?\d+(\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(\.\d*)?)$/)) {
endpoint.setLatLng(L.latLng(endpoint.value.split(/[/,]/)), false);
} else {
endpoint.setLatLng(L.latLng(json[0]), false);
}
endpoint.value = json[0].display_name;

input.val(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, populate_input) {
if (!ll) {
return;
}

if (populate_input) {
var precision = OSM.zoomPrecision(map.getZoom());
endpoint.value = ll.lat.toFixed(precision) + ", " + ll.lng.toFixed(precision);
input.val(endpoint.value);
}
endpoint.hasGeocode = true;
endpoint.latlng = ll;
endpoint.marker
Expand Down Expand Up @@ -246,6 +271,14 @@ OSM.Directions = function (map) {
$("#sidebar_content").html("<div class=\"alert alert-danger\">" + I18n.t("javascripts.directions.errors.no_route") + "</div>");
}

if (fitRoute) {
var partiallyCombinedBounds = L.latLngBounds();
partiallyCombinedBounds.extend(L.latLngBounds(o, o));
partiallyCombinedBounds.extend(L.latLngBounds(d, d));

map.fitBounds(partiallyCombinedBounds.pad(0.05));
}

return;
}

Expand All @@ -254,7 +287,12 @@ OSM.Directions = function (map) {
.addTo(map);

if (fitRoute) {
map.fitBounds(polyline.getBounds().pad(0.05));
var fullyCombinedBounds = L.latLngBounds();
fullyCombinedBounds.extend(polyline.getBounds());
fullyCombinedBounds.extend(L.latLngBounds(o, o));
fullyCombinedBounds.extend(L.latLngBounds(d, d));

map.fitBounds(fullyCombinedBounds.pad(0.05));
}

var distanceText = $("<p>").append(
Expand Down Expand Up @@ -385,14 +423,14 @@ 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);
});

var params = Qs.parse(location.search.substring(1)),
route = (params.route || "").split(";"),
from = route[0] && L.latLng(route[0].split(",")),
to = route[1] && L.latLng(route[1].split(","));
from = route[0] && L.latLng(route[0].split(/[/,]/)),
to = route[1] && L.latLng(route[1].split(/[/,]/));

if (params.engine) {
var engineIndex = findEngine(params.engine);
Expand All @@ -402,8 +440,10 @@ OSM.Directions = function (map) {
}
}

endpoints[0].setValue(params.from || "", from);
endpoints[1].setValue(params.to || "", to);
var ll_from_search_form = decodeURIComponent(location.search).match(/^\?from=([+-]?\d+(\.\d*)?)(?:\s+|\s*[/,]\s*)([+-]?\d+(\.\d*)?)$/);

endpoints[0].setValue(params.from || "", !ll_from_search_form, from);
endpoints[1].setValue(params.to || "", !ll_from_search_form, to);

map.setSidebarOverlaid(!from || !to);

Expand Down

0 comments on commit 6489d0e

Please sign in to comment.