2013-09-03 12 views
12

ist es möglich (in PHP Back-End) zu konvertieren:Google Maps - Adresse in Breiten- und Längengrad umwandeln - PHP-Backend?

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

zu

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

Der Code, den ich zur Zeit ist mit dem Standard ein:

<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 habe schon seit einer Weile https://developers.google.com/maps/documentation/geocoding gelesen, aber ich bin nicht erfahren genug, denke ich.

Danke.

Antwort

5

Ja, Sie können. Sie würden mit file_get_contents oder curl zu einer URL wie diese eine Anfrage tun:

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

Was zurückgegeben wird eine json ist codiert Antwort. Sie können dies in Arrays unter Verwendung json_decode analysieren.

Die Dokumentationsseite enthält ein Beispiel dafür, wie die Antwort aussehen könnte. Verwenden Sie diese, um die benötigten Daten zu finden.

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>"; 
} 

Beispielcode, wie oben mit der Funktion, fühlen Sie sich frei my blog

32

Dies funktioniert für mich zu besuchen:

<?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"; 
    } 
?> 

Referenzen:

+0

Bitte beachten Sie, ich den Code aktualisiere mehr oo/genaue http://www.fcsoftware.co.uk/blog/?p=71 seine – 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

Dieser für einzelne Adresse gearbeitet wird, aber ich habe mehr Adresse dann wie kann ich dafür tun. –

Verwandte Themen