2012-02-21 66 views
0

我在我的網站中使用Google Maps API v.3。我想實現的是以下流程:Google地圖以特定搜索區域爲中心

  1. 用戶被呈現一個表單,他輸入一個地址(某種 - 城市,街道等)。
  2. 表格填充完成後,向他顯示一張地圖,向他顯示以他輸入的地址爲中心的地圖。然後他可以輸入一個關鍵字來搜索地圖區域內的谷歌地點。

我停在的地址是地圖的翻譯地圖。根據我對v3 API的理解,我應該用LatLng中心位置初始化地圖 - 但是有一個城市名稱,所以我現在還不能完成。我需要在文本地址和座標之間進行某種翻譯 - 例如,當我搜索「比佛利山莊」時,Google地圖正在執行的操作。我想呢,有些相反的情況?我應該怎麼做在javascript API?

或者有沒有在v3嵌入式地圖中包含搜索欄的選項?

+0

他問v3,你的鏈接是基於V2 – defau1t 2012-02-21 09:06:25

回答

2

您需要使用geocode類似下面從http://code.google.com/apis/maps/documentation/javascript/geocoding.html

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

複製

,然後你將需要調用您的按鈕codeAddress()功能點擊