<?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>designing4u.de &#187; latitude</title>
	<atom:link href="http://www.designing4u.de/tag/latitude/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.designing4u.de</link>
	<description>Yet Another Coding Blog</description>
	<lastBuildDate>Fri, 29 Jul 2011 08:11:18 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Geolocation class &#8211; retrieving longitude and latitude using google maps</title>
		<link>http://www.designing4u.de/2008/05/geolocation-class-retrieving-longitude-and-latitude-using-google-maps/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=geolocation-class-retrieving-longitude-and-latitude-using-google-maps</link>
		<comments>http://www.designing4u.de/2008/05/geolocation-class-retrieving-longitude-and-latitude-using-google-maps/#comments</comments>
		<pubDate>Tue, 06 May 2008 10:58:43 +0000</pubDate>
		<dc:creator>Wojtek</dc:creator>
				<category><![CDATA[Google Maps]]></category>
		<category><![CDATA[OOP PHP]]></category>
		<category><![CDATA[geocoding]]></category>
		<category><![CDATA[Geolocation]]></category>
		<category><![CDATA[latitude]]></category>
		<category><![CDATA[longitude]]></category>

		<guid isPermaLink="false">http://www.designing4u.de/?p=7</guid>
		<description><![CDATA[A while ago I had to prepare a map for a customer. He wanted to be able to use text files with addresses and display them on the map. To be able to do that I had to find a service, which will convert a regular address into geographical longitude and latitude. What would be [...]]]></description>
			<content:encoded><![CDATA[<p>A while ago I had to prepare a map for a customer. He wanted to be able to use text files with addresses and display them on the map. To be able to do that I had to find a service, which will convert a regular address into geographical longitude and latitude. What would be better than google maps to do that really simple task? I researched a little bit on google and found a really nice way, how to accomplish that.<br />
<span id="more-7"></span><br />
On Tim Shower's homepage I have found a really nice <a href="http://www.timshowers.com/2008/08/php-geocoding-tutorial-with-the-google-maps-api-part-one/" target="_blank">php geocoding tutorial</a>, which I used to build my solution. For our purposes we will use client for URL (cURL) to retrieve the longitude and latitude of our places. Let's define our class first.<br />
[source:php]<br />
class Geolocation {</p>
<p>	private $url = array();</p>
<p>}<br />
[/source]<br />
We define private array url, in which we will store URLs prepared for cURL request. Let's build a contructor.<br />
[source:php]<br />
class Geolocation {</p>
<p>	private $url = array();</p>
<p>	function __construct($file) {<br />
		$gmapkey = "YOURKEY";<br />
		$address = file($file);<br />
		foreach($address as $addres) {<br />
			$this->url[] = "http://maps.google.com/maps/geo?q=".urlencode($addres)."&#038;output=csv&#038;key=".$gmapkey;<br />
		}<br />
	}<br />
}<br />
[/source]<br />
You need to replace $gmapkey with the key you can sign for at google. Our __constructor reads the text file and saves each line in an array. We then loop through this array and initialize URLs.</p>
<p>In the next method we will loop through the addresses to retrieve the geolocation.</p>
<p>[source:php]<br />
class Geolocation {</p>
<p>	private $url = array();</p>
<p>	function __construct($file) {<br />
		$gmapkey = "YOURKEY";<br />
		$address = file($file);<br />
		foreach($address as $addres) {<br />
			$this->url[] = "http://maps.google.com/maps/geo?q=".urlencode($addres)."&#038;output=csv&#038;key=".$gmapkey;<br />
		}<br />
	}<br />
	public function getLatLng() {<br />
		$i=1;<br />
		foreach($this->url as $urls) {<br />
			$cinit = curl_init();<br />
			curl_setopt($cinit, CURLOPT_URL, $urls);<br />
			curl_setopt($cinit, CURLOPT_HEADER,0);<br />
			curl_setopt($cinit, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);<br />
			curl_setopt($cinit, CURLOPT_FOLLOWLOCATION, 1);<br />
			curl_setopt($cinit, CURLOPT_RETURNTRANSFER, 1);<br />
			$response = curl_exec($cinit);<br />
			curl_close($cinit);</p>
<p>			if(strstr($data,"200")) {<br />
				$data = explode(",",$data);<br />
				echo $i.": ";<br />
				$zoom = $data[1];<br />
				echo $lat = $data[2];<br />
				echo $lon = $data[3]."<br />";<br />
			}</p>
<p>			/*<br />
			$sql = "INSERT INTO..."<br />
			$res = mysql_query($sql);<br />
			echo ($res) ? $i.": lat:".$latitude." lon: ".$longitdue." -- OK<br />" : $i.": ERROR<br />";<br />
			*/<br />
			$i++;<br />
		}<br />
	}<br />
}<br />
[/source]<br />
You can either echo the results or just save them in your MySQL database. You can also make multidimensional array and save some extra information about the certain place. </p>
<p>Let's take a look at how you should use this class.<br />
[source:php]<br />
include('geolocation.class.php');<br />
$geo = new Geolocation('address.txt');<br />
$geo->getLatLng();<br />
[/source]<br />
Basically you need to include the class, initialize it with the name of the file you want to use and getLatLng will do the magic for you. This class is pretty old and you may encounter one problem using it. After 10th request I started to receive the same geolocation for different places. What you can do is to put the foreach loop to sleep for 2 seconds and it should be fine. That's why I actually use iterator in this function. You basically need to add this line right after the loop starts.<br />
[source:php]<br />
if($i%10 == 0) sleep(2);<br />
[/source]</p>
<p>Here is the content of the text file:<br />
[source:html]<br />
Berlin, Dieffenbachstr. 38<br />
Berlin, Cuvrystr. 32<br />
Berlin, Lasdehner Str. 30<br />
[/source]<br />
Basically one address per line. That would be all, have fun with playing and extending this class.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.designing4u.de/2008/05/geolocation-class-retrieving-longitude-and-latitude-using-google-maps/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
	</channel>
</rss>

