10

Is it possible search multiple individual keywords in one place search request using Google Maps JavaScript API v3?

In the Google Places API documentation it states that multiple keywords can be used https://developers.google.com/places/training/additional-places-features

?keyword=theater+gym

but this does not work in the JavaScript API. I tried:

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

...and does not return places for each keyword. Does anyone have an idea how to search multiple keywords?

Note: I'm trying to search multiple individual keywords in one request not a phrase with spaces.

4

6 回答 6

11

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

一种解决方法是发送三个单独的查询,每个关键字一个。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>

于 2013-11-02T20:50:49.547 回答
7

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

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

您可以使用 OR 和 AND,但请注意大小写!

于 2014-05-28T12:13:12.080 回答
3

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

我使用 Web 服务,但我认为它应该适用于所有 API 访问点。我花了很多组合来测试,但最终我意识到 API 需要括号和引号才能使多词 OR 条件起作用。

例如,以 url 参数形式:

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

于 2015-10-06T18:01:26.493 回答
3

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

举个例子:

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

现在为我工作。

于 2019-05-31T08:45:38.340 回答
1

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

请查看可能支持的类型RadarSearchRequests 尝试这样

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

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

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

于 2013-11-08T04:43:48.043 回答
0

我相信这与Google Maps API open issue 4845有关。请对该问题加注星标以跟踪进度。

于 2013-10-29T05:24:10.723 回答