Skip to content

Instantly share code, notes, and snippets.

@aaizemberg
Last active May 13, 2021 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aaizemberg/5236914 to your computer and use it in GitHub Desktop.
Save aaizemberg/5236914 to your computer and use it in GitHub Desktop.
Geocodificando un archivo de texto, usando GoogleMaps desde PHP. Ejecutar "php -f geocoding.php"
<?php
function clean($string)
{
$string = strtr($string,"áéíóúÁÉÍÓÚñÑ.","aeiouAEIOUnN ");
// Remove all remaining other unknown characters
// $string = preg_replace('/[^a-zA-Z0-9\-]/', ' ', $string);
// $string = preg_replace('/^[\-]+/', '', $string);
// $string = preg_replace('/[\-]+$/', '', $string);
// $string = preg_replace('/[\-]{2,}/', ' ', $string);
return $string;
}
/* archivo.txt
Catamarca 323, Tucuman, Argentina
Balcarce 50, CABA, Argentina
Avenida Paseo Colón 828, Ciudad Autonoma de Buenos Aires, Argentina
...
*/
$lines = file('archivo.txt');
foreach ($lines as $line_num => $line) {
$direccion = clean(strtoupper($line));
$url = urlencode("http://maps.google.com/maps/api/geocode/xml?address='" . $direccion . "'&sensor=false");
$xml = simplexml_load_file($url);
if ( $xml->status == 'OK' ) {
if ( count($xml->result) == 1 ) {
print $xml->status . "\t" . $xml->result->formatted_address . "\t" . $xml->result->geometry->location->lat . "\t" . $xml->result->geometry->location->lng . "\n";
}
else {
print count($xml->result) . "\t" . $xml->result->formatted_address . "\t" . $xml->result->geometry->location->lat . "\t" . $xml->result->geometry->location->lng . "\n";
}
}
else {
print $xml->status . "\n";
}
}
?>