Skip to content

Commit

Permalink
Add button for directions from location
Browse files Browse the repository at this point in the history
  • Loading branch information
hlfan committed Feb 19, 2025
1 parent 79f5ccc commit b9ffd2f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 21 deletions.
25 changes: 25 additions & 0 deletions app/assets/javascripts/index/directions.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,31 @@ OSM.Directions = function (map) {
}));
});

$(".directions_form .locate").on("click", function () {
if ($(".directions_form .locate").hasClass("disabled")) return;
if (!navigator.geolocation) {
$(".directions_form .locate").addClass("disabled");
return;
}
navigator.geolocation.getCurrentPosition(geoSuccess);

function geoSuccess(position) {
const location = L.latLng(position.coords.latitude, position.coords.longitude),
[coordFrom, coordTo] = endpoints.map(ll => ll.latlng);
let routeTo;
if (coordTo) {
routeTo = coordTo.lat + "," + coordTo.lng;
} else if (coordFrom) {
routeTo = coordFrom.lat + "," + coordFrom.lng;
endpoints[0].swapCachedReverseGeocodes(endpoints[1]);
}
const routeFrom = location.lat + "," + location.lng;
OSM.router.route("/directions?" + new URLSearchParams({
route: routeFrom + ";" + routeTo
}));
}
});

$(".directions_form .btn-close").on("click", function (e) {
e.preventDefault();
$(".describe_location").toggle(!endpoints[0].value);
Expand Down
9 changes: 9 additions & 0 deletions app/assets/stylesheets/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,15 @@ header .search_forms,
}
}

.search_forms {
.geolocate {
transition: filter 0.15s ease-in-out;
}
.btn:not(:hover) .geolocate {
filter: brightness(53%);
}
}

/* Rules for search sidebar */

#sidebar .search_results_entry {
Expand Down
39 changes: 18 additions & 21 deletions app/views/layouts/_search.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,26 @@
<div class="d-flex flex-row-reverse px-3 pt-3 pb-1"><button type="button" class="btn-close" aria-label="<%= t("javascripts.close") %>"></button></div>

<div class="d-flex flex-column mx-2 gap-1">
<div class="d-flex gap-1 align-items-center">
<div class="d-flex flex-column gap-1 flex-grow-1">
<div class="d-flex gap-2 align-items-center">
<div class="routing_marker_column flex-shrink-0">
<%= image_tag "marker-green.png", :class => "img-fluid", :data => { :type => "from" }, :draggable => "true" %>
</div>
<%= text_field_tag "route_from", params[:from], :placeholder => t("site.search.from"), :autocomplete => "on", :class => "form-control py-1 px-2", :dir => "auto" %>
</div>
<div class="d-flex gap-2 align-items-center">
<div class="routing_marker_column flex-shrink-0">
<%= image_tag "marker-red.png", :class => "img-fluid", :data => { :type => "to" }, :draggable => "true" %>
</div>
<%= text_field_tag "route_to", params[:to], :placeholder => t("site.search.to"), :autocomplete => "on", :class => "form-control py-1 px-2", :dir => "auto" %>
</div>
<div class="d-flex gap-2 align-items-center">
<div class="routing_marker_column flex-shrink-0">
<%= image_tag "marker-green.png", :class => "img-fluid", :data => { :type => "from" }, :draggable => "true" %>
</div>
<div>
<button type="button" class="reverse_directions btn btn-outline-secondary border-0 p-2" title="<%= t("site.search.reverse_directions_text") %>">
<svg class="d-block" width="20" height="20" viewBox="-10 -10 20 20" fill="none" stroke="currentColor" stroke-width="2">
<path d="m-4 -2 0 10 m-4 -4 4 4 4 -4" />
<path d="m4 2 0 -10 m4 4 -4 -4 -4 4" />
</svg>
</button>
<%= text_field_tag "route_from", params[:from], :placeholder => t("site.search.from"), :autocomplete => "on", :class => "form-control py-1 px-2", :dir => "auto" %>
<button type="button" class="locate border-0 btn btn-outline-secondary p-2">
<span class="d-block geolocate icon"></span>
</button>
</div>
<div class="d-flex gap-2 align-items-center">
<div class="routing_marker_column flex-shrink-0">
<%= image_tag "marker-red.png", :class => "img-fluid", :data => { :type => "to" }, :draggable => "true" %>
</div>
<%= text_field_tag "route_to", params[:to], :placeholder => t("site.search.to"), :autocomplete => "on", :class => "form-control py-1 px-2", :dir => "auto" %>
<button type="button" class="reverse_directions btn btn-outline-secondary border-0 p-2" title="<%= t("site.search.reverse_directions_text") %>">
<svg class="d-block" width="20" height="20" viewBox="-10 -10 20 20" fill="none" stroke="currentColor" stroke-width="2">
<path d="m-4 -2 0 10 m-4 -4 4 4 4 -4" />
<path d="m4 2 0 -10 m4 4 -4 -4 -4 4" />
</svg>
</button>
</div>
<div class="d-flex gap-2 align-items-center">
<div class="routing_marker_column flex-shrink-0"></div>
Expand Down

0 comments on commit b9ffd2f

Please sign in to comment.