Is there a way to get the users location without having the user install Google Gears?
Asked Answered
C

6

7

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.

Cameo answered 24/11, 2008 at 10:4 Comment(1)
This has always had the best results for me: united-coders.com/christian-harms/free-ip-location-scriptTympanitis
V
9

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";
   }
 }
Valkyrie answered 24/11, 2008 at 10:8 Comment(0)
K
10

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">
</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
Killam answered 24/11, 2008 at 11:39 Comment(1)
For my website I have analysed that success rate is 54%. This is not very impressive :(Ordinarily
V
9

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";
   }
 }
Valkyrie answered 24/11, 2008 at 10:8 Comment(0)
C
5

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.

Catalan answered 24/11, 2008 at 10:40 Comment(0)
L
3

You can use HTML5 for this purpose. That is pretty simple. Here is example.

Laveralavergne answered 9/7, 2012 at 3:51 Comment(0)
L
1

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.

Libratory answered 12/4, 2009 at 22:23 Comment(0)
V
1

You can geocode the IP address using this simple code. It works great. Try this:

function functionname(Your IP Here)
{

    $ip = $ClientIP;

    $json = file_get_contents("http://api.easyjquery.com/ips/?ip=".$ip."&full=true");

    $json = json_decode($json,true);

    return $json;
}
Viniferous answered 19/10, 2012 at 7:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.