如何从给定的格度和位置找到位置名称经度?

时间:2017-08-01 08:39:27

标签: android google-maps google-maps-api-3 geocoding android-fusedlocation

我正在尝试获取位置名称或地址。我已成功通过Google Fused Location API获取经度和纬度。

现在我想使用纬度和经度来获取位置地址(例如:城市名称,道路号或特定地址)。

为此目的,我正在使用谷歌地理编码器,它工作正常。但在某些设备中,位置地址返回空值。

我在网上搜索了这个问题的解决方案,发现有些设备制造商没有在他们的设备中包含此功能。这就是为什么这些设备无法通过反向地理编码方法找到地址的原因。 Here is the link of that information

那么有没有其他方法可以在没有Geoconding的情况下将地址名称作为字符串查找?

以下是我通过地理编码获取地址的代码

public static void getAddress(Context context, double LATITUDE, double LONGITUDE) {


    try {
        Geocoder geocoder = new Geocoder(context, Locale.getDefault());
        List<Address> addresses = geocoder.getFromLocation(LATITUDE, LONGITUDE, 1);
        if (addresses != null && addresses.size() > 0) {



            String address = addresses.get(0).getAddressLine(0); // If any additional address line present than only, check with max available address lines by getMaxAddressLineIndex()
            String city = addresses.get(0).getLocality();
            String state = addresses.get(0).getAdminArea();
            String country = addresses.get(0).getCountryName();
            String postalCode = addresses.get(0).getPostalCode();
            String knownName = addresses.get(0).getFeatureName(); // Only if available else return NULL

            Log.d(TAG, "getAddress:  address" + address);
            Log.d(TAG, "getAddress:  city" + city);
            Log.d(TAG, "getAddress:  state" + state);
            Log.d(TAG, "getAddress:  postalCode" + postalCode);
            Log.d(TAG, "getAddress:  knownName" + knownName);

        }
    } catch (IOException e) {
        e.printStackTrace();
    }
    return;
}

1 个答案:

答案 0 :(得分:0)

使用它为我工作

public class LocationAddress {
    private static final String TAG = "LocationAddress";

    private static String area = null;

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


                        StringBuilder sb = new StringBuilder();
                        for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
                            sb.append(address.getAddressLine(i)).append("\n");
                        }

                        area = address.getSubLocality();

                        sb.append(address.getLocality()).append("\n");
                        sb.append(address.getPostalCode()).append("\n");
                        sb.append(address.getCountryName());
                        result = sb.toString();
                    }
                } catch (IOException e) {
                    Log.e(TAG, "Unable connect to Geocoder", e);
                } finally {
                    Message message = Message.obtain();
                    message.setTarget(handler);
                    if (result != null) {
                        message.what = 1;
                        Bundle bundle = new Bundle();
                        /*result = "Latitude: " + latitude + " Longitude: " + longitude +
                                "\n\nAddress:\n" + result;*/
                        bundle.putString("address", result);
                        bundle.putString("area",area);
                        message.setData(bundle);
                    } else {
                        message.what = 1;
                        Bundle bundle = new Bundle();
                       /* result = "Latitude: " + latitude + " Longitude: " + longitude +
                                "\n Unable to get address for this lat-long.";*/
                        bundle.putString("address", result);
                        bundle.putString("area",area);
                        message.setData(bundle);
                    }
                    message.sendToTarget();
                }
            }
        };
        thread.start();
    }
}

要在Activity使用

中拨打此电话
 LocationAddress locationAddress = new LocationAddress();
                locationAddress.getAddressFromLocation(latitude,longitude,
                        getApplicationContext(), new GeocoderHandler());

<强> GeocoderHandler

private class GeocoderHandler extends Handler {
        @Override
        public void handleMessage(Message message) {
            String locationAddress  = null;
            String area = null;
            switch (message.what) {
                case 1:
                    Bundle bundle = message.getData();
                    locationAddress = bundle.getString("address");
                    area = bundle.getString("area");
                    break;
                default:
                    locationAddress = null;
            }

            if(locationAddress != null)
            locationAddress=locationAddress.replaceAll("[\r\n]+", " ");

            Log.d("===========>>>",area);
        Log.d("===========>>>>",locationAddress);
        }
    }