地理编码器返回地址。

4

我正在使用Geocoder类来获取用户在EditText中输入的字符串地址的纬度和经度。

有趣的是,它可以返回“q”、“qq”、“n”、“N”等查询的结果。有没有办法使其更好(验证或其他服务)?

if (!s.toString().isEmpty()) {
          try {
                 List<Address> addresses = geocoder.getFromLocationName(s.toString(), 4);
                            if (addresses.size() > 0) {
                                Address userAddress = addresses.get(0);
                                double latitude = userAddress.getLatitude();
                                double longitude = userAddress.getLongitude();
                                mRestaurantLatLng = new LatLng(latitude, longitude);
                                if (userAddress != null) {
                                    mPin.setVisibility(View.VISIBLE);
                                } else {
                                    mPin.setVisibility(View.GONE);
                                    mRestaurantLatLng = null;
                                }
                            } else {
                                mPin.setVisibility(View.GONE);
                                mRestaurantLatLng = null;
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
1个回答

0

你可以直接使用另一个服务,或者根据你想要的详细程度修剪结果。

就像这样:

List<Address> results = GeoCoder.getFromLocationName(query, 100);
filter(results);
if (results.size() > 0) {
   // Do Stuff
}


....

void filter(List<Address> results) {
   for (int i = 0; i < results.size(); i++) {
      Address address = results.get(i);
      if (!address.hasLatitude() || !address.hasLongitude()) {
          results.remove(i);
      }
      // remove any that dont match your desired detail level
      ...
}

实际上,地址"q"具有纬度和经度。 - Kyryl Zotov
对,但它还有其他的特点吗?纬度和经度很容易得到。你只需要选择一对就可以覆盖足够的区域,这并不重要。但是,如果你能够获得足够详细的地址信息,那么它可能更符合你的需求。我的观点是根据你的需求添加自己的过滤器。 - Greg Giacovelli
如果你有空的话,可以尝试查询一下我的问题中提到的地点。你会得到街道、城市,甚至是门牌号码! - Kyryl Zotov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接