Positioning in a web browser

I am looking for a method to find the user's location while visiting my website, I tried Maxmind, but they seem to be inaccurate at the city level. the information I want (country, city, longitude, latitude), I want the country and city to be very accurate, if possible.

I also used HTML5, but the problem is that it asks my user to share location information, which seems like a bad solution to me. (althougth I got very accurate results)

Any solutions?

Note: I found that a Google search received an accurate detection and without asking to share my location, but I did not find an api to use the google service

+4
source share
3 answers

This is the code I found for the Geo Location tutorial using google maps. Hope this is helpful.

This works along the path of connecting to the network.

  • if you use a Boradband provider that gives you a fixed IP address of your location will be more accurate.
  • If you use a mobile device to connect to the Internet, the location of the nearest mobile tower with which your network will receive a signal will be shown.

HTML5 geolocation example

<!DOCTYPE html> <html> <head> <title>GeoLocation Demo</title> <meta charset="utf-8"/> <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> <script> var startPos; var map; function init() { if (navigator.geolocation) { document.getElementById("support").innerHTML = "<p style='color:green'>Great! This browser supports HTML5 Geolocation</p>"; navigator.geolocation.getCurrentPosition(updateLocation, handleLocationError, { timeout: 50000 }); //navigator.geolocation.watchPosition(updateCurrPosition,handleLocationError); } else { document.getElementById("support").innerHTML = "<p style='color:red'>Oops! This browser does not support HTML5 Geolocation</p>"; } } function updateLocation(position) { startPos = position; var latitude = position.coords.latitude; var longitude = position.coords.longitude; //document.getElementById("startLat").innerHTML = latitude; //document.getElementById("startLon").innerHTML = longitude; var coords = new google.maps.LatLng(latitude, longitude); var mapOptions = { zoom: 10, center: coords, mapTypeControl: false, navigationControlOptions: { style: google.maps.NavigationControlStyle.SMALL }, mapTypeId: google.maps.MapTypeId.ROADMAP }; map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions); var marker = new google.maps.Marker({ position: coords, map: map, title: "Your current location!" }); } function handleLocationError(error) { switch (error.code) { case 0: updateStatus("There was an error while retrieving your location: " + error.message); break; case 1: updateStatus("The user prevented this page from retrieving the location."); break; case 2: updateStatus("The browser was unable to determine your location: " + error.message); break; case 3: updateStatus("The browser timed out before retrieving the location."); break; } } function updateStatus(msg) { document.getElementById("divStatus").innerHTML = msg; } </script> </head> <body onload="init()"> <div id="support"></div> <div id="divStatus"></div> <div id="map_canvas" style="width:600px;height:400px;"></div> </body> </html> 
+3
source

I hope these links can help you. The first two links have a geolocation database by IP address. While links 3,4 and 5 work with the traceroute team. Let me know if they will work for you.

+1
source

IP addresses can be changed, it depends on the provider, and I think that is why the geolocation database is not always accurate. You can compare accuracy among these geolocation sites except maxmind. www.geobytes.com , www.hostip.info , www.ip2location.com

0
source

Source: https://habr.com/ru/post/1443589/


All Articles