2013-09-03 23 views
12

è possibile convertire (in PHP back-end):Google Maps - conversione dell'indirizzo in latitudine e longitudine - backend PHP?

<?php 
    $Address = "Street 1, City, Country"; // just normal address 
?> 

a

<?php 
    $LatLng = "10.0,20.0"; // latitude & lonitude 
?> 

Il codice Attualmente sto usando è quella predefinita:

<script type='text/javascript' src='https://maps.googleapis.com/maps/api/js?v=3.exp&#038;sensor=false'></script> 

<script> 

function initialize() { 
    var myLatlng = new google.maps.LatLng(10.0,20.0); 
    var mapOptions = { 
    zoom: 16, 
    center: myLatlng, 
    mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    var map = new google.maps.Map(document.getElementById('map'), mapOptions); 

    var marker = new google.maps.Marker({ 
     position: myLatlng, 
     map: map, 
     title: 'This is a caption' 
    }); 
} 

google.maps.event.addDomListener(window, 'load', initialize); 

</script> 

<div id="map"></div> 

I Ho letto per un po 'lo https://developers.google.com/maps/documentation/geocoding ma non ne ho abbastanza.

Grazie.

risposta

5

Sì, è possibile. Si potrebbe fare una richiesta con file_get_contents o curl a un URL simile a questo:

http://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA&sensor=false

cosa viene restituito è una risposta JSON codificato. È possibile analizzare questo in matrici utilizzando json_decode.

La pagina di documentazione ha un esempio di come potrebbe apparire la risposta, utilizzarla per trovare i dati necessari.

7
<?php 

/* 
* Given an address, return the longitude and latitude using The Google Geocoding API V3 
* 
*/ 

function Get_LatLng_From_Google_Maps($address) { 
    $address = urlencode($address); 

    $url = "http://maps.googleapis.com/maps/api/geocode/json?address=$address&sensor=false"; 

    // Make the HTTP request 
    $data = @file_get_contents($url); 
    // Parse the json response 
    $jsondata = json_decode($data,true); 

    // If the json data is invalid, return empty array 
    if (!check_status($jsondata)) return array(); 

    $LatLng = array(
     'lat' => $jsondata["results"][0]["geometry"]["location"]["lat"], 
     'lng' => $jsondata["results"][0]["geometry"]["location"]["lng"], 
    ); 

    return $LatLng; 
} 

/* 
* Check if the json data from Google Geo is valid 
*/ 

function check_status($jsondata) { 
    if ($jsondata["status"] == "OK") return true; 
    return false; 
} 

/* 
* Print an array 
*/ 

function d($a) { 
    echo "<pre>"; 
    print_r($a); 
    echo "</pre>"; 
} 

Per codice di esempio di come utilizzare la funzione di cui sopra, non esitate a visitare my blog

32

questo funziona per me:

<?php 
    $Address = urlencode($Address); 
    $request_url = "http://maps.googleapis.com/maps/api/geocode/xml?address=".$Address."&sensor=true"; 
    $xml = simplexml_load_file($request_url) or die("url not loading"); 
    $status = $xml->status; 
    if ($status=="OK") { 
     $Lat = $xml->result->geometry->location->lat; 
     $Lon = $xml->result->geometry->location->lng; 
     $LatLng = "$Lat,$Lon"; 
    } 
?> 

Riferimenti:

+0

Si prega di notare ho aggiornato il codice per essere più oo/http://www.fcsoftware.co.uk/blog/?p=71 accurata – Dave

2
this will work please try this one i have tested it . 
<?php 
      $address = 'Street 1, City, Country'; // Your address 
      $prepAddr = str_replace(' ','+',$address); 

      $geocode=file_get_contents('http://maps.google.com/maps/api/geocode/json?address='.$prepAddr.'&sensor=false'); 

      $output= json_decode($geocode); 

      $lat = $output->results[0]->geometry->location->lat; 
      $long = $output->results[0]->geometry->location->lng; 

      echo $address.'<br>Lat: '.$lat.'<br>Long: '.$long; 

    ?> 
+0

Questa è lavorato per singolo indirizzo, ma ho più indirizzo quindi come posso fare per questo. –