A
A
Anton R.2020-09-25 11:09:37
JavaScript
Anton R., 2020-09-25 11:09:37

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

2 answer(s)
K
Kirill Gorelov, 2020-09-25
@anton_reut

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.

H
hzzzzl, 2020-09-25
@hzzzzl

automatic determination of the address of departure, that is, the current coordinates of a person?

will not exit automatically without the user's permission, but the browser may request the location
https://developer.mozilla.org/en-US/docs/Web/API/G...
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 question

Ask a Question

731 491 924 answers to any question