views:

1661

answers:

5

I don't want to have the user install Google Gears so I can show him his guessed location. Is there a way to get the location without having to use Google Gears?

I have found http://www.wipmania.com/de/blog/google-geolocation-api/ but it does not offer an example.

+4  A: 

This is typically called IP Geolocation.

An example is here.

The thing is, most sites (if you plan on calling this as a web service) will charge you for it. Otherwise, throw together a web service that grabs a geolocation page, parses it for the address, and returns that piece of information.

In PHP, this seems to work pretty well:

<?php
 if(isset($_GET["ip"])){
   echo geolocate($_GET["ip"]);
 }

 function geolocate($ip){
   $raw_html = file_get_contents("http://www.geody.com/geoip.php?ip=$ip");
   if(preg_match('/Location:(.*)/',$raw_html,$matches)){
     $location_raw = $matches[1];

     //Get rid of pesky HTML tags
     $location = preg_replace("/<[^>]*>/","",$location_raw);
     return $location;
   }else{
     return "ERROR";
   }
 }
Stefan Mai
+2  A: 

One thing to watch out for with an geolocation service or library is that they will never be 100% accurate. We use one, IP2Location, and it actually has been a steady source of complaints to our customer service department. We are looking at ways to alter the behavior of the site and weighing the factors of removing it completely.

The problem lies in the fact that the burden of associating the IP with a user is with the users ISP. They have large blocks of IP ranges, and they register which blocks belong to a specific location. The problem is that ISPs often don't update this information when they shift blocks or a block covers too large of an area.

For example, I live about sixty miles north of Manhattan, but my IP often shows that I am in Chicago with most of the lookup tools. I have FIOS, and have talked to a couple of other people who have confirmed the same behavior.

joseph.ferris
+7  A: 

Google provides a way to get the user location via it's AJAX API loader. This will even work without Gears installed. Simply include a script tag pointing to the JsAPI:

<script type="text/javascript" 
   src="http://www.google.com/jsapi?key=ABCDEFG"&gt;
</script>

When an application makes use of the AJAX API loader, the loader attempts to geo locate the client based on it's IP address. If this process succeeds, the client's location, scoped to the metro level, is made available in the google.loader.ClientLocation property. If the process fails to find a match, this property is set to null.

if (google.loader.ClientLocation){
  var center = new google.maps.LatLng(
    google.loader.ClientLocation.latitude,
    google.loader.ClientLocation.longitude
  );
  map.setCenter(center);
}

When populated, the google.loader.ClientLocation object is populated with the following metro-level granularity properties:

  • ClientLocation.latitude — supplies the low resolution latitude associated with the client's IP address
  • ClientLocation.longitude — supplies the low resolution longitude associated with the client's IP address
  • ClientLocation.address.city — supplies the name of the city associated with the client's IP address
  • ClientLocation.address.country — supplies the name of the country associated with the client's IP address
  • ClientLocation.address.country_code — supplies the name of the ISO 3166-1 country code associated with the client's IP address
  • ClientLocation.address.region — supplies the country specific region name associated with the client's IP address
aemkei
For my website I have analysed that success rate is 54%. This is not very impressive :(
Varun
+1  A: 

If your users are on mobile, Xitfy is also a free option. They have a small client that after the user agrees can be seamlessly installed on their handset. You can then call a REST based api to get their exact lat/lon sourced from gps / wifi or cell tower.

They support Android, Windows Mobile, BlackBerry and Symbian which covers almost all smartphones.

Jeremy
A: 

Until HTML 5 GeoLocation API is standard on all browsers and the users don’t have to explicitly grant permission like on Firefox at the moment, then the closest you will get is what you see on services like IP2Location as they most likely get their data from ISPs so the accuracy all depends on the diligence of the ISPs in updating their IP allocation information.

Souless