How to determine the user's current location and what is best to use?

There is an international project. When the user performs certain actions, it is necessary to receive data about his location (country, city, street(if possible)), and then we send this data to our server. I've seen examples from Google and Yandex maps, it is possible to get the coordinates. I'm still more inclined to use Google.

Is there an example of getting this data while making as few requests as possible?

P.S. At the same time, I do not need anything draw on the map and draw the map itself

Author: sanu0074, 2016-02-29

3 answers

  1. Using HTML5 - navigator.geolocation.getCurrentPosition(). The browser asks the user for permission and, if they agree, gives VERY (in most cases) accurate data in the form of latitude and longitude. Then you can feed it https://developers.google.com/maps/documentation/geocoding/intro without a map.
    upd 2018: this method works for https websites
  2. Determine by IP. There is a free (but not the most complete) database - https://dev.maxmind.com/ru/geolite2/ Here is a library under her - https://github.com/maxmind/GeoIP2-php In this case, the data is available immediately, but it is not accurate (Country, city).
 4
Author: xaja, 2018-06-05 13:12:25

If on js, I can give advice to use a request,
to a third-party service,
and then get json data from it, via jsonp.

<!-- Answer stackoverflow -->
<div id="ip"></div>
<div id="address"></div>
<div id="details"></div>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
    $.get("https://ipinfo.io", function (response) { 
      $("#ip").html("IP: " + response.ip); 
      $("#address").html("Location: " + response.city + ", " + response.region); 
      $("#details").html(JSON.stringify(response, null, 4)); 
      console.log(response); 
    }, "jsonp");
</script>

On codepen:
https://codepen.io/AndreyMyP/pen/gzXVXV

 2
Author: Frog Frogov, 2018-05-08 02:43:53

Yandex, by the way, provides the "Geocoder API" for HTTP requests: https://tech.yandex.ru/maps/doc/geocoder/desc/concepts/input_params-docpage/

The geocoder response can be generated in the following formats: XML, according to the YMapsML specification; JSON or JSONP.

To start working with the Geocoder HTTP API, you need to get the key for the "JavaScript API and HTTP Geocoder"package.

But there is a limit on the number of free requests of 25 thousand per day. And certain conditions, for example, the project must be publicly available, i.e. if there is a registration, it must be open to everyone.

 1
Author: velocat, 2019-01-30 21:06:33