Skip to content

Instantly share code, notes, and snippets.

@wescleymatos
Created July 31, 2013 15:32
Show Gist options
  • Save wescleymatos/6123042 to your computer and use it in GitHub Desktop.
Save wescleymatos/6123042 to your computer and use it in GitHub Desktop.
Obtém a latitude e longitude direto do google mapa a partir de um banco de dados de cidades.
<?php
header('Content-Type: text/html; charset=utf-8');
//conexão com o servidor
$conect = mysql_connect("host", "user", "password");
// Caso a conexão seja reprovada, exibe na tela uma mensagem de erro
if (!$conect) die ("<h1>Falha na coneco com o Banco de Dados!</h1>");
// Caso a conexão seja aprovada, então conecta o Banco de Dados.
$db = mysql_select_db("db");
$sql = "SELECT * FROM rn";
$result = mysql_query($sql, $conect);
$i = 1;
while($consulta = mysql_fetch_array($result)) {
$cityclean = str_replace (" ", "+", $consulta["nome_cidade"]);
$details_url = "http://maps.googleapis.com/maps/api/geocode/json?address=" . utf8_encode($cityclean) . "+-+RN&sensor=false";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $details_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$geoloc = json_decode(curl_exec($ch), true);
curl_close($ch);
$lat = $geoloc["results"][0]["geometry"]["location"]['lat'];
$lng = $geoloc["results"][0]["geometry"]["location"]['lng'];
$id = $consulta["id"];
$query = mysql_query("UPDATE rn SET lat='$lat', lng='$lng' WHERE id='$id' ") or die(mysql_error());
echo $i . "<br>";
$i++;
}
mysql_free_result($result);
mysql_close($conecta);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment