当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Can I use a formatted address in Google Places API call

问题描述:

I am hoping to provide a specific function in my app. I want to be able to enter an address, or use an address stored in a database e.g. "119 Harley Street, London" or "119 Harley Street" (this is just a random address to illustrate my requirements taken from the Google Places 'vicinity' field relating to a business called "The London Clinic Eye Centre").

Based on the address entered, I would like to be able to identify the Business Name at this address, if there is one, so the 'name' field in the Places JSON result, and also identify if there are any recent reviews by customers, with Date/Time and the review itself, photos etc.

Is this at all possible, or am I looking for something that isn't possible? I have experience of using other Google APIs (Geolocation & JS) but this is a new one to me, and can't see any way of achieving this from the documentation - I hope I am missing something.

Can anyone help? An alternative way of achieving what I need would also be much appreciated.

Many thanks in advance.

网友答案:

Looks like you want something like the At this location in Google Maps (example):

This is not exactly available in the same way in the Places API, but I think you can use Nearby Search to get pretty close:

  1. Use Places Autocomplete (or Geocoding) to obtain the latlng for an address (or business, it doesn't matter).
  2. Use Places Nearby Search to find all establishments at that location, by querying with that latlng as location, no keyword, and rankby=distance
  3. Keep only the closest results (first few) and/or (maybe) filter by their address.

Example Nearby Search request for "Bahnhofstrasse 30 Zurich", which geocodes to 47.3705904,8.5391694:

https://maps.googleapis.com/maps/api/place/nearbysearch/json?location=47.3705904,8.5391694&rankby=distance&key=YOUR_API_KEY

Results (name, vicinity):

  1. Griederbar, Bahnhofstrasse 30, Zürich
  2. Coiffure Valentino, Bahnhofstrasse 30, Zürich
  3. Grieder G.Point, Bahnhofstrasse 30, Zürich
  4. Shanty Beauty Design, Bahnhofstrasse 30, Zürich
  5. Louis Vuitton, Bahnhofstrasse 30, Zürich
  6. Brunschwig & Cie Sa, Bahnhofstrasse 30, Zürich
  7. Clariden Leu, Bahnhofstrasse 32, Zürich
  8. Gen Del Sa, Bahnhofstrasse 32, Zürich
  9. Abegg & Co AG, Zürich, Bahnhofstrasse 30, Zürich
  10. Wohlfahrtsfonds der Clariden Leu AG, Bahnhofstrasse 32, Zürich
  11. Uhren shop Zürich Jaeger-LeCoultre, Bahnhofstrasse 32, Zürich
  12. TOD'S Boutique, Bahnhofstrasse 32, Zürich
  13. Audemars Piguet, Boutique Zurich, Bahnhofstrasse 32, Zürich
  14. Grieder, Bahnhofstrasse 30, Zürich
  15. ─ 20. no more results at Bahnhofstrasse 30, Zürich
网友答案:

Here is a little snippet straight from Google that show you how to use the "prediction" API to get a list of suggested places:

function initCallback() {
    var getDetailsCallback = function(place, status) {
        if (status != google.maps.places.PlacesServiceStatus.OK) {
            alert(status);
            return;
        }    // write details of place to console
        console.log('Place');
        console.log(place);
    };
    var getQueryPredictionsCallback = function(predictions, status) {
        if (status != google.maps.places.PlacesServiceStatus.OK) {
            alert(status);
            return;
        }
        // write predictions to the console
        console.log('Predictions');
        console.log(predictions);

        // get details of each prediction
        var map = new google.maps.Map(document.createElement('div'));
        var placesService = new google.maps.places.PlacesService(map);
        predictions.forEach(function(prediction) {
            placesService.getDetails({ placeId: prediction.place_id }, getDetailsCallback);  
        });
    };

    var autocompleteService = new google.maps.places.AutocompleteService();
    autocompleteService.getQueryPredictions({ input: '119 Harley Street' }, getQueryPredictionsCallback);
}
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?&libraries=places&callback=initCallback" async defer></script>
分享给朋友:
您可能感兴趣的文章:
随机阅读: