Article Diary - General Knowledge Digest

Geo-location Mapping With Google Maps API & PHP

Using a combination of a couple APIs, PHP and Curl you can easily incorporate Google Maps into your website that fairly accurately identify a visitor's location. Knowing a users location and further being able to map the location has many practical applications. I am going to breakdown the core elements of creating a map just like the one above. I plan on following up soon with more advanced Google Maps examples.
 
You will need the users' address to begin. If you run a site that collects that information during registration it can be used for mapping otherwise you can get the users city, state and country by grabbing their IP address and then calling one of the available geocoding APIs available.
 
Grab the users IP address.
$ip = $_SERVER['REMOTE_ADDR'];
 
Get the users City, State and Country.
$sturl = 'http://api.hostip.info/get_html.php?ip='.$ip;
$ch = curl_init($sturl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$res = curl_exec($ch);
$resinfo = curl_getinfo($ch);
curl_close($ch);
preg_match('/City: ([a-zA-Z].+[a-zA-Z]+)/', $res, $r);
preg_match('/ (([A-Z][A-Z])/', $res, $s);
$city = $r[1];
$country = $s[1];
 
The above API at http://hostip.info can also return the latitude and longitude which is needed to map the location however for the purposes of this demo I am using the Google geo locator API. You will also need a Google Maps API key which can be requested from http://code.google.com/apis/maps/.
 
Call the Google API to get the latitude and longitude.
$key = "Your Google Maps API Key";
$address = urlencode($city.", ".$country);
$sturl = 'http://maps.google.com/maps/geo?q=' . $address . '&output=csv&key=' . $key;
$ch = curl_init($sturl);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
curl_setopt($ch,CURLOPT_TIMEOUT,10);
$res = curl_exec($ch);
$resinfo = curl_getinfo($ch);
curl_close($ch);
$res = explode(",",$res);
$latitude = $res[2];
$longitude = $res[3];
 
Insert an image tag with the following SRC referencing Google's API.
http://maps.google.com/staticmap?key=' . $key . '&size=506x280&markers=' . $latitude . ',' . $longitude . '&zoom=3
 
This is just the tip of the iceberg. For more details on the API visit the Google documentation at http://code.google.com/apis/maps/documentation/.
 
Click here to download the PHP source code.
 
Attached Files
There are no attachments for this article.
Comments
There are no comments for this article. Be the first to post a comment.
Name
Email
Security Code Security Code
Related Articles RSS Feed
jCart - Free PHP AJAX Shopping Cart
Viewed 1277 times since Sun, Jan 3, 2010
jQuery Slider from a Select Element
Viewed 2365 times since Thu, Jan 14, 2010
Modify HTTP Headers in PHP with Examples
Viewed 1030 times since Wed, Mar 10, 2010
Tiny Flash MP3 Audio Player
Viewed 948 times since Wed, Jan 6, 2010
How can I change session timeout value?
Viewed 949 times since Tue, Apr 27, 2010
10 AJAX based PHP WebMail Clients
Viewed 1123 times since Mon, Jul 26, 2010
13 Useful Javascript Syntax Highlighting Scripts
Viewed 1299 times since Mon, Jan 4, 2010
PHP Script For PDF to HTML Conversion
Viewed 4715 times since Tue, Dec 15, 2009
Google Maps jQuery Plugin - gMap
Viewed 1274 times since Mon, Feb 8, 2010
How to restrict access to your .htaccess file?
Viewed 949 times since Sun, Mar 14, 2010
MENU