• Get Longitude and Latitude For Any City, State, Zip or Street Using Yahoo Maps Services

    Posted by markzero on Wednesday, January 26th, 2011

    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:


    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(

    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:

       'appid' => $appKey,
       'street'=>'Road to Nowhere 33',
       '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…

    Posted in: PEAR, PHP.

    Share this story:

    Leave a Reply