Get Longitude and Latitude For Any City, State, Zip or Street Using Yahoo Maps Services
Worked with Google Maps, or Street? Any Geo services require longitude and latitude to show exact location you’re looking for. But what if you don’t have such information, but ZIP or street names? This post will teach you how to easily get longitude and latitude from different information.
See, now I again use PEAR package for RESTful connections to web services, so go ahead and require packages with exact order:
require_once($_SERVER['DOCUMENT_ROOT']."/PEAR.php"); require_once($_SERVER['DOCUMENT_ROOT'].'/Net/URL2.php'); require_once($_SERVER['DOCUMENT_ROOT']."/HTTP/Request2.php"); ?>
If you don’t have it, no time for search hassling, download them here.
Alright now, where are we getting this info from? Quick answer – Yahoo Maps Service. So, connect to them:
$request = new HTTP_Request2( 'http://local.yahooapis.com/MapsService/V1/geocode' );
I’m not going in details here what is what in this URL since this is quite not important. Next, we pass our data through POST:
$request->setMethod(HTTP_Request2::METHOD_POST) ->addPostParameter(array( 'appid' => $appKey, 'street'=>'Road to Nowhere 33', 'city'=>'Gotham', 'state'=>'hmm..', 'zip'=>'if have it' )); $response = $request->send()->getBody(); $xml = simplexml_load_string($response);
Here, you must have you application key from Yahoo developer network. Rest of parameters are self explanatory… After the request, we get response from PEAR’s getBody method. This service returns XML which contains our longitude and latitude, for future use (e.g. Google Streets?).
$myLat = $xml->Result->Latitude; $myLng = $xml->Result->Longitude;
There, no problems occurred. If you meet some, post comments below…
Share this story:
Leave a Reply