【问题标题】:How to search for cities in an area using Google Maps v3?如何使用 Google Maps v3 搜索区域内的城市?
【发布时间】:2012-07-03 04:49:01
【问题描述】:

我尝试搜索可见地图范围内的所有城市。我该怎么做?

以下是我尝试做的:

$.fn.gmap3.geocoder.geocode({ 'address': 'Georgia' }, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        globalMap.setCenter(results[0].geometry.location);
        var resultBounds = new google.maps.LatLngBounds(
            results[0].geometry.viewport.getSouthWest(),
            results[0].geometry.viewport.getNorthEast()
        );
        globalMap.fitBounds(resultBounds);
        // get cities in the map
        var service = new google.maps.places.PlacesService(globalMap);
        var request = {
            bounds: resultBounds,
            types: ['locality']
        };
        service.search(request, function (results, status) {
            debugger;
        });
    }
});

但结果是ZERO_RESULTS。也许是因为结果被限制在 50.000 米的半径范围内?

有人知道如何解决我的问题吗?非常感谢。

--更新--

感谢 Sean 仔细阅读我的帖子并提供详细反馈。

这是我引用库的方式: <pre>src="https://maps.googleapis.com/maps/api/js?sensor=false&amp;libraries=places"</pre> 我还为地理编码功能添加了更多细节以获得更精确的结果。但我仍然没有得到我想要的结果。

检查此页面中的列表:https://developers.google.com/places/documentation/supported_types,我意识到第一个列表中的几乎所有项目都返回值,但不是第二个列表。唯一的项目返回值是 'political',它只返回 1 而不是 20。

这是我修改后的代码:

this.setCenterByAddress = function (address, region) {
    $.fn.gmap3.geocoder.geocode({ 'address': address, 'region': region }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            globalMap.setCenter(results[0].geometry.location);
            var resultBounds = new google.maps.LatLngBounds(
                results[0].geometry.viewport.getSouthWest(),
                results[0].geometry.viewport.getNorthEast()
            );
            globalMap.fitBounds(resultBounds);
            // get cities in the map
            var service = new google.maps.places.PlacesService(globalMap);
            var request = {
                bounds: resultBounds,
                types: ['country',
                'administrative_area_level_1',
                'administrative_area_level_2',
                'administrative_area_level_3',
                'colloquial_area',
                'country',
                'floor',
                'geocode',
                'intersection',
                'locality',
                'natural_feature',
                'neighborhood',
                'political',
                'point_of_interest',
                'post_box',
                'postal_code',
                'postal_code_prefix',
                'postal_town',
                'premise',
                'room',
                'route',
                'street_address',
                'street_number',
                'sublocality',
                'sublocality_level_4',
                'sublocality_level_5',
                'sublocality_level_3',
                'sublocality_level_2',
                'sublocality_level_1',
                'subpremise',
                'transit_station']
            };
            service.search(request, function (results, status) {
                debugger;
            });
        }
    });
}

更多信息:即使使用位置和半径,也会返回相同的值。而且我使用免费地图并一直收到“OVER_QUERY_LIMIT”。

【问题讨论】:

  • 你有什么解决办法吗?

标签: google-maps-api-3 google-places-api google-geocoder


【解决方案1】:

除非您使用locationradius 选项,否则您不应将其限制在50,000 米,而您并非如此;您正在使用bounds。我建议退出一个级别并深入研究从开始调用geocode 返回的结果,因为您对PlacesService 的使用似乎是正确的。 resultBounds 对象的内部值是什么?我还注意到,当您调用地理编码器时,您没有使用区域偏差,可能是“Georgia”不够具体。例如,您是指俄罗斯联邦内的地区还是美国州内的地区?而且我不确定您使用什么 URL 来加载 Google Maps API 和地点库,但这也可能会影响您的结果。

我会仔细检查从地理编码器返回的结果,因为除非我遗漏了什么,否则您的基本方法看起来是正确的。

【讨论】:

  • <script type="text/javascript" src="&lt;a%20href=" https: rel="nofollow" target="_blank">maps.googleapis.com/maps/api/…></script>
  • 由于您是从 google.com 加载的,因此假定美国是您的地区偏差。因此,我希望您收到美国乔治亚州的地理编码响应。
  • 我写了完整的回复,但我不知道如何放在这里。我认为它已被管理员删除。如果您不介意,请以未格式化的方式阅读我的评论。谢谢。检查此页面中的列表:developers.google.com/places/documentation/supported_types,我意识到第一个列表中的几乎所有项目都返回值,但不是第二个列表。唯一的项目返回值是 'political',它只返回 1 而不是 20。
【解决方案2】:

您不能使用 Geocoding API/Places API 在特定经纬度附近返回两个以上的地点

请在 Google Places API 论坛上查看 this thread

将 Google Places API 作为政治结果是不可能的 例如地方、邻里和次地方返回到 确定请求的区域,每个请求限制为两个。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多