繁体   English   中英

Google Maps API - 地点搜索中的多个关键字

[英]Google Maps API - Multiple keywords in place searches

是否可以使用 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 但要注意大小写!!

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

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

例如,在 url 参数形式中:

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

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

举个例子:

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

现在为我工作。

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

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM