Last active
May 13, 2021 17:47
-
-
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"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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"; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
La próxima versión la hago con nominatim de OSM
http://nominatim.openstreetmap.org/search?format=json&city=localidad&county=departamento&state=Provincia&country=Argentina
http://nominatim.openstreetmap.org/search?format=json&city=ciudad&county=Cantón...&state=Provincia_de...&country=Ecuador
https://nominatim.openstreetmap.org/search?format=json&q=R%C3%ADo%20Ceballos,%20C%C3%B3rdoba