I spend a few days searching on the web to get a good geocode database
and all my efforts proved to be in vain because there were a number of sites
that was providing me with a geocoding address database at an expensive cost
of $30-$100 . The cost was huge for me as this was only a supplement to my
full project . Luckily for me and for you while researching on this topics I came
across a number of viable options that could fit my need with a little bit of hack.
This were the three mapping giants Google Maps , Yahoo Maps & Bing Maps,
each of had their geocoding services that could be accessed via an API and this
exactly fitted my needs since I could store this data onto my database although
it is again the policies , you can still use it without a problem for small and big
projects within each of the MAP API's query limit .
Research on the Google,Yahoo and Bing Maps
So I started my testing the various Geocoding values returned by the variousAPI. I started out with Google Maps . Well the results of geocoding of the Google
Maps was highly accurate both for locations in and outside the US. The
Google Maps scaled well in terms of both accuracy as well as granularity of
results, in short it was giving out accurate geocoding results for even small towns .
The next test was on Bing Maps but sorry to say Bing Maps accuracy was best
till the city level , most of the Geocoding Address results was restricted to
only City Level results .The next and final research was done on Yahoo Maps
and happy to say Yahoo Maps Address Geocoding results was too accurate and
results between Yahoo and Google Maps could actually be mapped with each
other .
Difference between Google Maps and Yahoo Maps ?
Each one has its own pros and cons and telling which ome is better is tough job .However the key differences I have found out while comparing the two API's
- Graphically Google Maps has superior looks and gives a good
experience to the end user than the Yahoo Maps . - Loading time of Yahoo Maps is less than that of Google Maps.
- Geocoding Rate of Google Maps is about 2000 queries a day/server .
- Geocoding Rate of Yahoo Maps is around 50,000 queries a day .
- In Google Maps you can take the benefits of the Google Driving Directions
API but Yahoo doesn't provide such a facility to use its Driving Directions
in an API even though it does have one integrated in its Yahoo Maps website.
Creating the Geocode Address Database
With Yahoo providing me an Geocoding Address limit of 50,000 queries and its
Geocoding addressing being accurate enough , I decided to use the Yahoo Maps API.
So lets start the coding part .The code for Yahoo Geocoding is as follows
Geocode.php
<?php // Json encode the data function json_code ($json) { $json = substr($json, strpos($json,'{')+1, strlen($json)); $json = substr($json, 0, strrpos($json,'}')); $json=preg_replace('/(^|,)([\\s\\t]*)([^:]*) (([\\s\\t]*)):(([\\s\\t]*))/s','$1"$3"$4:',trim($json)); return json_decode('{'.$json.'}', true); } // function to get the geocode of the address function geoCodeYp($point) { // Replace your Yahoo AppID here $yahoo_appid = 'Your yahoo app id'; //URLEncode for eg convert space to %20 . $pointenc = urlencode($point); // URL Formation that will fetch you the results on query $url="http://where.yahooapis.com/geocode?location=".$pointenc."&gflags=R&appid=".$yahoo_appid."&flags=J"; // get the contents of the URL formed $jsondata = file_get_contents($url); $json_data= '{ a: 1, b: 245, c with whitespaces: "test me", d: "function () { echo \"test\" }", e: 5.66 }'; $coord=explode(" ",$point); // this will json encode the data . $convertedtoarray=$this--->json_code($jsondata); // line1 of addrress comprising of house,street no etc // line 2 of address comprising of city state country $line1 =$convertedtoarray['ResultSet']['Results']['0']['line1'] ; $line2 =$convertedtoarray['ResultSet']['Results']['0']['line1'] ; $county =$convertedtoarray['ResultSet']['Results']['0']['county'] ; $street =$convertedtoarray['ResultSet']['Results']['0']['street'] ; if(($line1=="")||($line2=="")||($county=="")||($street=="")) { $yahooresults['status']="noresult"; } else { $countrycode=$convertedtoarray['ResultSet']['Results']['0']['countrycode'] ; $statecode=$convertedtoarray['ResultSet']['Results']['0']['statecode'] ; $city=$convertedtoarray['ResultSet']['Results']['0']['city'] ; $house=$convertedtoarray['ResultSet']['Results']['0']['house'] ; $latitude=$convertedtoarray['ResultSet']['Results']['0']['latitude'] ; $longitude=$convertedtoarray['ResultSet']['Results']['0']['longitude'] ; $yahooresults = array('countrycode'=>$countrycode,'statecode'=>$statecode,'county'=>$county,'city'=>$city,'street'=>$street,'house'=>$house,'latitude'=>$latitude,'longitude'=>$longitude); } return $yahooresults ; } ?>
To increase modularity & code reusability I would advice you to keep this page
separate . So How do I use this php code?
Create another php page in which you will be using this geocoding functions
using the php include page command .
1)Suppose you want to get the geocode address of a place say "CHICAGO" .
You can simply call the function as follows
geoCodeYp("CHICAGO") ;
Now for storing this result into the database you will have to capture the
result like this
$result=geoCodeYp("Chicago") ; // get each of the values as $latitude=$result['latitude']; // Get the Latitude $longitude=$result['longitude']; // Get the Longitude
.............Remaining Variables..................
You can store each of the results in database by retrieving
the remaining values as $result['valuename']; . The valuenames
are city,countrycode,state,street,house etc.
2) To geocode address of a point(latitude&longitude)
//Example Point with latitude 72.5632 & longitude 19.756
$latitude="72.5632"; $longitude="19.756"; // You have to include space between the latitude & longitude $point=$latitude." ".$longitude ; //call the geocode function geocode($point);
Time Required to setup Geocode Address Database
Suppose you have to want to geocode the address of around 5,00,000 address
1 yahoo appid -> 10 days 2 yahoo appid -> 5 days 10 yahoo appid -> 1 dayAnd best thing is that you can do this from a single server or from your pc
with php installed .Tell me if you have problems implementing it . I haven't
included the steps to insert the values into database & retrieve from it ,
well that is what Google is for!!!
Do Check out all the Google Maps category on this website!!
2 comments
Thanks for the great article. I am getting an error running your code. Here is the PHP error:
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /localhost/html/yahoo/yahoo-geocode.php on line 41
Here is Line #41:
$convertedtoarray=$this--->json_code($jsondata);
Any idea why I am getting this error? I am assuming the "--->" is what's causing it. pls advise
Thanks for sharing such an informative post. There is always a useful and accurate way to use Google Maps without distorting web pages.