将自动完成结果限制在COUNTRY和STATE内 [英] Restrict places autocomplete results to within COUNTRY and STATE

查看:78
本文介绍了将自动完成结果限制在COUNTRY和STATE内的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用此URL来限制我的搜索结果在澳大利亚境内:

I'm currently using this URL to get my results restricted within Australia:

但是我想说的是将搜索范围限制在当前状态,例如新南威尔士州.

But I'd like to say limit my searches to the current state let's say New South Wales.

我尝试了这个: https://maps.googleapis.com/maps/api/place/autocomplete/json?input=393&components=country:AU&state=NSW&types=address&key= [my_api_key]

但是它返回的结果与来自维多利亚州而不是来自新南威尔士州的项目相同.

But it's returning the same number of results with items from Victoria, which is not from NSW.

根据文档:

https://developers.google.com/places/web-service/autocomplete

但这并不表示限制状态.

It does not indicate restricting the state though.

推荐答案

我的解决方案是通过比较州和国家/地区以及Places API返回的描述来过滤传入结果.示例实现如下:

My solution is filter the incoming result by comparing the state and the country with description being returned from Places API. Sample implementation is below:

        JSONObject jsonObj = new JSONObject(jsonResults.toString());
        JSONArray predsJsonArray = jsonObj.getJSONArray("predictions");
        // Extract the Place descriptions from the results
        resultList = new ArrayList<>(predsJsonArray.length());
        for (int i = 0; i < predsJsonArray.length(); i++) {
            if(predsJsonArray.getJSONObject(i) != null){
                String description = predsJsonArray.getJSONObject(i).getString("description");
                if(description != null){
                    if(description.contains(state + ", " + country))
                    resultList.add(new Place(description,
                            predsJsonArray.getJSONObject(i).getString("place_id")));
                }
            }
        }

这篇关于将自动完成结果限制在COUNTRY和STATE内的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆