简体   繁体   中英

Update Google Places API results using javascript

I'm using Google Places API to get location details using javascript.

var request = {
            input: request.term,
            componentRestrictions: {country: 'us'},
            types: ['geocode']};

service.getPlacePredictions(request, function (predictions, status) {

    if (status != 'OK') {
        return;
    }
    response($.map(predictions, function (prediction, i) {
        var cty_st = '';

        var pred_terms_lnt = prediction.terms.length;

        if(pred_terms_lnt >= 3){
            cty_st = prediction.terms[pred_terms_lnt - 3].value+', '+prediction.terms[pred_terms_lnt - 2].value;
        }
        else if(pred_terms_lnt >= 2){
            cty_st = prediction.terms[pred_terms_lnt - 2].value;
        }
        else{
            cty_st = prediction.description.replace(", United States", "");
        }

        return {
            label: cty_st,
            value: cty_st,
            ref: prediction.reference,
            id: prediction.id
        }
  }));
 });

I want to show only city and state code from the results fetched and when I search for a location I'm getting duplicate city, state coder in the results. I want to remove these duplicates from the results. How to do that?

在此处输入图片说明

From the above results you can see last two results have same city and state code. I want to show only once.

代替types: ['geocode']使用types: ['(cities)']

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