<?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; maxmind</title>
	<atom:link href="http://htmlblog.net/tag/maxmind/feed/" rel="self" type="application/rss+xml" />
	<link>http://htmlblog.net</link>
	<description>The web sandbox of Asvin Balloo</description>
	<lastBuildDate>Tue, 09 Nov 2010 11:39:27 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.5</generator>
		<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&#8217;ll show you how to map the visitor visually on the world map using Google Maps. You must sign up for a Google Maps API key if you [...]]]></description>
			<content:encoded><![CDATA[<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&#8217;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><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&#8217;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="brush: xhtml">
&lt;script src=&quot;http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=YOURKEY&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
</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="brush: xhtml">
&lt;div id=&quot;map&quot; style=&quot;width:630px;height:350px;&quot;&gt;&lt;/div&gt;
</pre>
<p>Till now the overall code for the page, including the first part, is :</p>
<pre class="brush: php">
&lt;?php
	require_once(&quot;Net/GeoIP.php&quot;);

	if (isset($HTTP_SERVER_VARS[&#039;HTTP_X_FORWARDED_FOR&#039;]) &amp;&amp; eregi(&quot;^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$&quot;, 

$HTTP_SERVER_VARS[&#039;HTTP_X_FORWARDED_FOR&#039;])){
		$ip = $HTTP_SERVER_VARS[&#039;HTTP_X_FORWARDED_FOR&#039;];
	}
	else{
		$ip = getenv(&quot;REMOTE_ADDR&quot;);
	}

	$geoip = Net_GeoIP::getInstance(&quot;./data/GeoIP.dat&quot;);

	$country = $geoip-&gt;lookupCountryName($ip);
?&gt;

&lt;html&gt;
	&lt;head&gt;
		&lt;title&gt;Google maps&lt;/title&gt;
		&lt;script src=&quot;http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=YOURKEY&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;
	&lt;/head&gt;
	&lt;body&gt;
		&lt;div id=&quot;map&quot; style=&quot;width:630px;height:350px;&quot;&gt;&lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>Now begins the cool stuff, I&#8217;ll show you the complete javascript code and then explain afterwards.</p>
<pre class="brush: javascript">
var locator = {
	addAddressToMap: function(response) {
		locator.map.clearOverlays();
		if (!response || response.Status.code != 200) {
			//alert(&quot;Sorry, we were unable to geocode that address&quot;);
		}
		else {
			place = response.Placemark[0];
			point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
			marker = new GMarker(point);
			locator.map.addOverlay(marker);
			marker.openInfoWindowHtml(place.address);
		}
	},

	load: function() {
		if (GBrowserIsCompatible()) {
			locator.map = new GMap2(document.getElementById(&quot;map&quot;));
        		locator.map.setCenter(new GLatLng(34, 0), 1);
        		locator.geocoder = new GClientGeocoder();
        		locator.geocoder.getLocations(&quot;&lt;?php echo $country; ?&gt;&quot;, locator.addAddressToMap);
      		}
    	}
}
</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>
<h3>The load function</h3>
<p>The load function will be called upon loading the page, it will initialize our map, and center it.</p>
<pre class="brush: javascript">
locator.map = new GMap2(document.getElementById(&quot;map&quot;));
locator.map.setCenter(new GLatLng(34, 0), 1);
</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>
<h3>The addAddressToMap function</h3>
<p>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&#8217;ve placed the response.Placemark[0] in a variable called place :</p>
<pre class="brush: javascript">
place = response.Placemark[0];
</pre>
<p>, thus the coordinates can be accessed via :</p>
<pre class="brush: javascript">
place.Point.coordinates[1]
place.Point.coordinates[0]
</pre>
<p>With these 2 coordinates we create a marker which we&#8217;ll plot on the map :</p>
<pre class="brush: javascript">
point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
marker = new GMarker(point);
locator.map.addOverlay(marker);
</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="brush: javascript">
marker.openInfoWindowHtml(place.address);
</pre>
<p>our final PHP page looks like that :</p>
<pre class="brush: php">
&lt;?php
	require_once(&quot;Net/GeoIP.php&quot;);

	if (isset($HTTP_SERVER_VARS[&#039;HTTP_X_FORWARDED_FOR&#039;]) &amp;&amp; eregi(&quot;^[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}$&quot;, 

$HTTP_SERVER_VARS[&#039;HTTP_X_FORWARDED_FOR&#039;])){
		$ip = $HTTP_SERVER_VARS[&#039;HTTP_X_FORWARDED_FOR&#039;];
	}
	else{
		$ip = getenv(&quot;REMOTE_ADDR&quot;);
	}

	$geoip = Net_GeoIP::getInstance(&quot;./data/GeoIP.dat&quot;);

	$country = $geoip-&gt;lookupCountryName($ip);

?&gt;

&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;&gt;
&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot; xml:lang=&quot;fr&quot; lang=&quot;fr&quot;&gt;
	&lt;head&gt;
		&lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=utf-8&quot;/&gt;
		&lt;title&gt;my ip address with Google Maps&lt;/title&gt;
		&lt;script src=&quot;http://maps.google.com/maps?file=api&amp;amp;v=2&amp;amp;key=YOURKEY&quot; type=&quot;text/javascript&quot;&gt;&lt;/script&gt;

		&lt;script type=&quot;text/javascript&quot;&gt;
    		//&lt;![CDATA[

    		var locator = {
    			addAddressToMap: function(response) {
				locator.map.clearOverlays();
				if (!response || response.Status.code != 200) {
					//alert(&quot;Sorry, we were unable to geocode that address&quot;);
				}
				else {
					place = response.Placemark[0];
					point = new GLatLng(place.Point.coordinates[1], place.Point.coordinates[0]);
					marker = new GMarker(point);
					locator.map.addOverlay(marker);
					marker.openInfoWindowHtml(place.address);
				}
			},

			load: function() {
				if (GBrowserIsCompatible()) {
					locator.map = new GMap2(document.getElementById(&quot;map&quot;));
        				locator.map.setCenter(new GLatLng(34, 0), 1);
        				locator.geocoder = new GClientGeocoder();
        				locator.geocoder.getLocations(&quot;&lt;?php echo $country; ?&gt;&quot;, locator.addAddressToMap);
				}
      			}
    		}

    		//]]&gt;
		&lt;/script&gt;
	&lt;/head&gt;
	&lt;body onload=&quot;locator.load();&quot; onunload=&quot;GUnload();&quot;&gt;
		&lt;div id=&quot;map&quot; style=&quot;width:630px;height:350px;&quot;&gt;&lt;/div&gt;
	&lt;/body&gt;
&lt;/html&gt;
</pre>
<p>After the page has been loaded, we just call the <strong>locator.load()</strong> function which will initialize the whole process.</p>
]]></content:encoded>
			<wfw:commentRss>http://htmlblog.net/geolocate-your-visitors-with-php-part-2/feed/</wfw:commentRss>
		<slash:comments>15</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&#8217;ll show you how to geolocate your visitors with PHP. First of all let&#8217;s have a look at what you&#8217;ll need : PEAR GeoIP MaxMind GeoLite Country Installing PEAR GeoIP Using your console or using the command prompt type : pear install http://download.pear.php.net/package/Net_GeoIP-1.0.0RC1.tgz Obtaining MaxMind GeoLite Country The database can be [...]]]></description>
			<content:encoded><![CDATA[<p>In this short post I&#8217;ll show you how to geolocate your visitors with <a href="http://php.net">PHP</a>. First of all let&#8217;s have a look at what you&#8217;ll need :</p>
<ul>
<li>PEAR GeoIP</li>
<li>MaxMind GeoLite Country</li>
</ul>
<p><span id="more-19"></span></p>
<h3>Installing PEAR GeoIP</h3>
<p>Using your console or using the command prompt type :</p>
<pre class="brush: bash">
pear install http://download.pear.php.net/package/Net_GeoIP-1.0.0RC1.tgz
</pre>
<h3>Obtaining MaxMind GeoLite Country</h3>
<p>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--></p>
<h3>The code</h3>
<pre class="brush: php">
&lt;?php
	require_once(&#039;Net/GeoIP.php&#039;);

	$geoip = Net_GeoIP::getInstance(&#039;./GeoIP.dat&#039;);
	$ip = getenv(&#039;REMOTE_ADDR&#039;);
	$country = $geoip-&gt;lookupCountryName($ip);

	echo &quot;Hi, you&#039;re from &quot;.$country;
?&gt;
</pre>
<h3>Walkthrough</h3>
<p>Now let&#8217;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,  &#8220;Hi, you&#8217;re from Mauritius&#8221;. That&#8217;s it, now you know where your visitor&#8217;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&#8217;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>17</slash:comments>
		</item>
	</channel>
</rss>

