【问题标题】:Google Maps API 3 search boxGoogle Maps API 3 搜索框
【发布时间】:2011-11-08 15:15:57
【问题描述】:

我不知道如何在我的谷歌地图中实现搜索框。我有它,用户可以从表单中选择一些东西,并在地图上加载标记。现在我想添加他们可以使用谷歌搜索框输入城市和州的位置,比如在 maps.google.com 上。这可以通过 API v. 3 完成吗?

【问题讨论】:

标签: html google-maps search google-maps-api-3


【解决方案1】:

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

【讨论】:

  • 我怎样才能获得该地点的名称或地址等其他信息?
  • @Alp:上面的用例描述了你已经知道名字或地址,想要得到对应的位置(lat,lng)的情况。如果您想在某个地点附近找到有趣的地方,那么您可以查看Google places
  • @Alp 我需要与您在评论中要求的内容完全相同的东西,这就是您要寻找的东西:developers.google.com/maps/documentation/geocoding/#Geocoding 您现在可能已经找到了您想要的东西,但是仍然;)
  • 您正在寻找的是“反向地理编码”(即从纬度和经度点查找文本地址)。
猜你喜欢
  • 2013-06-28
  • 1970-01-01
  • 1970-01-01
  • 2021-09-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多