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.
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.
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).
The Geolocation API uses a pay-as-you-go pricing model.
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 addressClientLocation.longitude
— supplies the low resolution longitude associated with the client's IP addressClientLocation.address.city
— supplies the name of the city associated with the client's IP addressClientLocation.address.country
— supplies the name of the country associated with the client's IP addressClientLocation.address.country_code
— supplies the name of the ISO 3166-1 country code associated with the client's IP addressClientLocation.address.region
— supplies the country specific region name associated with the client's IP addressThis 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";
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With