谷歌地图API - 多个关键字的位置搜索

Google Maps API - Multiple keywords in place searches

本文关键字:位置 搜索 关键字 API 谷歌地图      更新时间:2023-09-26

是否可以使用Google Maps JavaScript API v3在一个地方搜索多个关键字?

在Google Places API文档中,它指出可以使用多个关键字 https://developers.google.com/places/training/additional-places-features

?keyword=theater+gym

但这在 JavaScript API 中不起作用。我试过了:

function performSearch() {
  var request = {
    location: map.center,
    radius: '500',
    keyword: 'theater+gym+tacos',
    rankBy: 'distance'
  };
  service.radarSearch(request, callback);
}

。并且不返回每个关键字的位置。有谁知道如何搜索多个关键字?

注意:我正在尝试在一个请求中搜索多个单独的关键字,而不是带有空格的短语。

看起来答案是否定的(至少目前,您可以请求对问题跟踪器进行增强。

解决方法是发送三个单独的查询,每个关键字一个。 对于 3 个关键字应该没问题,在某些时候您会遇到查询速率限制。

  var request = {
    location: pyrmont,
    radius: 500,
    keyword: 'theater'
  };
  infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
  var request2 = {
    location: pyrmont,
    radius: 500,
    keyword: 'gym'
  };
  var service2 = new google.maps.places.PlacesService(map);
  service2.nearbySearch(request2, callback);
  var request3 = {
    location: pyrmont,
    radius: 500,
    keyword: 'tacos'
  };
  var service3 = new google.maps.places.PlacesService(map);
  service3.nearbySearch(request2, callback);

概念验证

代码片段:

var map;
var infowindow;
function initialize() {
  var pyrmont = new google.maps.LatLng(-33.8665433, 151.1956316);
  map = new google.maps.Map(document.getElementById('map-canvas'), {
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    center: pyrmont,
    zoom: 15
  });
  var request = {
    location: pyrmont,
    radius: 500,
    keyword: 'theater'
  };
  infowindow = new google.maps.InfoWindow();
  var service = new google.maps.places.PlacesService(map);
  service.nearbySearch(request, callback);
  var request2 = {
    location: pyrmont,
    radius: 500,
    keyword: 'gym'
  };
  var service2 = new google.maps.places.PlacesService(map);
  service2.nearbySearch(request2, callback);
  var request3 = {
    location: pyrmont,
    radius: 500,
    keyword: 'tacos'
  };
  var service3 = new google.maps.places.PlacesService(map);
  service3.nearbySearch(request2, callback);
}
function callback(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      createMarker(results[i]);
    }
  } else alert("Places request failed: " + status);
}
function createMarker(place) {
  var placeLoc = place.geometry.location;
  var marker = new google.maps.Marker({
    map: map,
    position: place.geometry.location
  });
  google.maps.event.addListener(marker, 'click', function() {
    infowindow.setContent(place.name);
    infowindow.open(map, this);
  });
}
google.maps.event.addDomListener(window, 'load', initialize);
html,
body,
#map-canvas {
  height: 100%;
  width: 100%;
  margin: 0px;
  padding: 0px
}
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places"></script>
<div id="map-canvas"></div>

使用 Google Place API,进行布尔搜索似乎是这样工作的:

var request = {
            location: /*your location*/,
            radius: /*your radius*/,              
            keyword: "(cinema) OR (theater)"           
        };

您可以使用 OR、AND 但尊重大小写!!

当我试图弄清楚如何在一个请求中使用多个关键字时,我遇到了同样的问题。起初,@netoale的回答帮助了我,但这不再可能。在Google出现一些一般性问题后,我向支持人员询问,其中一名员工写信给我说,可以用简单的"|"链接多个关键字。

举个例子:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=lat,lng&key=API_KEY&keyword=theater|castle|park&rankby=distance

现在对我有用。

编辑:这最初似乎有效,但更多的结果不可靠,不可预测。我们最终无法以这种方式在我们的场景中使用 API。

我使用 Web 服务,但我认为它应该适用于所有 API 接入点。我花了相当多的组合来测试,但我最终意识到 API 需要括号和引号才能使多词 OR 条件正常工作。

例如,在网址参数形式中:

关键字=("地名 1") 或 ("地名 2") 或 ("地名 3")

您是否尝试使用类型选项。如果使用类型,您可以获得多个地点信息

请参阅可能支持的类型和雷达搜索请求像这样尝试

function performSearch() {
  var request = {
    location: map.center,
    radius: '500',
    types: ['movie_theater', 'gym'],
    rankBy: 'distance'
  };
  service.radarSearch(request, callback);
}

我不知道"炸玉米饼"是哪种类型。 请支持类型并给出"炸玉米饼"类型值。

如果您有任何问题,请告诉我

我相信

这与谷歌地图API开放问题4845有关。请为该问题加注星标以跟踪进度。