简体   繁体   中英

Google Places API - Location near Landmark

I was wondering if it is possible to limit the Google Places API to search only for 'x' near 'landmark' where landmark is limited to landmarks only.

For example:

Not desired: coffee shop near Walmart

Desired: coffee shop near Eiffel Tower

Using javascript API.

Cheers in advance.

As found at https://developers.google.com/maps/documentation/javascript/places#place_search_requests

A Places Nearby search is initiated with a call to the PlacesService's nearbySearch() method, which will return an array of PlaceResult objects.

 service = new google.maps.places.PlacesService(map); service.nearbySearch(request, callback); 

You must also pass a callback method to nearbySearch(), to handle the results object and google.maps.places.PlacesServiceStatus response.

 var map; var service; var infowindow; function initialize() { var pyrmont = new google.maps.LatLng(-33.8665433,151.1956316); map = new google.maps.Map(document.getElementById('map'), { center: pyrmont, zoom: 15 }); var request = { location: pyrmont, radius: '500', types: ['store'] }; service = new google.maps.places.PlacesService(map); service.nearbySearch(request, callback); } function callback(results, status) { if (status == google.maps.places.PlacesServiceStatus.OK) { for (var i = 0; i < results.length; i++) { var place = results[i]; createMarker(results[i]); } } } 

You'd change the LatLng of pyrmont to change the location, in your case, The Eiffeltower. You could also change all occurrences of pyrmont to your own variable. shop could also be changed.

You'd need to do two searches:

  1. Lookup the candidate landmark using a PlacesService 's textSearch method. If the top result matches your definition of landmark (perhaps the types array includes point_of_interest ?), then use its location in step 2.
  2. Use PlacesService 's nearbySearch to do a search for keyword = x around that location.

Something like:

service = new google.maps.places.PlacesService(map);

var request = {
    query: "Eiffel Tower",
    types: ["point_of_interest"]
}

function extractTopLandmark(results, status) {
    // Give up if no landmark was found
    if (status != google.maps.places.PlacesServiceStatus.OK) return;
    if (results.length < 1) return;
    // We have a landmark, use its location to do a nearby search.
    var request = {
        keyword: "coffee shop",
        location: results[0].geometry.location,
        radius: 500
    };
    service.nearbySearch(request, addMarkers);
}

function addMarkers(results, status) {
  if (status == google.maps.places.PlacesServiceStatus.OK) {
    for (var i = 0; i < results.length; i++) {
      var place = results[i];
      createMarker(results[i]);
    }
  }
}

service.textSearch(request, extractTopLandmark);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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