Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 789
Thomaschaaf Avatar asked Nov 24 '08 10:11

Thomaschaaf


People also ask

How does Google get location from IP?

From the IP address of your Internet connection IP addresses are used to make the connection between your device and the websites and services that you use. IP addresses are roughly based on geography. This means that any website that you use, including google.com, may get some information about your general area.

How do you get geolocation?

The Geolocation API is accessed via a call to navigator. geolocation ; this will cause the user's browser to ask them for permission to access their location data. If they accept, then the browser will use the best available functionality on the device to access this information (for example, GPS).

Is Geolocation API free?

The Geolocation API uses a pay-as-you-go pricing model.


2 Answers

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
like image 180
aemkei Avatar answered Nov 14 '22 06:11

aemkei


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";
   }
 }
like image 45
Stefan Mai Avatar answered Nov 14 '22 04:11

Stefan Mai