Google Places API - getQueryPredictions Restrict by Country/City/State?

后端 未结 2 1912
攒了一身酷
攒了一身酷 2021-02-14 10:16

Below is the getQueryPredictions example given by google

service.getQueryPredictions({input: \'pizza near\'}, callback);

Is there a way to rest

2条回答
  •  青春惊慌失措
    2021-02-14 10:46

    Use this similar function (it's a bit more powerfull than getQueryPredictions):

    getPlacePredictions(
                {
                input: "pizza near",
                types: ['(cities)'],
                componentRestrictions: {country: 'fr'}
                }, 
                callback);
    

    Four types are supported: 'establishment' for businesses, 'geocode' for addresses, '(regions)' for administrative regions and '(cities)' for localities.

    Or, if you want to use geyQueryPredictions(), you can do the following trick (but it's not a good way):

    {input: 'pizza near' + ',AR-M'}

    Where 'AR-M' is the Postcode of ARGENTINA, MENDOZA. (where I live) Just look at your location's postcode.

    The, when you show the predictions, do:

    for (var i = 0, max = predictions.length; i < max; i++) {
        var address = predictions[i].description.replace(/AR-M,/g, '');
            ...
        }
    

    Hope that helps.

提交回复
热议问题