Script per recuperare lat e long da google maps

di il
2 risposte

Script per recuperare lat e long da google maps

Salve devo creare uno script che recupera da gmaps lat e long e me le inserisce in un database con indirizzi già presenti...

ho provato ad inserire questo codice ma mi da errore
<?php 
// -A-  
//-------**se il codice da -A- a -B- lo metti in un file esterno devi fare un include o un require 
//variabili di connessione---------------------------------------- 
$key = "AIzaSyAKIZjb16_H2e3m30kXQIPi_l3nisN_2iQ";
$host = 'localhost';     //nome host 
$username = 'root';      //user name 
$password = '';  //password 
$db = 'portale';            // nome data base 
//coordinate 


//---connessione---------------------------------------------------- 
$conn = @mysql_connect($host,$username,$password) or die (mysql_error()); 
$sel = @mysql_select_db($db) or die (mysql_error()); 

$longitude = "";
$latitude = "";
$precision = "";
$address_to_encode="via Durini, 24 20122 Milano";
 $delay = 0;
 $base_url = "http://maps.google.com/maps/geo?output=xml&key=AIzaSyAKIZjb16_H2e3m30kXQIPi_l3nisN_2iQ&oe=utf-8";
// a mysql_error() si può sostituire un altro messaggio 
// -B- 
// se code da -A- a -B- esterno decommntare riga seguente 
//include_once "connessione.php"; 
function getAddress($address_to_encode)
 {
	$base_url = "http://maps.google.com/maps/geo?output=xml&key=AIzaSyAKIZjb16_H2e3m30kXQIPi_l3nisN_2iQ&oe=utf-8";

	$results = array();
	 // Initialize delay in geocode speed

	 // Iterate through the rows, geocoding each address
	 $geocode_pending = true;
	 while ($geocode_pending) {
	 $request_url = $base_url . "&q=" . urlencode($address_to_encode);
	 $xml = simplexml_load_file($request_url) or die("url not loading");
	 $status = $xml->Response->Status->code;
	 if (strcmp($status, "200") == 0) {
	 // Successful geocode
	 $geocode_pending = false;
	 $coordinates = $xml->Response->Placemark->Point->coordinates;
	 $coordinatesSplit = split(",", $coordinates);
	 // Format: Longitude, Latitude
	 $lng = $coordinatesSplit[0];
	 $lat = $coordinatesSplit[1];
	 $results[] = array("lat" => $lat, "lng" => $lng);
	 } else if (strcmp($status, "620") == 0) {
	 // sent geocodes too fast
	 $delay += 100000;
	 } else {
	 // failure to geocode
	 $geocode_pending = false;
	 $results[] = array("lat" => 0, "lng" => 0);
	 $this->errorMsg .= "Address ".$address.
	 " failed to geocoded. Received status ".$status."\n";
	 }
	 usleep($delay);
	 }
	 return $results;
 }
$query_stringa="SELECT * FROM sports_facilities";//ed altre condizioni 
$query=mysql_query($query_stringa);//query interroga 
$quanti_record=mysql_num_rows($query);//numero di record presenti se == 0 devi saltare il ciclo 
echo $quanti_record;
 $delay = 0;

if($quanti_record>0){//se ci sono record 
	while($riga=mysql_fetch_array($query)){
		$address_to_encode =$riga['address'] .' '.$riga['civic_number'].','.$riga['cap'];
		echo $address_to_encode.PHP_EOL;		
		$risultato = getAddress($address_to_encode);
		echo $results['lat'].' '.$results['lng'];
			
 
	}//fine while 
}//fine if 

mysql_close(); 
?>


Warning: simplexml_load_file(): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
in C:\wamp\www\fitadvisor.it\public\popolacoord.php on line 38

Warning: simplexml_load_file(): I/O warning : failed to load external entity "" in C:\wamp\www\fitadvisor.it\public\popolacoord.php on line 38
url not loading

qualcuno mi sa aiutare???

2 Risposte

  • Re: Script per recuperare lat e long da google maps

    Probabilmente hai un url vecchio.
    https://maps.google.com/maps/ge non esiste, ti viene dato Pagina non trovata.

    Cerca qualche classe/script aggiornato.
  • Re: Script per recuperare lat e long da google maps

    Puoi fare una riechiesta GET a questo URL:
    https://maps.googleapis.com/maps/api/geocode/json?address={indirizzo}&key={apikey}
    ad esempio:
    https://maps.googleapis.com/maps/api/geocode/json?address=via%20Roma,%20Rimini&key=xxxxxxxxx
    e riceverai come risposta:
    
    {
       "results" : [
          {
             "address_components" : [
                {
                   "long_name" : "Via Roma",
                   "short_name" : "Via Roma",
                   "types" : [ "route" ]
                },
                {
                   "long_name" : "Rimini",
                   "short_name" : "Rimini",
                   "types" : [ "locality", "political" ]
                },
                {
                   "long_name" : "Rimini",
                   "short_name" : "Rimini",
                   "types" : [ "administrative_area_level_3", "political" ]
                },
                {
                   "long_name" : "Provincia di Rimini",
                   "short_name" : "RN",
                   "types" : [ "administrative_area_level_2", "political" ]
                },
                {
                   "long_name" : "Emilia-Romagna",
                   "short_name" : "Emilia-Romagna",
                   "types" : [ "administrative_area_level_1", "political" ]
                },
                {
                   "long_name" : "Italy",
                   "short_name" : "IT",
                   "types" : [ "country", "political" ]
                },
                {
                   "long_name" : "47921",
                   "short_name" : "47921",
                   "types" : [ "postal_code" ]
                }
             ],
             "formatted_address" : "Via Roma, 47921 Rimini RN, Italy",
             "geometry" : {
                "bounds" : {
                   "northeast" : {
                      "lat" : 44.06445859999999,
                      "lng" : 12.5782135
                   },
                   "southwest" : {
                      "lat" : 44.0581885,
                      "lng" : 12.569247
                   }
                },
                "location" : {
                   "lat" : 44.0617069,
                   "lng" : 12.5746148
                },
                "location_type" : "GEOMETRIC_CENTER",
                "viewport" : {
                   "northeast" : {
                      "lat" : 44.06445859999999,
                      "lng" : 12.5782135
                   },
                   "southwest" : {
                      "lat" : 44.0581885,
                      "lng" : 12.569247
                   }
                }
             },
             "place_id" : "ChIJZYBB15jDLBMRnqCPcqzZIGs",
             "types" : [ "route" ]
          }
       ],
       "status" : "OK"
    }
    


    Puoi generare la tua APIKEY qui: https://cloud.google.com/maps-platform , clickando su get started
Devi accedere o registrarti per scrivere nel forum
2 risposte