简体   繁体   English

使用javascript更新Google Places API结果

[英]Update Google Places API results using javascript

I'm using Google Places API to get location details using javascript. 我正在使用Google Places API使用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)']

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 限制Google Places Api结果 - Restricting Google Places Api results 为什么使用Google Places JavaScript客户端库返回“ NaN”,而在API URL中进行硬编码则返回准确的结果? - Why does using the Google Places JavaScript client library return “NaN”, while hardcoding in a API URL returns accurate results? 谷歌地图地方 Javascript API - Google Maps Places Javascript API 使用 Javascript 从 Google Places 搜索 API 获取纬度和经度 - Getting Latitude and Longitude from Google Places search api using Javascript 使用 Google Maps Javascript API 查找最近的地点? - Find the closest places using Google Maps Javascript API? 如何使用Google Places和JavaScript API循环浏览其他页面? - How to loop through additional pages using google places and javascript API? 如何使用Google Maps Places API的结果更新Angular范围参数? - How do you update an Angular scope parameter with results from Google Maps Places API? 将 Fetch 与 Google Places API 结合使用 - Using Fetch with Google Places API 使用javascript上的谷歌地点api获取设备当前位置的附近地点 - Getting nearby places to the device current location using google places api on javascript Javascript,填充谷歌地图结果将API搜索结果放入下拉列表框 - Javascript, populate the result of Google maps places API search results into Dropdown list box
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM