如何使用 Google Places API 获取给定位置附近的地标(兴趣点)

How to get landmarks (points of interest) near a given location using Google Places API

我已经使用 Places API 找到了给定位置附近的餐馆和旅馆等地点,并且运行良好。我也想获得给定位置附近的兴趣点或地标。我的尝试如下:

var service = new google.maps.places.PlacesService(map);
service.nearbySearch({
        location: latlng,         
        radius: 1500,
    }, function (results, status) {
        if (status === google.maps.places.PlacesServiceStatus.OK) {
            var type1 = results[i].types[0];
            var type2 = results[i].types[1];
            if (type1 == 'point_of_interest' || type2 == 'point_of_interest') {
                //process place details
            }
        }
    });

例如:对于巴黎战神广场附近的地标搜索,应该 return 埃菲尔铁塔等地标,距离很近。但是,它 return 仅限于酒店和住宿等场所。

如何到达给定位置附近的 return 个兴趣点?解决此问题的任何帮助将不胜感激。

您可以在地点搜索请求中使用关键字参数。根据文档关键字参数是:

A term to be matched against all available fields, including but not limited to name, type, and address, as well as customer reviews and other third-party content.

所以 keyword: "POI" 之类的东西可以做个把戏。请看下面的例子。

代码片段:

    var map, service;

function initialize() {
    var champdemars = new google.maps.LatLng(48.8556475,2.2986304);
    var mapOptions = {
        zoom:14,
        center: champdemars
    };
    map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
    service = new google.maps.places.PlacesService(map);
    service.nearbySearch({
        location: champdemars,         
        radius: 1500,
        keyword: "POI"
    }, function (results, status) {
        if (status === google.maps.places.PlacesServiceStatus.OK) {
            results.forEach(function (p) {
                var m = new google.maps.Marker({
                   position: p.geometry.location,
                   icon: {
                     url: p.icon,
                     scaledSize: new google.maps.Size(24,24)
                   },
                   title: p.name,
                   map: map
                });                  
            });
        } else {
            alert(status);
        }
    });        
}
html,
body,
#map-canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<div id="map-canvas"></div>
<script async defer src="https://maps.googleapis.com/maps/api/js?v=3&libraries=places&callback=initialize"></script>

如您所见,埃菲尔铁塔、陆军博物馆或联合国教科文组织等兴趣点现在显示在地图上。