2010-09-28 27 views
0

我做了很多研究,但無法找到在google地圖 中創建latlng的方式,如何使用latlng號碼的地址?我如何使用php創建Latlng地址爲谷歌地圖V3

劇本

function initialize() 
{ 
    var latlng = new google.maps.LatLng(-41.251290,174.803638); 
    var opt = 
    { 
     center:latlng, 
     zoom:10, 
     mapTypeId: google.maps.MapTypeId.ROADMAP, 
     disableAutoPan:false, 
     navigationControl:true, 
     navigationControlOptions: {style:google.maps.NavigationControlStyle.SMALL }, 
     mapTypeControl:true, 
     mapTypeControlOptions: {style:google.maps.MapTypeControlStyle.DROPDOWN_MENU} 
    }; 
    var map = new google.maps.Map(document.getElementById("map"),opt); 
    var marker= new google.maps.Marker({ 
    position: new google.maps.LatLng(-41.251290,174.803638), 
    title: "CodeGlobe", 
    clickable: true, 
    map: map 
}); 

非常感謝!

回答

1

使用其Web服務API

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

做的東西的請求像

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

使用類似file_get_contents(不記得是否支持跨站點.. ?無論如何,我相信你可以查找適當的功能)

然後用json_decode解析結果我認爲這個函數......因爲我碰到了PHP,所以一直沒有這麼做。

嘆息來吧人。

$address = "1600 Amphitheatre Parkway, Mountain View, CA"; 
$url = "http://maps.googleapis.com/maps/api/geocode/json?address=".urlencode($address)."&sensor=false"; 
$result_string = file_get_contents($url); 
$result = json_decode($result_string, true); 
list($lat, $long) = $result['geometry']['location']; 

類似的東西。我沒有測試過它,但給它一個旋轉。 5行代碼。

+0

的file_get_contents允許「跨網站」如果服務器被配置爲允許它。 json_decode是正確的功能。 – Galen 2010-09-28 01:34:32

+0

感謝您的建議,但是對於我來說,您的指導有點難,因爲我只是程序員的初學者。不管怎麼說,還是要謝謝你。我會嘗試。 – xuanyinwen 2010-09-28 02:42:48

+0

好吧,你必須自己解決一些事情來學習。它不應該太難...我們只會說10行代碼。 – mpen 2010-09-28 05:31:01

0

我曾與馬克斯答案取$lat$long來解決它的問題,我不得不這樣做

$lat = $result['results'][0]['geometry']['location']['lat'];

$long = $result['results'][0]['geometry']['location']['lng'];