问题描述
我无法弄清楚如何在我的谷歌地图中实现搜索框。我有它在哪里用户可以从窗体中挑选一些东西,并在地图上加载标记。现在我想添加他们可以在Google地图上输入城市和州的位置,例如在maps.google.com上。这可以通过API v.3完成吗?
I cannot figure out how to implement the search box in my google maps. I have it where the user can pick some stuff from a form and it loads the markers on the map. Now I want to add where they can type in the city and state using the google search box, like on maps.google.com. Can this be done with API v. 3?
推荐答案
谷歌地图中没有完整的widget用于此任务。但它很容易实现。在HTML中,您可以有一个文本字段和一个按钮搜索。 (相反,您可以在文本字段中处理Enter键)。
There is no complete "widget" in Google maps for this task. But it is easy to accomplish it. In HTML you can have a text field and a button "Search". (Instead you can handle the Enter key in the text field).
<input type="text" id="search_address" value=""/>
<button onclick="search();">Search</button>
在Javascript中,您可以实例化并使用Geocoder:
In Javascript you instantiate and use a Geocoder:
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);
}
}
);
};
这篇关于Google Maps API 3搜索框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!