Skip to content

Commit b9ffd2f

Browse files
committed
Add button for directions from location
1 parent 79f5ccc commit b9ffd2f

File tree

3 files changed

+52
-21
lines changed

3 files changed

+52
-21
lines changed

app/assets/javascripts/index/directions.js

+25
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,31 @@ OSM.Directions = function (map) {
7171
}));
7272
});
7373

74+
$(".directions_form .locate").on("click", function () {
75+
if ($(".directions_form .locate").hasClass("disabled")) return;
76+
if (!navigator.geolocation) {
77+
$(".directions_form .locate").addClass("disabled");
78+
return;
79+
}
80+
navigator.geolocation.getCurrentPosition(geoSuccess);
81+
82+
function geoSuccess(position) {
83+
const location = L.latLng(position.coords.latitude, position.coords.longitude),
84+
[coordFrom, coordTo] = endpoints.map(ll => ll.latlng);
85+
let routeTo;
86+
if (coordTo) {
87+
routeTo = coordTo.lat + "," + coordTo.lng;
88+
} else if (coordFrom) {
89+
routeTo = coordFrom.lat + "," + coordFrom.lng;
90+
endpoints[0].swapCachedReverseGeocodes(endpoints[1]);
91+
}
92+
const routeFrom = location.lat + "," + location.lng;
93+
OSM.router.route("/directions?" + new URLSearchParams({
94+
route: routeFrom + ";" + routeTo
95+
}));
96+
}
97+
});
98+
7499
$(".directions_form .btn-close").on("click", function (e) {
75100
e.preventDefault();
76101
$(".describe_location").toggle(!endpoints[0].value);

app/assets/stylesheets/common.scss

+9
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,15 @@ header .search_forms,
581581
}
582582
}
583583

584+
.search_forms {
585+
.geolocate {
586+
transition: filter 0.15s ease-in-out;
587+
}
588+
.btn:not(:hover) .geolocate {
589+
filter: brightness(53%);
590+
}
591+
}
592+
584593
/* Rules for search sidebar */
585594

586595
#sidebar .search_results_entry {

app/views/layouts/_search.html.erb

+18-21
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,26 @@
4040
<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>
4141

4242
<div class="d-flex flex-column mx-2 gap-1">
43-
<div class="d-flex gap-1 align-items-center">
44-
<div class="d-flex flex-column gap-1 flex-grow-1">
45-
<div class="d-flex gap-2 align-items-center">
46-
<div class="routing_marker_column flex-shrink-0">
47-
<%= image_tag "marker-green.png", :class => "img-fluid", :data => { :type => "from" }, :draggable => "true" %>
48-
</div>
49-
<%= text_field_tag "route_from", params[:from], :placeholder => t("site.search.from"), :autocomplete => "on", :class => "form-control py-1 px-2", :dir => "auto" %>
50-
</div>
51-
<div class="d-flex gap-2 align-items-center">
52-
<div class="routing_marker_column flex-shrink-0">
53-
<%= image_tag "marker-red.png", :class => "img-fluid", :data => { :type => "to" }, :draggable => "true" %>
54-
</div>
55-
<%= text_field_tag "route_to", params[:to], :placeholder => t("site.search.to"), :autocomplete => "on", :class => "form-control py-1 px-2", :dir => "auto" %>
56-
</div>
43+
<div class="d-flex gap-2 align-items-center">
44+
<div class="routing_marker_column flex-shrink-0">
45+
<%= image_tag "marker-green.png", :class => "img-fluid", :data => { :type => "from" }, :draggable => "true" %>
5746
</div>
58-
<div>
59-
<button type="button" class="reverse_directions btn btn-outline-secondary border-0 p-2" title="<%= t("site.search.reverse_directions_text") %>">
60-
<svg class="d-block" width="20" height="20" viewBox="-10 -10 20 20" fill="none" stroke="currentColor" stroke-width="2">
61-
<path d="m-4 -2 0 10 m-4 -4 4 4 4 -4" />
62-
<path d="m4 2 0 -10 m4 4 -4 -4 -4 4" />
63-
</svg>
64-
</button>
47+
<%= text_field_tag "route_from", params[:from], :placeholder => t("site.search.from"), :autocomplete => "on", :class => "form-control py-1 px-2", :dir => "auto" %>
48+
<button type="button" class="locate border-0 btn btn-outline-secondary p-2">
49+
<span class="d-block geolocate icon"></span>
50+
</button>
51+
</div>
52+
<div class="d-flex gap-2 align-items-center">
53+
<div class="routing_marker_column flex-shrink-0">
54+
<%= image_tag "marker-red.png", :class => "img-fluid", :data => { :type => "to" }, :draggable => "true" %>
6555
</div>
56+
<%= text_field_tag "route_to", params[:to], :placeholder => t("site.search.to"), :autocomplete => "on", :class => "form-control py-1 px-2", :dir => "auto" %>
57+
<button type="button" class="reverse_directions btn btn-outline-secondary border-0 p-2" title="<%= t("site.search.reverse_directions_text") %>">
58+
<svg class="d-block" width="20" height="20" viewBox="-10 -10 20 20" fill="none" stroke="currentColor" stroke-width="2">
59+
<path d="m-4 -2 0 10 m-4 -4 4 4 4 -4" />
60+
<path d="m4 2 0 -10 m4 4 -4 -4 -4 4" />
61+
</svg>
62+
</button>
6663
</div>
6764
<div class="d-flex gap-2 align-items-center">
6865
<div class="routing_marker_column flex-shrink-0"></div>

0 commit comments

Comments
 (0)