Google Maps API - Multiple keywords in place searches

前端 未结 6 2088
青春惊慌失措
青春惊慌失措 2021-02-08 19:58

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

相关标签:
6条回答
  • 2021-02-08 20:18

    EDIT: This appeared to work initially but more results were not reliable and were not predictable. We ended up not being able to use the API in this way for our scenario.

    I use the web service but I assume it should work with all API access points. It took me quite a number combinations to test but I eventually landed on realizing the API required parentheses AND quotes to get multi-word OR conditions to work.

    For example, in url param form:

    keywords=("Place Name 1") OR ("PlaceName2") OR ("Place Name3")

    0 讨论(0)
  • 2021-02-08 20:21

    It looks like the answer is "no" (at least at the present time, you could request an enhancement on the issue tracker.

    A work around would be to send three separate queries, one for each keyword. Should be OK for 3 keywords, at some point you will run into the query rate limit.

      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);
    

    proof of concept

    code snippet:

    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>

    0 讨论(0)
  • 2021-02-08 20:24

    I had the same problem as I tried to figure out how to use multiple keywords with one request. At first the answer of @netoale helped me, but this is no longer possible. After some general problems with Google I asked in the support and one of the employees wrote me that it is possible to link several keywords with a simple "|".

    As an example:

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

    Works for me now.

    0 讨论(0)
  • 2021-02-08 20:32

    Did you try with type option. If using type you can get more than one place information

    Please see the possible supported types and RadarSearchRequests try like this

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

    I don't know 'tacos' is which type. please supported type and give that 'tacos' type value.

    Please let me know if you have any issues

    0 讨论(0)
  • 2021-02-08 20:41

    I believe this is related to Google Maps API open issue 4845. Please star that issue to track progress.

    0 讨论(0)
  • 2021-02-08 20:44

    Using Google place API, Make a boolean search seems to work like this :

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

    you can use OR, AND but respect case!!

    0 讨论(0)
提交回复
热议问题