2011-08-16 49 views
0

我對JavaScript很陌生,我正在尋找一個基本的API或函數,可以將地址作爲字符串值提供給我,並返回經度和緯度該地址的緯度。我在數據庫中有一系列地址,我想爲它們中的每一個添加經度和緯度值。使用JavaScript或PHP從地址返回經度和緯度的基本API

感謝

+0

我假設您的意思是郵政式地址,而不是IP或其他東西,對嗎? – Bojangles

+0

[如何以編程方式獲取街道地址的經度和緯度(以及合法)](http://stackoverflow.com/questions/158474/how-to-obtain-longitude-and-latitude-for-a-街道地址編程方式和l) – Gordon

回答

1

查找下面的例子你需要獲得緯度/經度如下。

<?php 
print"<pre>"; 
print_r((string)$getAddress->result->geometry->location->lat); echo "<br />"; 
print_r((string)$getAddress->result->geometry->location->lng); 
?> 

欲瞭解更多詳情請點擊以下鏈接:

http://code.google.com/apis/maps/documentation/geocoding/

+0

謝謝你完美的作品。 – aHunter

1

這就是所謂的地理編碼,是很容易做到與谷歌地圖API:使用PHP

<?php 
$address ="1600+Amphitheatre+Parkway,+Mountain+View,+CA"; 
$url ="http://maps.googleapis.com/maps/api/geocode/xml?address=".$address."&sensor=false"; 
$getAddress = simplexml_load_file($url); 
print"<pre>"; 
print_r($getAddress); 
?> 

它返回所有細節

http://code.google.com/apis/maps/documentation/geocoding/#GeocodingRequests

0
function GetLatLong($address){ 
    $myKey = 'Get key from google api v2 '; 
     $URL = "http://maps.google.co.uk/maps/geo?q=" . urlencode($address) . "&output=xml&key=".$myKey; 
     $xml = simplexml_load_file($URL); 
     $status = $xml->Response->Status->code; 
     if ($status=='200') 
     { //address geocoded correct, show results 
      foreach ($xml->Response->Placemark as $node) 
      { // loop through the responses 
       $address = $node->address; 
       if(strpos($address,'USA') || strpos($address,'usa')) 
       { 
        $quality = $node->AddressDetails['Accuracy']; 
        $coordinates = $node->Point->coordinates; 
        return (explode(',',$coordinates)); 
       } 
      } 
     } 
    } 

該API將僅適用於美國和英國的地址工作。地址應該用'+'分隔,不應該包含任何空格。 此代碼僅解析美國的座標。請根據您的要求修改代碼