views:

26

answers:

1

Hey,

I have an entity that one of it's members is the actual city, country that it was "created" - the user only gives me lat, long coordinates and I need to use the google API to reverse geocode the coordinates into city, country.

My question - What is the best way to fetch that information? is it safe enough to use a 3rd party web service in the middle of a record creating process? What are the common ways to do that?

+1  A: 

It's certainly safe enough. The only issue is response time (as you will be calling the remote web service synchronously), but if you're doing this once only on each insert I don't think that would be much of a problem.

The Google Geocoding API will return results in XML format, so you just need to call the web service URL and pull the information you need from the response.

Here's an example reverse geocoding result:

http://maps.googleapis.com/maps/api/geocode/xml?latlng=40.714224,-73.961452&sensor=false

You don't say which language you're using, but I'm assuming PHP—here's a very basic example of parsing the response and displaying the addresses using SimpleXML:

$lat = 40.714224;
$lng = -73.961452;

$xml = simplexml_load_file("http://maps.googleapis.com/maps/api/geocode/xml?latlng=" . $lat . "," . $lng . "&sensor=false");

foreach($xml->result as $result)
{
    foreach($result->address_component as $addresscomponent)
    {
        echo $addresscomponent->long_name . ' ';
    }

    echo '<br />';
}
Mark B