Answer the question
In order to leave comments, you need to log in
How to make a "Get route" Google Maps button on the site?
That is, the site visitor presses the "Build a route" button and Google maps opens where the route is built from his location to a given point.
I know that you can make such a link where:
https://www.google.com/maps/dir/ (here is the address of departure)/(here is the address of destination)
but how to automatically determine the address of departure, that is, the current coordinates of a person?
Answer the question
In order to leave comments, you need to log in
At most, you can determine the user's city, but the coordinates cannot be obtained automatically. Only if he himself clicks on the map where he is.
automatic determination of the address of departure, that is, the current coordinates of a person?
function getCoords() {
function success(position) {
const latitude = position.coords.latitude;
const longitude = position.coords.longitude;
// это можно куда-нибудь сохранить, в сторадж или переменную
const href = `https://www.google.com/maps/search/${latitude},${longitude}`
console.log(href)
}
function error() {
console.log('Unable to retrieve your location')
}
if(!navigator.geolocation) {
console.log('Geolocation is not supported by your browser')
} else {
navigator.geolocation.getCurrentPosition(success, error);
}
}
getCoords()
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question