2011-09-01 155 views
7

我無法弄清楚如何在我的谷歌地圖中實現搜索框。我有它在哪裏用戶可以從窗體中挑選一些東西,並在地圖上加載標記。現在我想添加他們可以在Google地圖上輸入城市和州的位置,例如在maps.google.com上。這可以通過API v。3來完成嗎?Google Maps API 3搜索框

+0

我不知道如何與谷歌的搜索框完成,但有幾個例子,可以幫助你開始你的問題[地理編碼示例](http://code.google.com/apis/maps/documentation/javascript/examples/geocoding-simple.html)[使用jquery進行地理編碼的示例](http://tech.cibul.net/ geocode-with-google-maps-api-v3 /) – Jorge

回答

26

此任務在Google地圖中沒有完整的「小部件」。但它很容易實現。在HTML中,您可以有一個文本字段和一個按鈕「搜索」。 (相反,您可以在文本字段中處理Enter鍵)。

<input type="text" id="search_address" value=""/> 
<button onclick="search();">Search</button> 

在Javascript中實例化和使用地理編碼:

var addressField = document.getElementById('search_address'); 
var geocoder = new google.maps.Geocoder(); 
function search() { 
    geocoder.geocode(
     {'address': addressField.value}, 
     function(results, status) { 
      if (status == google.maps.GeocoderStatus.OK) { 
       var loc = results[0].geometry.location; 
       // use loc.lat(), loc.lng() 
      } 
      else { 
       alert("Not found: " + status); 
      } 
     } 
    ); 
}; 
+0

我該如何獲得附加信息,如該地點的名稱或地址? – Alp

+1

@Alp:上面的用例描述了您已經知道名稱或地址並且想要獲取相應位置(lat,lng)的情況。如果您想在某個位置附近找到有趣的地方,那麼您可以查看[Google地方](http://code.google.com/apis/maps/documentation/places/)。 – Jiri

+1

@Alp我需要一個確切的東西來請求你的評論,這就是你要找的:https://developers.google.com/maps/documentation/geocoding/#Geocoding你可能已經找到了什麼你現在正在尋找,但仍然;) – mavili