By admirer

2012-11-02 06:03:23 8 Comments

I am searching for a method to locate the user location while visiting my website, i have tried Maxmind but they seems to be inaccurate in the city level. the information i want is (Country, City, longitude, latitude) i want the country and the city to be very accurate if possible.

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

Any solutions?

Notice: i found google search got an accurate detection and without "ask for sharing my location", but i didn't find any api to use google service


@Naveen Babu 2012-11-02 06:23:20

This is a code i found for a tutorial for Geo Location using Google maps.. Hope this is useful.

This works on the way you connect to the network.

  • if you use a Boradband ISP what gives you an fixed ip your location will be more accurate.
  • if you use mobile device to connect to internet, the Location of the nearest Mobile tower from which your network gets the signal will be shown

Example for HTML5 Geolocation

<!DOCTYPE html>
<title>GeoLocation Demo</title>
<meta charset="utf-8"/>
<script src="" type="text/javascript"></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

      } 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);

          case 1:
              updateStatus("The user prevented this page from retrieving the location.");

          case 2:
              updateStatus("The browser was unable to determine your location: " + error.message);


          case 3:

              updateStatus("The browser timed out before retrieving the location.");


  function updateStatus(msg) {
      document.getElementById("divStatus").innerHTML = msg;

<body onload="init()">  
<div id="support"></div>
<div id="divStatus"></div>
<div id="map_canvas" style="width:600px;height:400px;"></div>

@admirer 2012-11-02 06:43:41

as i see your code is using HTML5, thus it will popup "Share location" request to my user before advancing .. This is problematic for me .. and i know that HTML5 force that popup .. but at the same time i see google doing it in the search without any popup .. any idea ?

@Naveen Babu 2012-11-02 07:46:05

I have read Browsers have setting to disable share location by default as it is a against privacy of the user and can get any software developer into serious trouble if we dont have prior authorization from the user to access the location

@admirer 2012-11-02 14:18:44

so i think your code won't work .. my problem is how google getting it accurate without that ? i think there is another solution

@lsaffie 2014-02-18 00:57:46

I believe this is a pretty accurate answer to your question. I've done something similar to this. You can see an actual example here Feel free to examine the source to see what the code looks like

@Anish 2013-01-22 15:43:05


I hope these links might help you. First two links have a database of geolocation information with respect to IP addresses. Whereas the links 3,4, & 5 work on traceroute command. Let me know if these work for you .

@HexaHow 2012-11-02 10:03:31

The IP addresses may be changed depends on ISP and I think that is the reason why the geolocation database is not always accurate. You can compare the accuracy among these geolocation sites other than maxmind.,,

Related Questions

Sponsored Content

9 Answered Questions

[SOLVED] Location detecting techniques for IP addresses

  • 2010-04-04 13:41:23
  • ilhan
  • 29786 View
  • 18 Score
  • 9 Answer
  • Tags:   http geolocation

2 Answered Questions

3 Answered Questions

[SOLVED] Best node.js module for finding location?

2 Answered Questions

[SOLVED] Best IP to Country Database

3 Answered Questions

[SOLVED] How to learn location of the web user?

1 Answered Questions

[SOLVED] Convert longitude/latitude to country, city in MaxMind

0 Answered Questions

How foursquare homepage location detector works?

  • 2013-04-17 04:18:19
  • knightrider
  • 109 View
  • 0 Score
  • 0 Answer
  • Tags:   html5 geolocation

1 Answered Questions

html5 geolocation for user location in mvc3

4 Answered Questions

Sponsored Content