<?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>HavlenaBlog &#187; gps</title>
	<atom:link href="http://www.havlena.net/blog/en/tag/gps/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.havlena.net/blog</link>
	<description>... o všem a všelijak</description>
	<lastBuildDate>Wed, 26 May 2010 00:18:27 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.6</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>PHP/Google Maps: How to get GPS coordinates for address</title>
		<link>http://www.havlena.net/blog/en/php-a-google-maps-jak-ziskat-gps-souradnice-pro-adresu/</link>
		<comments>http://www.havlena.net/blog/en/php-a-google-maps-jak-ziskat-gps-souradnice-pro-adresu/#comments</comments>
		<pubDate>Thu, 17 Sep 2009 22:43:39 +0000</pubDate>
		<dc:creator>Matouš Havlena</dc:creator>
				<category><![CDATA[PHP 5]]></category>
		<category><![CDATA[Tips and tricks]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[geocoding]]></category>
		<category><![CDATA[google maps]]></category>
		<category><![CDATA[gps]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://www.havlena.net/blog/?p=807</guid>
		<description><![CDATA[This article contains code example of searching and saving GPS coordinates for addresses stored in the database. For this purpose we will use Google Maps API and Geocoding Service (Geocoding is process of converting addresses into geographic coordinates).
The geocoding service may only be used in conjunction with displaying results on a Google map.
Geocoding Service
Geocoding Service [...]]]></description>
			<content:encoded><![CDATA[<p>This article contains <strong>code example of searching and saving GPS coordinates for addresses stored in the database</strong>. For this purpose we will use <a href="http://code.google.com/apis/maps/">Google Maps API</a> and <strong><a href="http://code.google.com/apis/maps/documentation/geocoding/">Geocoding Service</a></strong> (Geocoding is process of converting addresses into geographic coordinates).<img title="More..." src="http://www.havlena.net/blog/wp-includes/js/tinymce/plugins/wordpress/img/trans.gif" alt="" /><span id="more-807"></span></p>
<p>The geocoding service may only be used in conjunction with displaying results on a Google map.</p>
<h3>Geocoding Service</h3>
<p>Geocoding Service provides way to get geographic coordinates via an HTTP request. We must send a request to <a href="http://maps.google.com/maps/geo?">http://maps.google.com/maps/geo?</a> with several parameteres. We are interested in these parameters:</p>
<ul>
<li><code class="codecolorer text default"><span class="text">q</span></code> (required) &#8211; the address to which you want get coordinates</li>
<li><code class="codecolorer text default"><span class="text">key</span></code> (required) &#8211; your API key (you can obtain it <a href="http://code.google.com/apis/maps/">here</a>)</li>
<li><code class="codecolorer text default"><span class="text">sensor</span></code> (required) &#8211; <code class="codecolorer text default"><span class="text">true</span></code> if the request is sent from a device with location sensor, otherwise <code class="codecolorer text default"><span class="text">false</span></code></li>
<li><code class="codecolorer text default"><span class="text">output</span></code> (required) &#8211; the format of output (the options are <code class="codecolorer text default"><span class="text">xml</span></code>, <code class="codecolorer text default"><span class="text">kml</span></code>, <code class="codecolorer text default"><span class="text">csv</span></code> or <code class="codecolorer text default"><span class="text">json</span></code>)</li>
<li><code class="codecolorer text default"><span class="text">oe</span></code> (optional) &#8211; output encoding (It&#8217;s recommended set this parameter to <code class="codecolorer text default"><span class="text">utf8</span></code>)</li>
<li><a href="http://code.google.com/intl/cs-CZ/apis/maps/documentation/geocoding/#GeocodingRequests">&#8230;a další</a></li>
</ul>
<p>The input data should be encoded in <code class="codecolorer text default"><span class="text">utf-8</span></code>.</p>
<h4>CSV output format</h4>
<p>Output data can be generated in these formats:</p>
<ul>
<li><code class="codecolorer text default"><span class="text">json</span></code> (default)</li>
<li><code class="codecolorer text default"><span class="text">kml</span></code></li>
<li><code class="codecolorer text default"><span class="text">xml</span></code></li>
<li><code class="codecolorer text default"><span class="text">csv</span></code> &#8211; data are separated by semi-colon</li>
</ul>
<p>In our situation is CSV ideal because it returns only 4 blocks of data separated by semi-colon:</p>
<ol>
<li><strong>HTTP status code</strong>
<ul>
<li><code class="codecolorer text default"><span class="text">200</span></code><strong> </strong>- succes</li>
<li><code class="codecolorer text default"><span class="text">500</span></code> &#8211; server error</li>
<li><code class="codecolorer text default"><span class="text">602</span></code> &#8211; unknown address</li>
<li><code class="codecolorer text default"><span class="text">610</span></code> &#8211; bad API key</li>
<li><code class="codecolorer text default"><span class="text">620</span></code> &#8211; too many queries (&gt;5000 per day or too many requests in too short a period of time)</li>
<li>a další&#8230;</li>
</ul>
</li>
<li><strong>Accuracy</strong>
<ul>
<li><code class="codecolorer text default"><span class="text">0</span></code> &#8211; unknown</li>
<li><code class="codecolorer text default"><span class="text">1</span></code> &#8211; country level</li>
<li>&#8230;</li>
<li><code class="codecolorer text default"><span class="text">4</span></code> &#8211; city (village) leel</li>
<li><code class="codecolorer text default"><span class="text">5</span></code> &#8211; ZIP level</li>
<li><code class="codecolorer text default"><span class="text">6</span></code> -street level</li>
<li><code class="codecolorer text default"><span class="text">7</span></code> &#8211; intersection level</li>
<li><code class="codecolorer text default"><span class="text">8</span></code> &#8211; address level</li>
<li><code class="codecolorer text default"><span class="text">9</span></code> &#8211; building level</li>
</ul>
</li>
<li><strong>Latitude</strong></li>
<li><strong>Longitude</strong></li>
</ol>
<p>For example:</p>
<div class="codecolorer-container text default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">200,6,42.730070,-73.90570</div></div>
<h3>PHP: example of getting GPS coordinates from CSV file</h3>
<p>We assume having table in db with buildings&#8217; addresses to which we can find their GPS coordinates. The table could look like this:</p>
<div class="codecolorer-container mysql default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><div class="mysql codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #990099; font-weight: bold;">CREATE</span>  <span style="color: #990099; font-weight: bold;">TABLE</span> <span style="color: #990099; font-weight: bold;">IF <span style="color: #CC0099; font-weight: bold;">NOT</span> EXISTS</span> <span style="color: #008000;">`buildings`</span> <span style="color: #FF00FF;">&#40;</span><br />
<span style="color: #008000;">`building<span style="color: #008080; font-weight: bold;">_</span>id`</span> <span style="color: #999900; font-weight: bold;">INT</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #FF9900; font-weight: bold;">AUTO_INCREMENT</span> <span style="color: #000033;">,</span><br />
<span style="color: #008000;">`building<span style="color: #008080; font-weight: bold;">_</span>street`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span>200<span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #000033;">,</span><br />
<span style="color: #008000;">`building<span style="color: #008080; font-weight: bold;">_</span>street<span style="color: #008080; font-weight: bold;">_</span>nr`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span>20<span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #000033;">,</span><br />
<span style="color: #008000;">`building<span style="color: #008080; font-weight: bold;">_</span>city`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span>200<span style="color: #FF00FF;">&#41;</span> <span style="color: #CC0099; font-weight: bold;">NOT</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #000033;">,</span><br />
<span style="color: #008000;">`building<span style="color: #008080; font-weight: bold;">_</span>latitude`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span>30<span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #000033;">,</span><br />
<span style="color: #008000;">`building<span style="color: #008080; font-weight: bold;">_</span>longitude`</span> <span style="color: #999900; font-weight: bold;">VARCHAR</span><span style="color: #FF00FF;">&#40;</span>30<span style="color: #FF00FF;">&#41;</span> <span style="color: #9900FF; font-weight: bold;">NULL</span> <span style="color: #000033;">,</span><br />
<span style="color: #990099; font-weight: bold;">PRIMARY KEY</span> <span style="color: #FF00FF;">&#40;</span><span style="color: #008000;">`building<span style="color: #008080; font-weight: bold;">_</span>id`</span><span style="color: #FF00FF;">&#41;</span> <span style="color: #FF00FF;">&#41;</span></div></div>
<p>Then our PHP script for filling up GPS coordinates could look like this:</p>
<pre>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border: 1px solid #9F9F9F;width:435px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br />24<br />25<br />26<br />27<br />28<br />29<br />30<br />31<br />32<br />33<br />34<br />35<br />36<br />37<br />38<br />39<br />40<br />41<br />42<br />43<br />44<br />45<br />46<br /></div></td><td><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$conn</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_connect</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;localhost&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;user&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;passwd&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could not connect: &quot;</span><span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #990000;">mysql_select_db</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;database&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; <span style="color: #990000;">die</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Could select db: &quot;</span><span style="color: #339933;">.</span> <span style="color: #990000;">mysql_error</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #666666; font-style: italic;">// YOUR DOMAIN API KEY</span><br />
<span style="color: #000088;">$api_key</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;ABCDEFGHIJK&quot;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$query</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;select * from buildings where building_latitude is null and building_longitude is null order by building_id&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_fetch_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// SET ADDRESS</span><br />
&nbsp; <span style="color: #000088;">$address</span> <span style="color: #339933;">=</span> <span style="color: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;building_street&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;building_street_nr&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; &quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;building_city&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; Czech republic&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// URL TO HTTP REQUEST</span><br />
&nbsp; <span style="color: #000088;">$link</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;http://maps.google.com/maps/geo?q=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$address</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;key=&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$api_key</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;&amp;sensor=false&amp;output=csv&amp;oe=utf8&quot;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// WE GET FILE CONTENT</span><br />
&nbsp; <span style="color: #000088;">$page</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$link</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// WE OBTAIN DATA FROM GIVEN CSV</span><br />
&nbsp; <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$status</span><span style="color: #339933;">,</span> <span style="color: #000088;">$accuracy</span><span style="color: #339933;">,</span> <span style="color: #000088;">$latitude</span><span style="color: #339933;">,</span> <span style="color: #000088;">$longitude</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">,</span> <span style="color: #000088;">$page</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// IF EVERYTHING OK AND ACCURANCY GREATER THEN 3 WE SAVE COORDINATES</span><br />
&nbsp; <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$status</span> <span style="color: #339933;">==</span> 200<span style="color: #009900;">&#41;</span> and <span style="color: #009900;">&#40;</span><span style="color: #000088;">$accuracy</span><span style="color: #339933;">&gt;=</span>4<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$query_edit</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;update buildings set building_latitude = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$latitude</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;',<br />
&nbsp; &nbsp; building_longitude = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$longitude</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'<br />
&nbsp; &nbsp; where building_id = '&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;building_id&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot;'&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #000088;">$result_edit</span> <span style="color: #339933;">=</span> <span style="color: #990000;">mysql_query</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_edit</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;building_id&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; - OK&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #b1b100;">echo</span> <span style="color: #000088;">$row</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">&quot;building_id&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">.</span><span style="color: #0000ff;">&quot; - ERROR&lt;br /&gt;&quot;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; <span style="color: #666666; font-style: italic;">// TIMER BECAUSE GOOGLE DOESN'T LIKE TO BE QUERIED IN SHORT TIME</span><br />
&nbsp; <span style="color: #990000;">sleep</span><span style="color: #009900;">&#40;</span>3<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #990000;">mysql_close</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$conn</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></td></tr></tbody></table></div>
</pre>
<p>In following article will be introduced MySQL procedure which returns list of the nearest buildings calculated from GPS.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.havlena.net/blog/en/php-a-google-maps-jak-ziskat-gps-souradnice-pro-adresu/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

