<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The html blog &#187; google maps</title>
	<atom:link href="http://htmlblog.net/tag/google-maps/feed/" rel="self" type="application/rss+xml" />
	<link>http://htmlblog.net</link>
	<description>The web sandbox of Asvin Balloo</description>
	<lastBuildDate>Wed, 07 Oct 2009 17:20:33 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Geolocate your visitors with PHP (part 2)</title>
		<link>http://htmlblog.net/geolocate-your-visitors-with-php-part-2/</link>
		<comments>http://htmlblog.net/geolocate-your-visitors-with-php-part-2/#comments</comments>
		<pubDate>Wed, 30 Jul 2008 11:30:08 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[geolocalisation]]></category>
		<category><![CDATA[geolocate]]></category>
		<category><![CDATA[google maps]]></category>
		<category><![CDATA[mashup]]></category>
		<category><![CDATA[maxmind]]></category>
		<category><![CDATA[net]]></category>
		<category><![CDATA[Net_GeoIP]]></category>
		<category><![CDATA[pear]]></category>

		<guid isPermaLink="false">http://htmlblog.net/?p=26</guid>
		<description><![CDATA[

In the first part of this series I showed how you could get the country of a visitor via his IP address. Now with this precious information, I'll show you how to map the visitor visually on the world map using Google Maps.
Sample preview.

You must sign up for a Google Maps API key if you [...]]]></description>
			<content:encoded><![CDATA[<p><a href='http://htmlblog.net/demo/myip/'><img src="http://htmlblog.net/wp-content/uploads/2008/07/myip.png" alt="" title="myip" width="437" height="318" class="alignnone size-full wp-image-27" /></a></p>
<div style="clear:both"></div>
<p><a href="http://htmlblog.net/geolocate-your-visitors-with-php-part-1/">In the first part</a> of this series I showed how you could get the country of a visitor via his IP address. Now with this precious information, I'll show you how to map the visitor visually on the world map using <a href="http://code.google.com/apis/maps/documentation/">Google Maps</a>.</p>
<p><strong><a href='http://htmlblog.net/demo/myip/'>Sample preview.</a></strong><br />
<span id="more-26"></span><br />
You must sign up for a <a href="http://code.google.com/apis/maps/signup.html">Google Maps API key</a> if you don't already have one.</p>
<p>After signing up for the key, include the following code in the head of your page :</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/script.html"><span style="color: #000000; font-weight: bold;">&lt;script</span></a> <span style="color: #000066;">src</span>=<span style="color: #ff0000;">&quot;http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=YOURKEY&quot;</span> <span style="color: #000066;">type</span>=<span style="color: #ff0000;">&quot;text/javascript&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span></span>
&nbsp;</pre>
<p>replacing the <strong>YOURKEY</strong> by the key you obtained.</p>
<p>We then define a container to hold our map with a width of 630px and height of 350px :</p>
<pre class="html4strict">&nbsp;
<span style="color: #009900;"><a href="http://december.com/html/4/element/div.html"><span style="color: #000000; font-weight: bold;">&lt;div</span></a> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;map&quot;</span> <span style="color: #000066;">style</span>=<span style="color: #ff0000;">&quot;width:630px;height:350px;&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/div&gt;</span></span>
&nbsp;</pre>
<p>Till now the overall code for the page, including the first part, is :</p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Net/GeoIP.php&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$HTTP_SERVER_VARS</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'HTTP_X_FORWARDED_FOR'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <a href="http://www.php.net/eregi"><span style="color: #000066;">eregi</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;^[0-9]{1,3}<span style="color: #000099; font-weight: bold;">\\</span>.[0-9]{1,3}<span style="color: #000099; font-weight: bold;">\\</span>.[0-9]{1,3}<span style="color: #000099; font-weight: bold;">\\</span>.[0-9]{1,3}$&quot;</span>, 
&nbsp;
<span style="color: #0000ff;">$HTTP_SERVER_VARS</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'HTTP_X_FORWARDED_FOR'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$ip</span> = <span style="color: #0000ff;">$HTTP_SERVER_VARS</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'HTTP_X_FORWARDED_FOR'</span><span style="color: #66cc66;">&#93;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$ip</span> = <a href="http://www.php.net/getenv"><span style="color: #000066;">getenv</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;REMOTE_ADDR&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">$geoip</span> = Net_GeoIP::<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;./data/GeoIP.dat&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #0000ff;">$country</span> = <span style="color: #0000ff;">$geoip</span>-&gt;<span style="color: #006600;">lookupCountryName</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ip</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;Google maps&lt;/title&gt;
		&lt;script src=<span style="color: #ff0000;">&quot;http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=YOURKEY&quot;</span> type=<span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;/script&gt;
	&lt;/head&gt;
	&lt;body&gt;
&lt;div id=<span style="color: #ff0000;">&quot;map&quot;</span> style=<span style="color: #ff0000;">&quot;width:630px;height:350px;&quot;</span>&gt;&lt;/div&gt;
&nbsp;
	&lt;/body&gt;
&lt;/html&gt;
&nbsp;</pre>
<p>Now begins the cool stuff, I'll show you the complete javascript code and then explain afterwards.</p>
<pre class="javascript">&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> locator = <span style="color: #66cc66;">&#123;</span>
	addAddressToMap: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>response<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		locator.<span style="color: #006600;">map</span>.<span style="color: #006600;">clearOverlays</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>!response || response.<span style="color: #000066;">Status</span>.<span style="color: #006600;">code</span> != <span style="color: #CC0000;">200</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			<span style="color: #009900; font-style: italic;">//alert(&quot;Sorry, we were unable to geocode that address&quot;);</span>
		<span style="color: #66cc66;">&#125;</span>
		<span style="color: #000066; font-weight: bold;">else</span> <span style="color: #66cc66;">&#123;</span>
			place = response.<span style="color: #006600;">Placemark</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>;
			point = <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #66cc66;">&#40;</span>place.<span style="color: #006600;">Point</span>.<span style="color: #006600;">coordinates</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>, place.<span style="color: #006600;">Point</span>.<span style="color: #006600;">coordinates</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
			marker = <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #66cc66;">&#40;</span>point<span style="color: #66cc66;">&#41;</span>;
			locator.<span style="color: #006600;">map</span>.<span style="color: #006600;">addOverlay</span><span style="color: #66cc66;">&#40;</span>marker<span style="color: #66cc66;">&#41;</span>;
			marker.<span style="color: #006600;">openInfoWindowHtml</span><span style="color: #66cc66;">&#40;</span>place.<span style="color: #006600;">address</span><span style="color: #66cc66;">&#41;</span>;
		<span style="color: #66cc66;">&#125;</span>
	<span style="color: #66cc66;">&#125;</span>,
&nbsp;
	load: <span style="color: #003366; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
		<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #66cc66;">&#40;</span>GBrowserIsCompatible<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
			locator.<span style="color: #006600;">map</span> = <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #66cc66;">&#40;</span>document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;map&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        		locator.<span style="color: #006600;">map</span>.<span style="color: #006600;">setCenter</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">34</span>, <span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#41;</span>;
        		locator.<span style="color: #006600;">geocoder</span> = <span style="color: #003366; font-weight: bold;">new</span> GClientGeocoder<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        		locator.<span style="color: #006600;">geocoder</span>.<span style="color: #006600;">getLocations</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;&lt;?php echo $country; ?&gt;&quot;</span>, locator.<span style="color: #006600;">addAddressToMap</span><span style="color: #66cc66;">&#41;</span>;
      		<span style="color: #66cc66;">&#125;</span>
    	<span style="color: #66cc66;">&#125;</span>
<span style="color: #66cc66;">&#125;</span>
&nbsp;</pre>
<p>We have a nice <a href="http://json.org/">JSON </a>object with 2 functions in it, namely:</p>
<ul>
<li>load</li>
<li>addAddressToMap</li>
</ul>
<p><strong>The load function</strong><br />
The load function will be called upon loading the page, it will initialize our map, and center it.</p>
<pre class="javascript">&nbsp;
locator.<span style="color: #006600;">map</span> = <span style="color: #003366; font-weight: bold;">new</span> GMap2<span style="color: #66cc66;">&#40;</span>document.<span style="color: #006600;">getElementById</span><span style="color: #66cc66;">&#40;</span><span style="color: #3366CC;">&quot;map&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
locator.<span style="color: #006600;">map</span>.<span style="color: #006600;">setCenter</span><span style="color: #66cc66;">&#40;</span><span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #66cc66;">&#40;</span><span style="color: #CC0000;">34</span>, <span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>We then create an instance of the <a href="http://code.google.com/apis/maps/documentation/reference.html#GClientGeocoder">GClientGeoCoder</a> class which will allow us to obtain geocodes for user specified addresses. </p>
<p>With the object created, we then call the <a href="http://code.google.com/apis/maps/documentation/reference.html#GClientGeocoder">getLocations</a> method with 2 parameters. The first one is the country which we got from the php script and the second parameter being the callback function. In this case our callback function is addAddressToMap.</p>
<p><strong>The addAddressToMap function</strong><br />
The addAddressToMap function is called when the geocoder returns an answer, with response as parameter. The response object contains various information such as the address and the coordinates for the country obtained via the <a href="http://code.google.com/apis/maps/documentation/reference.html#GClientGeocoder">getLocations </a>method. </p>
<p>These coordinates can be accessed via </p>
<ul>
<li>response.Placemark[0].Point.coordinates[1] for longitude and</li>
<li>response.Placemark[0].Point.coordinates[0] for latitude</li>
</ul>
<p>To make things a bit short I've placed the response.Placemark[0] in a variable called place :</p>
<pre class="javascript">&nbsp;
place = response.<span style="color: #006600;">Placemark</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>;
&nbsp;</pre>
<p>, thus the coordinates can be accessed via :</p>
<pre class="javascript">&nbsp;
place.<span style="color: #006600;">Point</span>.<span style="color: #006600;">coordinates</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>
place.<span style="color: #006600;">Point</span>.<span style="color: #006600;">coordinates</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span>
&nbsp;</pre>
<p>With these 2 coordinates we create a marker which we'll plot on the map :</p>
<pre class="javascript">&nbsp;
point = <span style="color: #003366; font-weight: bold;">new</span> GLatLng<span style="color: #66cc66;">&#40;</span>place.<span style="color: #006600;">Point</span>.<span style="color: #006600;">coordinates</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #66cc66;">&#93;</span>, place.<span style="color: #006600;">Point</span>.<span style="color: #006600;">coordinates</span><span style="color: #66cc66;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
marker = <span style="color: #003366; font-weight: bold;">new</span> GMarker<span style="color: #66cc66;">&#40;</span>point<span style="color: #66cc66;">&#41;</span>;
locator.<span style="color: #006600;">map</span>.<span style="color: #006600;">addOverlay</span><span style="color: #66cc66;">&#40;</span>marker<span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>As a bonus we also have a popup showing the country name using the <a href="http://code.google.com/apis/maps/documentation/reference.html">openInfoWindowHtml </a> method. We simply add the following line :</p>
<pre class="javascript">&nbsp;
marker.<span style="color: #006600;">openInfoWindowHtml</span><span style="color: #66cc66;">&#40;</span>place.<span style="color: #006600;">address</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;</pre>
<p>our final PHP page looks like that :</p>
<pre class="php">&nbsp;
<span style="color: #000000; font-weight: bold;">&lt;?php</span>
	<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;Net/GeoIP.php&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><a href="http://www.php.net/isset"><span style="color: #000066;">isset</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$HTTP_SERVER_VARS</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'HTTP_X_FORWARDED_FOR'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span> &amp;&amp; <a href="http://www.php.net/eregi"><span style="color: #000066;">eregi</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;^[0-9]{1,3}<span style="color: #000099; font-weight: bold;">\\</span>.[0-9]{1,3}<span style="color: #000099; font-weight: bold;">\\</span>.[0-9]{1,3}<span style="color: #000099; font-weight: bold;">\\</span>.[0-9]{1,3}$&quot;</span>, 
&nbsp;
<span style="color: #0000ff;">$HTTP_SERVER_VARS</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'HTTP_X_FORWARDED_FOR'</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$ip</span> = <span style="color: #0000ff;">$HTTP_SERVER_VARS</span><span style="color: #66cc66;">&#91;</span><span style="color: #ff0000;">'HTTP_X_FORWARDED_FOR'</span><span style="color: #66cc66;">&#93;</span>;
	<span style="color: #66cc66;">&#125;</span>
	<span style="color: #b1b100;">else</span><span style="color: #66cc66;">&#123;</span>
		<span style="color: #0000ff;">$ip</span> = <a href="http://www.php.net/getenv"><span style="color: #000066;">getenv</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;REMOTE_ADDR&quot;</span><span style="color: #66cc66;">&#41;</span>;
	<span style="color: #66cc66;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">$geoip</span> = Net_GeoIP::<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;./data/GeoIP.dat&quot;</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
	<span style="color: #0000ff;">$country</span> = <span style="color: #0000ff;">$geoip</span>-&gt;<span style="color: #006600;">lookupCountryName</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ip</span><span style="color: #66cc66;">&#41;</span>;
&nbsp;
<span style="color: #000000; font-weight: bold;">?&gt;</span>
&nbsp;
&lt;!DOCTYPE html <span style="color: #000000; font-weight: bold;">PUBLIC</span> <span style="color: #ff0000;">&quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot;</span> <span style="color: #ff0000;">&quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;</span>&gt;
&lt;html xmlns=<span style="color: #ff0000;">&quot;http://www.w3.org/1999/xhtml&quot;</span> xml:lang=<span style="color: #ff0000;">&quot;fr&quot;</span> lang=<span style="color: #ff0000;">&quot;fr&quot;</span>&gt;
	&lt;head&gt;
		&lt;meta http-equiv=<span style="color: #ff0000;">&quot;content-type&quot;</span> content=<span style="color: #ff0000;">&quot;text/html; charset=utf-8&quot;</span>/&gt;
		&lt;title&gt;my ip address with Google Maps&lt;/title&gt;
		&lt;script src=<span style="color: #ff0000;">&quot;http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=YOURKEY&quot;</span> type=<span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;&lt;/script&gt;
&nbsp;
		&lt;script type=<span style="color: #ff0000;">&quot;text/javascript&quot;</span>&gt;
    		<span style="color: #808080; font-style: italic;">//&lt;![CDATA[</span>
&nbsp;
    		<span style="color: #000000; font-weight: bold;">var</span> locator = <span style="color: #66cc66;">&#123;</span>    			
    			addAddressToMap: <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span>response<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				locator.map.clearOverlays<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>!response || response.Status.code != <span style="color: #cc66cc;">200</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
					<span style="color: #808080; font-style: italic;">//alert(&quot;Sorry, we were unable to geocode that address&quot;);</span>
				<span style="color: #66cc66;">&#125;</span>
				<span style="color: #b1b100;">else</span> <span style="color: #66cc66;">&#123;</span>
					place = response.Placemark<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span>;
					point = <span style="color: #000000; font-weight: bold;">new</span> GLatLng<span style="color: #66cc66;">&#40;</span>place.Point.coordinates<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#93;</span>, place.Point.coordinates<span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#93;</span><span style="color: #66cc66;">&#41;</span>;
					marker = <span style="color: #000000; font-weight: bold;">new</span> GMarker<span style="color: #66cc66;">&#40;</span>point<span style="color: #66cc66;">&#41;</span>;
					locator.map.addOverlay<span style="color: #66cc66;">&#40;</span>marker<span style="color: #66cc66;">&#41;</span>;
					marker.openInfoWindowHtml<span style="color: #66cc66;">&#40;</span>place.address<span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
			<span style="color: #66cc66;">&#125;</span>,
&nbsp;
			load: <span style="color: #000000; font-weight: bold;">function</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
				<span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span>GBrowserIsCompatible<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#123;</span>
					locator.map = <span style="color: #000000; font-weight: bold;">new</span> GMap2<span style="color: #66cc66;">&#40;</span>document.getElementById<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;map&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;
        				locator.map.setCenter<span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> GLatLng<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">34</span>, <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span>, <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>;
        				locator.geocoder = <span style="color: #000000; font-weight: bold;">new</span> GClientGeocoder<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;
        				locator.geocoder.getLocations<span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;&lt;?php echo $country; ?&gt;&quot;</span>, locator.addAddressToMap<span style="color: #66cc66;">&#41;</span>;
				<span style="color: #66cc66;">&#125;</span>
      			<span style="color: #66cc66;">&#125;</span>
    		<span style="color: #66cc66;">&#125;</span>
&nbsp;
    		<span style="color: #808080; font-style: italic;">//]]&gt;</span>
		<span style="color: #000000; font-weight: bold;">&lt;/script&gt;</span>
	&lt;/head&gt;
	&lt;body onload=<span style="color: #ff0000;">&quot;locator.load();&quot;</span> onunload=<span style="color: #ff0000;">&quot;GUnload();&quot;</span>&gt;
&lt;div id=<span style="color: #ff0000;">&quot;map&quot;</span> style=<span style="color: #ff0000;">&quot;width:630px;height:350px;&quot;</span>&gt;&lt;/div&gt;
&nbsp;
	&lt;/body&gt;
&lt;/html&gt;
&nbsp;</pre>
<p>After the page has been loaded, we just call the <strong>locator.load()</strong> function which will initialize the whole process.</p>
<p><strong><a href="http://htmlblog.net/demo/myip/">Live demo</a></strong><br />
<strong><a href="http://htmlblog.net/demo/myip/myip.zip">Download sample demo</a></strong></p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/geolocate-your-visitors-with-php-part-2/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Geolocate your visitors with PHP (part 1)</title>
		<link>http://htmlblog.net/geolocate-your-visitors-with-php-part-1/</link>
		<comments>http://htmlblog.net/geolocate-your-visitors-with-php-part-1/#comments</comments>
		<pubDate>Wed, 09 Jul 2008 15:04:31 +0000</pubDate>
		<dc:creator>asvin</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[country]]></category>
		<category><![CDATA[geoip]]></category>
		<category><![CDATA[google maps]]></category>
		<category><![CDATA[maxmind]]></category>
		<category><![CDATA[net]]></category>
		<category><![CDATA[pear]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://htmlblog.net/?p=19</guid>
		<description><![CDATA[

In this short post I'll show you how to geolocate your visitors with PHP. First of all let's have a look at what you'll need :

PEAR GeoIP
MaxMind GeoLite Country


Installing PEAR GeoIP
Using your console or using the command prompt type :
&#160;
pear install http://download.pear.php.net/package/Net_GeoIP-1.0.0RC1.tgz
&#160;
Obtaining MaxMind GeoLite Country
The database can be downloaded freely here. Download it and put [...]]]></description>
			<content:encoded><![CDATA[<p><img src="http://farm3.static.flickr.com/2358/2267796445_29dc34fe87_m.jpg" alt="geolocate" /></p>
<div style="clear:both"></div>
<p>In this short post I'll show you how to geolocate your visitors with <a href="http://php.net">PHP</a>. First of all let's have a look at what you'll need :</p>
<ul>
<li>PEAR GeoIP</li>
<li>MaxMind GeoLite Country</li>
</ul>
<p><span id="more-19"></span><br />
<strong>Installing PEAR GeoIP</strong><br />
Using your console or using the command prompt type :</p>
<pre class="bash">&nbsp;
pear <span style="color: #c20cb9; font-weight: bold;">install</span> http://download.pear.php.net/package/Net_GeoIP<span style="color: #000000;">-1.0</span>.0RC1.tgz
&nbsp;</pre>
<p><strong>Obtaining MaxMind GeoLite Country</strong><br />
The database can be <a href="http://www.maxmind.com/app/geoip_country">downloaded freely here</a>. Download it and put it in the same folder where your script will reside.<br />
<!--more--><br />
<strong>The code</strong></p>
<pre class="php"><ol><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #b1b100;">require_once</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Net/GeoIP.php'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0000ff;">$geoip</span> = Net_GeoIP::<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'./GeoIP.dat'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0000ff;">$ip</span> = <a href="http://www.php.net/getenv"><span style="color: #000066;">getenv</span></a><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'REMOTE_ADDR'</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<span style="color: #0000ff;">$country</span> = <span style="color: #0000ff;">$geoip</span>-&gt;<span style="color: #006600;">lookupCountryName</span><span style="color: #66cc66;">&#40;</span><span style="color: #0000ff;">$ip</span><span style="color: #66cc66;">&#41;</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">	<a href="http://www.php.net/echo"><span style="color: #000066;">echo</span></a> <span style="color: #ff0000;">&quot;Hi, you're from &quot;</span>.<span style="color: #0000ff;">$country</span>;</div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;"><span style="color: #000000; font-weight: bold;">?&gt;</span></div></li><li style="font-family: 'Courier New', Courier, monospace; color: black; font-weight: normal; font-style: normal;"><div style="font-family: 'Courier New', Courier, monospace; font-weight: normal;">&nbsp;</div></li></ol></pre>
<p><strong>Walkthrough</strong><br />
Now let's go through the lines of code. The first line  tells us to include the GeoIP class, which we installed previously and then create a geoip object in line 5 from the database we downloaded.<br />
Next, in line 6 we get the IP address of the visitor and finally we call the <a href="http://pear.php.net/manual/en/package.networking.net-geoip.lookupcountryname.php">lookupCountryName</a> method with the IP address as parameter in line 7 and it will return us the country. </p>
<p>The last line just outputs the country, for example,  "Hi, you're from Mauritius". That's it, now you know where your visitor's from.</p>
<p><a href="http://htmlblog.net/geolocate-your-visitors-with-php-part-2/">In the second part of this article</a> I'll show you how to use the script to visually map your visitors using <a href="http://code.google.com/apis/maps/">Google Maps</a>. You can have a little <a href="http://htmlblog.net/demo/myip/">preview of the script here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/geolocate-your-visitors-with-php-part-1/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
