<?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>ODM Technology &#187; api</title>
	<atom:link href="http://blog.odmtech.com/tag/api/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.odmtech.com</link>
	<description>Ord's Blog</description>
	<lastBuildDate>Sat, 04 Jul 2009 15:22:00 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Geocoding in PHP with Google&#8217;s API</title>
		<link>http://blog.odmtech.com/2009/02/23/geocoding-in-php-with-googles-api/</link>
		<comments>http://blog.odmtech.com/2009/02/23/geocoding-in-php-with-googles-api/#comments</comments>
		<pubDate>Tue, 24 Feb 2009 04:54:14 +0000</pubDate>
		<dc:creator>Ord</dc:creator>
				<category><![CDATA[OBID]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[geocode]]></category>
		<category><![CDATA[google]]></category>
		<category><![CDATA[map]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://blog.odmtech.com/?p=30</guid>
		<description><![CDATA[Today&#8217;s project was geocoding entries in a database of businesses, where we have the street address, city and postal code, but need latitude and longitude for mapping purposes.  There are a number of web sites that will perform this service for a fee, but it can also be done without charge using either the Google [...]]]></description>
			<content:encoded><![CDATA[<p>Today&#8217;s project was geocoding entries in a database of businesses, where we have the street address, city and postal code, but need latitude and longitude for mapping purposes.  There are a number of web sites that will perform this service for a fee, but it can also be done without charge using either the <a href="http://code.google.com/apis/maps/">Google Maps API</a> or <a href="http://dev.live.com/VirtualEarth/">Microsoft Virtual Earth API</a>.  Both of these APIs limit the number of queries per day which can be looked up without charge.   Also, both have terms which put some limitations on what can be done with the data, the Mircosoft one being much more restrictive.</p>
<p>Since the Google API is easier to use, I started with that one.  The function is written in PHP so that it will be easy to call in in response to searches from a web site if need be.  The function presented here is only using Google&#8217;s map API, it will be expanded to add an the Virtual Earth version to it, so that the caller can decide which service to use.</p>
<p>In my case, I only want to do lookups on records that actually have street addresses.  Records that only specify a city, or maybe a PO box I am not interested in getting coordinates for, so that is why the test for record['street'] is there.  Google API allows a country bias value &#8211; this helps to resolve ambiguities in the address.  This value is the top level domain for the country, which is <em>usually</em> the country code, but not always.  Here I just want to make sure that Canadian records are recognized, so the test.  If this routine needs to deal with all countries, we would need to look up a table converting country codes to tlds, or else just assign the country code to the gl value and put in tests for the exceptions.  The sensor=false tells the API that this request is not coming from a device that can determine it&#8217;s own position.</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">define</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;GOOGLE_KEY&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;yourapikeyforgoogle&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// geocode the supplied record using $service</span>
<span style="color: #000000; font-weight: bold;">function</span> geoCode<span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #339933;">,</span><span style="color: #000088;">$service</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Google&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$service</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;Google&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'street'</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$address</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'street'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$address</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot; ,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'city'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$address</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot; ,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'state'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$address</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot; ,&quot;</span><span style="color: #339933;">.</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'postalcode'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$url</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: #990000;">urlencode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$address</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$url</span><span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&amp;amp;output=csv&amp;amp;oe=utf8&amp;amp;sensor=false&amp;amp;key=&quot;</span><span style="color: #339933;">.</span>GOOGLE_KEY<span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'countrycode'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;CA&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #000088;">$url</span> <span style="color: #339933;">.=</span> <span style="color: #0000ff;">&quot;&amp;amp;gl=ca&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <span style="color: #990000;">file_get_contents</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$url</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #000088;">$parts</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;">$result</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <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: #000088;">$parts</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$status</span><span style="color: #339933;">==</span><span style="color: #0000ff;">&quot;200&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'latitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$latitude</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'longitude'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span> <span style="color: #000088;">$longitude</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'geo-accuracy'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #000088;">$accuracy</span><span style="color: #339933;">;</span>
                <span style="color: #000088;">$record</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'geo-service'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;Google&quot;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$record</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Google allows a maximum of 15000 queries per day from a single IP.  To code my values, I use a cron job that runs once per minute and looks up 10 at a time, with a 1 second interval between each.  That gives 14400 per day, slightly below the limit.</p>
<p>For either Google MAP or Microsoft VE API&#8217;s you will need a key. Google will give you one instantly when you <a href="http://code.google.com/apis/maps/signup.html">sign up</a>, and Microsof will let you request an <a href="https://mappoint-css.live.com/MwsSignup/">Evaluation Developer Acount </a>- a slightly longer process.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.odmtech.com/2009/02/23/geocoding-in-php-with-googles-api/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
