通过地理编码器在某些设备上工作从地点获取地址

时间:2017-06-01 20:45:42

标签: android google-maps google-geocoder

我正在尝试通过地理编码器从位置获取地址并且它正常工作,但有些时候某些设备无法正常工作

点击标记从地图获取位置并传递给此方法但有时无效并且我的textview设置文本等待获取地址

  private void getAddressFromLocation(final LatLng latlng, final TextView et) {
    et.setText(R.string.text_waiting_for_get_address);
    et.setTextColor(Color.GRAY);
    new Thread(new Runnable() {
        @SuppressLint("LongLogTag")
        @Override
        public void run() {
            Geocoder gCoder = new Geocoder(getActivity());
            try {
                final List<Address> list = gCoder.getFromLocation(
                        latlng.latitude, latlng.longitude, 1);
                if (list != null && list.size() > 0) {
                    address = list.get(0);
                    StringBuilder sb = new StringBuilder();
                    if (address.getAddressLine(0) != null) {
                        if (address.getMaxAddressLineIndex() > 0) {
                            for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { 
                              sb.append(address.getAddressLine(i)).append("-");
                            }
                            sb.append(address.getCountryName());
                        } else {
                            sb.append(address.getAddressLine(0));
                        }
                    }
                    strAddress = sb.toString();
                    strAddress = strAddress.replace("-null", "");
                    strAddress = strAddress.replace("-ایران", "");
                    strAddress = strAddress.replace("null", "");
                    strAddress = strAddress.replace("Unnamed", "");
                }
                if (getActivity() != null) {
                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (!TextUtils.isEmpty(strAddress)) {
                                et.setFocusable(false);
                                et.setFocusableInTouchMode(false);
                                if (et.getTag().toString().equals("origin")) {
                                    et.setText("Origin : " + strAddress + "");
                                } else {
                                    et.setText("Destination : " + strAddress + "");
                                }
                                et.setFocusable(true);
                                et.setFocusableInTouchMode(true);
                            } else {
                                et.setText("");
                            }
                        }
                    });
                } else {
                    return;
                }
            } catch (Exception exc) {
                exc.printStackTrace();
            }
        }
    }).start();
    }

1 个答案:

答案 0 :(得分:0)

听起来像Google问题跟踪器中的以下问题:

https://issuetracker.google.com/issues/35828055

随意加注并添加您的评论。要解决此问题,请尝试使用Geocoding API Web服务。