2012-03-19 82 views
0

我想使用GoogleMaps API v3。 之前,我使用的是版本2,我只是把地址和地圖顯示出來。 版本n°3總是可以的嗎? 如果是,如何? 我總是找到有緯度,經度的腳本......但沒有地址的腳本。 非常感謝,併爲我可憐的英語感到抱歉。是否可以只顯示地圖的地圖?

+0

你的英語非常好。 – defau1t 2012-03-19 20:00:56

回答

0

我想你混合了Maps-Javascript-API和Static-Maps-API(當前版本是V2)。

在static-maps-API中,您可以使用地址作爲中心參數。

在JavaScript-API中,您需要一個LatLng(無論是V2還是V3)。如果您只有地址,則必須使用地理編碼請求來請求LatLng。

+0

要請求LatLng,腳本是否存在? 我不想讓它在網站上,但我希望LatLng提供地址,這有可能嗎?如果是,在哪裏? 再次感謝。 – user1278739 2012-03-19 15:46:12

+1

是的:文檔中有一個例子。 https://google-developers.appspot.com/maps/documentation/javascript/examples/geocoding-simple – 2012-03-19 15:59:10

+0

非常感謝您;) – user1278739 2012-03-19 16:04:43

0

基本上你需要尋找一種叫geocode的東西。它是API文檔的一部分。像下面將幫助:

var geocoder; 
    var map; 
    function initialize() { 
    geocoder = new google.maps.Geocoder(); 
    var latlng = new google.maps.LatLng(-34.397, 150.644); 
    var myOptions = { 
     zoom: 8, 
     center: latlng, 
     mapTypeId: google.maps.MapTypeId.ROADMAP 
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions); 
    } 

    function codeAddress() { 
    var address = document.getElementById("address").value; 
    geocoder.geocode({ 'address': address}, function(results, status) { 
     if (status == google.maps.GeocoderStatus.OK) { 
     map.setCenter(results[0].geometry.location); 
     var marker = new google.maps.Marker({ 
      map: map, 
      position: results[0].geometry.location 
     }); 
     } else { 
     alert("Geocode was not successful for the following reason: " + status); 
     } 
    }); 
    } 

Helpful Link