Conversión de ciudad y país en puntos de coordenadas

Is there any fairly fast php code to convert a city + country to latitude and longitude coordinates. I have a list of locations and I need to convert them to coordinates. I tried doing it in javascript, but I encountered some problems trying to get the results back to php to store it in my JSON file. So is there any efficient PHP code to do this?

Gracias.

preguntado el 09 de marzo de 12 a las 16:03

What service are you using for geo-coding? Google? Yahoo? Bing? Something else? -

1 Respuestas

In my app, I'm using the following function to geo-code locations using google service. The function takes one parameter - location to geo-code (e.g. "Boston, USA" or "SW1 1AA, United Kingdom") and returns an associative array with Lat/Lon. If an error occurs or location cannot be determined, then it returns FALSE.

Note that in many cases city + country will not be able to determine the location uniquely. For example, there are probably 100 cities named Springfield in the USA alone. Also, when passing country to a geo-coding service, make sure to put the full country name and not a 2-letter code. I found this the hard way: I was passing 'CA' for "Canada" and was getting strange results. Apparently, google assumed that 'CA' means "California".

function getGeoLocationGoogle($location)
{
    $url = "http://maps.googleapis.com/maps/api/geocode/xml?address=". urlencode($location) . "&sensor=false";
    $userAgent = "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 FirePHP/0.4";

    //Setup curl object and execute
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_USERAGENT, $userAgent);
    curl_setopt($curl, CURLOPT_FAILONERROR, true);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);

    $req = $location;

    //Process response from Google servers
    if (($error = curl_errno($curl)) > 0)
    {
        return FALSE;
    } 

    $geo_location = array();
    //Try to convert XML response into an object
    try
    {
        $xmlDoc = new DOMDocument();
        $xmlDoc->loadXML($result);
        $root = $xmlDoc->documentElement;

        //get errors
        $status = $root->getElementsByTagName("status")->item(0)->nodeValue;
        if($status != "OK")
        {
            $error_msg = "Could not determine geographical location of $location - response code $status";
        }
        $location = $root->getElementsByTagName("geometry")->item(0)->getElementsByTagName("location")->item(0);
        if(!$location)
        {
            return FALSE;
        }

        $xmlLatitude = $location->getElementsByTagName("lat")->item(0);
        $valueLatitude = $xmlLatitude->nodeValue;
        $geo_location['Latitude'] = $valueLatitude; 

        //get longitude
        $xmlLongitude = $location->getElementsByTagName("lng")->item(0);
        $valueLongitude = $xmlLongitude->nodeValue;
        $geo_location['Longitude'] = $valueLongitude;

        //return location as well - for good measure
        $geo_location['Location'] = $req;
    }
    catch (Exception $e)
    {
        return FALSE;
    }       

    return $geo_location;
}

respondido 09 mar '12, 17:03

I need to calculate the coordinates for a couple thousand locations, so is this fast enough that the page won't time out? - ewein

If you need to do this on a few thousand locations, then, most likely, your design is wrong. You should never need to do that many geocoding requests on the fly. What are you trying to achieve? - aleks g

I am creating a map of connections between locations. So I have a database where I have my locations stored and I need to convert these locations to coordinates and store the coordinates into a JSON file. - ewein

@ewein If you're doing this as a one off exercise, then you should be fine with the above approach. Just take the data where the the coordinates are not determined yet and geo-code those, saving as you go along. Even if the script times out, just re-run it and it should pick up where it left off. Also, json file may be a bad idea if you really do have thousands of places. Consider using a database instead. - aleks g

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.