Using a combination of a couple APIs, PHP and Curl you can easily incorporate 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/index.html.

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.

Posted by Michael in API,PHP on April 18, 2008

16 Responses

Yes, it’s really a simple example to use and very helpful, thank you very much for sharing with the community

rariox on May 21, 2008 at 3:17 pm

I’m trying to use your Call the Google API to get the latitude and longitude example, but the res returned from cURL is 620, 0, 0, 0, but when I hit the exact same link manually, it returns the correct comma delimited results. Do you know why?

Robbie on August 18, 2008 at 6:13 pm

I have a suggestion for Michael.
The database of “hostip.info” is provably out of date or needs updates.

You have provided a very good way to make website dynamic. But there is a problem.

As you can see the Previous comment by “Robiee on 18 Aug 2008″ most of the visitors will definitely get this problem.
This because: i repeat database of “hostip.info” needs update.

If you will create a webpage using this script and may want to greet your visitors saying—

“Hello ! you are welcomed to this website. Please free feel to visit your city through this map.”

Then you will never know how many visitors will kick the site.

About “hostip.info” [for visitors and webmasters]
If you want “hostip.info” show you the desired result then visit the site first and if your country and citiy appears correctly then you need to do nothing, if it doesn’t then you can fill a form to update the location related with your IP that you are using.

Michael is suggested to provide a good site with updated database.

Good post
Thanky you

www.Raaj.com.np on September 13, 2008 at 6:42 pm

The Fuse PHP framework comes with a nice Javascript class and controller for integrating Google Maps: http://phpfuse.net/wiki/index.php?title=Example:GoogleMaps

Jim on September 19, 2008 at 11:31 pm

This may be quite useful on a classifieds or real estate website when posting ads. Thanks!

Alp Bahar on November 23, 2008 at 11:06 am

Awesome…anyone tried to store locations in a db and then show them all on a map?

Arif Gangji on January 12, 2009 at 1:00 am

Instead of hostip.info you could also make use of the GeoCity db and api from http://maxmind.com/ – there is a opensource and a payed version ( the opensource seems to be 70% accurate )
Api for php: http://www.maxmind.com/download/geoip/api/php/
Opensource GeoLiteCity database:
http://geolite.maxmind.com/download/geoip/database/

Frank on January 15, 2009 at 7:24 am

thank you for this great post, I’m implementing a feature in a drupal website for search by proximity and this post helpme alot! thanks!!!!!

hubahubi on February 05, 2009 at 5:48 am

You can also output result as ‘csv’ to get only response code, accuray, lon & lat

Steve on April 27, 2009 at 8:11 pm

thanks this is very useful for me!

Myfacefriends on May 08, 2009 at 9:33 pm

how i can get a Google Api key ?

diaz on June 29, 2009 at 2:07 pm

Very useful post. I am also looking into plunging in the satellite map on my site. does anybody have any experience with that.

Brian on July 14, 2009 at 9:02 pm

I'm getting a fatal error …

Call to undefined function curl_init() in /home/leigeber/public_html/sandbox/google_maps_api.php on line 15

Lawry on February 15, 2010 at 5:19 am

Great article – I think a lot of your readers might benefit from using this PHP Class – http://code.google.com/p/php-google-map-api – the class lets you do everything from Geocoding to rendering fully usable maps, it's only limited by your imagination!

Brad on May 06, 2010 at 10:30 am

Thanks! this is very useful for me.

WMC on June 21, 2010 at 2:46 pm
Comments are closed at this time.