2013-09-03 29 views
12

jest to możliwe do konwersji (w PHP back-end):Google Maps - konwersja adresu na szerokość i długość geograficzną - backend PHP?

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

do

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

Kod obecnie używam jest domyślna:

<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 od pewnego czasu czytam teraz https://developers.google.com/maps/documentation/geocoding, ale nie jestem wystarczająco doświadczony, jak sądzę.

Dzięki.

Odpowiedz

5

Tak, można. zrobiłbyś wniosek z file_get_contents lub curl do adresu URL tak:

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

Co jest zwracany jest json zakodowany odpowiedź. Możesz sparsować to w tablice przy użyciu json_decode.

Strona dokumentacji zawiera przykład tego, jak może wyglądać odpowiedź, użyj jej do znalezienia potrzebnych danych.

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

Dla przykładowego kodu, w jaki sposób za pomocą funkcji powyżej, zachęcamy do odwiedzenia my blog

32

Działa to dla mnie:

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

Referencje:

+0

Uwaga I uaktualniony kod będzie bardziej oo/dokładne http://www.fcsoftware.co.uk/blog/?p=71 – 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

ten pracował dla jednego adresu, ale mam wiele adres, a następnie jak mogę to zrobić. –