【问题标题】:Geocoder is not working in android above kitkat version地理编码器在 kitkat 版本以上的 android 中不起作用
【发布时间】:2017-10-04 05:54:54
【问题描述】:

我在我的 Android 应用程序中使用地理编码器来使用纬度和经度获取国家/地区名称。我发现它在 kitkat 版本及以下版本中运行良好。但是当我在上述版本中测试我的应用程序时,它给出了 null。所以我的问题很简单,即如何在 kitkat 版本之上使用地理编码器。 如果有任何其他更好的选择而不是地理编码器,那么请建议我! 提前致谢!

【问题讨论】:

  • 你是否获得了Api 22以上的运行时权限?
  • 检查运行时权限,如果问题仍然存在,请使用 Goole Location API 获取位置
  • 是的,已经添加了权限,但不起作用。

标签: java android google-maps geolocation geocoding


【解决方案1】:

我发布了 Geocoder 的代码。跟着它

//Keep this GeocodingLocation.java in a separate file. 

    public class GeocodingLocation {

        private static final String TAG = "GeocodingLocation";
        public static void getAddressFromLocation( final String locationAddress,
                                                  final Context context, final Handler handler) {
            Thread thread = new Thread() {
                @Override
                public void run() {
                    Geocoder geocoder = new Geocoder(context, Locale.getDefault());
                    String result = null;
                    String latitude = null;
                    String longitude = null;
                    try {
                        List<Address> addressList = geocoder.getFromLocationName(locationAddress, 1);
                        if (addressList != null && addressList.size() > 0) {

                            Log.e("GeocodingLocation --> getAddressFromLocation ==>" +
                                    addressList.toString());

                            Address address = (Address) addressList.get(0);
                            StringBuilder sb = new StringBuilder();
                            latitude = String.valueOf(address.getLatitude());
                            longitude = String.valueOf(address.getLongitude());

                            Logger.infoLog("GeocodingLocation --> Lat & Lang ==>" + latitude +"  "+longitude);
                            //sb.append(address.getLatitude()).append("\n");
                            //sb.append(address.getLongitude()).append("\n");

                        }
                    } catch (IOException e) {
                        Log.e(TAG, "Unable to connect to Geocoder", e);
                    } finally {
                        Message message = Message.obtain();
                        message.setTarget(handler);
                        if (latitude != null && longitude != null) {
                            message.what = 1;
                            Bundle bundle = new Bundle();
                            bundle.putString("latitude", latitude);
                            bundle.putString("longitude", longitude);
                            message.setData(bundle);
                        } else {
                            message.what = 1;
                            Bundle bundle = new Bundle();
                            result = "Address: " + locationAddress +
                                    "\n Unable to get Latitude and Longitude for this address location.";
                            bundle.putString("address", result);
                            message.setData(bundle);
                        }
                        message.sendToTarget();
                    }
                }
            };
            thread.start();
        }
    }

从您的片段中调用类。

private void getAddressLatitudeLongitude(String branchAddress) {
        Log.e("getAddressLatitudeLongitude ==>" + branchAddress);

        GeocodingLocation.getAddressFromLocation(branchAddress, thisActivity, new GeocoderHandler());
    }

将此类作为内部类保留在同一片段中

private class GeocoderHandler extends Handler {
        @Override
        public void handleMessage(Message message) {
            switch (message.what) {
                case 1:
                    try {
                        Bundle bundle = message.getData();
                        latitude = bundle.getString("latitude");
                        longitude = bundle.getString("longitude");

                        Log.e("CreateBranch --> GeocoderHandler ==>" + latitude + "  " + longitude);
                    }catch (Exception e){
                        e.printStackTrace();
                    }
                    break;
                default:
                    latitude = null;
                    longitude = null;
            }
            latitudeList.add(latitude);
            longitudeList.add(longitude);

            if(latitude==null || longitude==null){
                showAlertDialog("Please enter correct address", Constants.APP_NAME);
            }

            Log.e("Latitude list =>" + latitudeList);
            Log.e("Longitude list =>" + longitudeList);
        }
    }

输出将得到纬度和经度。希望这个答案有帮助。

【讨论】:

  • 我想你没有得到我的问题。我不是要求从地址中获取纬度和经度。我要求从纬度和经度获取位置地址。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-17
相关资源
最近更新 更多