3 ways to do this in this answer:
- Get the exact GPS location by asking the user for access to the API of their browser
- Get an approximate location (country, city, region) using an external GeoIP service
- Get an approximate location (country, city, district) using the CDN service
Ask the user to allow access to their browser API
You can find out the location of the client using the HTML5 features. This will give you the exact location of the user if this is done from a device with GPS, or an approximate location. As soon as the user confirms his location, you will receive it without confirmation. This solution uses Geolocation.getCurrentPosition () . In most cases, this must be done using the HTTPS protocol.
If in a hurry, here is CodePen with this solution: https://codepen.io/sebastienfi/pen/pqNxEa
<!DOCTYPE html> <html> <body> <p id="demo">Click the button to get your coordinates:</p> <button onclick="getLocation()">Try It</button> <script> var x = document.getElementById("demo"); function getLocation() { if (navigator.geolocation) { navigator.geolocation.getCurrentPosition( </script> </body> </html>
Error and deviation handling The second parameter of the getCurrentPosition () method is used to handle errors. It defines a function to run if it is not possible to determine the user's location:
function showError(error) { switch(error.code) { case error.PERMISSION_DENIED: x.innerHTML = "User denied the request for Geolocation." break; case error.POSITION_UNAVAILABLE: x.innerHTML = "Location information is unavailable." break; case error.TIMEOUT: x.innerHTML = "The request to get user location timed out." break; case error.UNKNOWN_ERROR: x.innerHTML = "An unknown error occurred." break; } }
Display result on map
function showPosition(position) { var latlon = position.coords.latitude + "," + position.coords.longitude; var img_url = "http://maps.googleapis.com/maps/api/staticmap?center= "+latlon+"&zoom=14&size=400x300&sensor=false"; document.getElementById("mapholder").innerHTML = "<img src='"+img_url+"'>"; }
Geoip
If the solution above does not work in your scenario, you can get an approximate position using the IP location. You do not necessarily get the exact location of the user, but you can specify the location of the nearest Internet site in the area of ββthe user's connection point, which can be close enough for 99% of cases of use (country, city, region),
Since this is not accurate, but does not require user approval, it may also meet your requirements.
Find below 2 ways to do this. I would recommend doing this with a CDN solution because it is faster and yes, speed matters a lot.
Get an approximate location (country, city, region) using an external GeoIP service
Many external services allow you to get the location of GeoIP. Here is an example from Google.
To get the Google API key, go to: https://developers.google.com/loader/signup?csw=1.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <title>Get web visitor location</title> <meta name="robots" value="none" /> </head> <body> <div id="yourinfo"></div> <script type="text/javascript" src="http://www.google.com/jsapi?key=<YOUR_GOOGLE_API_KEY>"></script> <script type="text/javascript"> if(google.loader.ClientLocation) { visitor_lat = google.loader.ClientLocation.latitude; visitor_lon = google.loader.ClientLocation.longitude; visitor_city = google.loader.ClientLocation.address.city; visitor_region = google.loader.ClientLocation.address.region; visitor_country = google.loader.ClientLocation.address.country; visitor_countrycode = google.loader.ClientLocation.address.country_code; document.getElementById('yourinfo').innerHTML = '<p>Lat/Lon: ' + visitor_lat + ' / ' + visitor_lon + '</p><p>Location: ' + visitor_city + ', ' + visitor_region + ', ' + visitor_country + ' (' + visitor_countrycode + ')</p>'; } else { document.getElementById('yourinfo').innerHTML = '<p>Whoops!</p>'; } </script> </body> </html>
Get an approximate location (country, city, district) using the CDN service
An alternative to using external services is most of the CDN. The advantage of this solution is that an additional HTTP request is not required, therefore it is faster. If you already have a CDN, this is the way to go. You can use CloudFlare, CloudFront, etc.
In this scenario, you will most likely end up specifying the location as a parameter of the HTTP request header, or even directly in the window object to get it using Javascript. Read the CDN documentation to find out more.
Edited mid-December 2018 to add more options.