Google api v3 geolocalizzazione problemini.

lelemkop

Utente Attivo
2 Lug 2013
55
0
6
Ciao a tutti rieccomi con il un piccolo problemino con il sito che sto facendo per i mie. Alcuni giorni fa sono stato aiutato con l'inserimento in un campo specifico. Tutto risolto quel problema.Però vorrei creare qualcosa di più completo. Ho adattato questo codice al mio scopo per visualizzare su google i marker dei clienti. Il problema che alcuni il sistema mi restituisce le coordinate e le inserisce nel db, altri proprio no. anche se inserendo illink che mi restituisce lo "vede". Qualcuno mi potrebbe spiegare il perché?
PHP:
<?php

$conn = mysql_connect('localhost', 'root', 'x');
if(!$conn)
 {
   echo "Database Error.";
 }
 else
 {
   echo "Connected.";
   mysql_select_db('prova', $conn);
   $query = "SELECT * from clienti";
   $result = mysql_query($query, $conn) or die($query.mysql_error());
    while($row = mysql_fetch_array($result))
    { 

      $address = $row['via'];
      $civico = $row['civico'];
	$citta = $row ['citta'];
      $id = $row['id'];

      $loc_address = str_replace(" ", "+", $address); //to replace spaces by '+' signs
          $location = $loc_address;
      echo $location."<br>";
      $delay = 0;  // this delay is here just to avoid errors - 
          $url = "http://maps.google.com/maps/api/geocode/json?address=".$location."" .$civico."".$citta."&sensor=false";
          echo $url;
            $output=file_get_contents($url);
            $out= json_decode($output);
            $lat = $out->results[0]->geometry->location->lat;
            $lng = $out->results[0]->geometry->location->lng;
            echo '<br>Lat is '. $lat;
            echo '<br>Long is '. $lng;


       $query = sprintf("UPDATE clienti " .
             " SET lat = '%s', lng = '%s' " .
             " WHERE id = '%s' LIMIT 1;",
             mysql_real_escape_string($lat),
             mysql_real_escape_string($lng),
             mysql_real_escape_string($id));
           $update_result = mysql_query($query);

      echo "Successfully inserted.";
    // Below is just for reference - they say we need the delay. The may problem: does not get the status to compare to 200, 620, etc.   
    /*$status = $xml->Response->Status->code;  
    if (strcmp($status, "200") == 0) {
            // Successful geocode
            echo '<br />successfull geocode</strong><br />';
            $geocode_pending = false;
    }
    else if (strcmp($status, "620") == 0) {
         // sent geocodes too fast
         $delay += 100000;
            } else {
         // failure to geocode
         $geocode_pending = false;
         echo "Address " . $address . " failed to geocoded. ";
         echo "Received status " . $status . "
        \n";
          }*/
    usleep($delay); 
    }
 }
mysql_close($conn);
?>

Grazie.
 
Ultima modifica di un moderatore:
Forse ho capito. Sono gli spazi nel campo città. Ho provato trim, ma non saprei come fare.

Ciao a tutti rieccomi con il un piccolo problemino con il sito che sto facendo per i mie. Alcuni giorni fa sono stato aiutato con l'inserimento in un campo specifico. Tutto risolto quel problema.Però vorrei creare qualcosa di più completo. Ho adattato questo codice al mio scopo per visualizzare su google i marker dei clienti. Il problema che alcuni il sistema mi restituisce le coordinate e le inserisce nel db, altri proprio no. anche se inserendo illink che mi restituisce lo "vede". Qualcuno mi potrebbe spiegare il perché?
PHP:
<?php

$conn = mysql_connect('localhost', 'root', 'x');
if(!$conn)
 {
   echo "Database Error.";
 }
 else
 {
   echo "Connected.";
   mysql_select_db('prova', $conn);
   $query = "SELECT * from clienti";
   $result = mysql_query($query, $conn) or die($query.mysql_error());
    while($row = mysql_fetch_array($result))
    { 

      $address = $row['via'];
      $civico = $row['civico'];
	$citta = $row ['citta'];
      $id = $row['id'];

      $loc_address = str_replace(" ", "+", $address); //to replace spaces by '+' signs
          $location = $loc_address;
      echo $location."<br>";
      $delay = 0;  // this delay is here just to avoid errors - 
          $url = "http://maps.google.com/maps/api/geocode/json?address=".$location."" .$civico."".$citta."&sensor=false";
          echo $url;
            $output=file_get_contents($url);
            $out= json_decode($output);
            $lat = $out->results[0]->geometry->location->lat;
            $lng = $out->results[0]->geometry->location->lng;
            echo '<br>Lat is '. $lat;
            echo '<br>Long is '. $lng;


       $query = sprintf("UPDATE clienti " .
             " SET lat = '%s', lng = '%s' " .
             " WHERE id = '%s' LIMIT 1;",
             mysql_real_escape_string($lat),
             mysql_real_escape_string($lng),
             mysql_real_escape_string($id));
           $update_result = mysql_query($query);

      echo "Successfully inserted.";
    // Below is just for reference - they say we need the delay. The may problem: does not get the status to compare to 200, 620, etc.   
    /*$status = $xml->Response->Status->code;  
    if (strcmp($status, "200") == 0) {
            // Successful geocode
            echo '<br />successfull geocode</strong><br />';
            $geocode_pending = false;
    }
    else if (strcmp($status, "620") == 0) {
         // sent geocodes too fast
         $delay += 100000;
            } else {
         // failure to geocode
         $geocode_pending = false;
         echo "Address " . $address . " failed to geocoded. ";
         echo "Received status " . $status . "
        \n";
          }*/
    usleep($delay); 
    }
 }
mysql_close($conn);
?>

Grazie.
 
RISOLTO. Ho inserito questa stringa
$query1="UPDATE interventi SET citta = REPLACE(citta, ' ', '')";

Ora è tutto ok.
 

Discussioni simili