从获取的坐标中获取位置名称

Nom*_*man 17 android location

我有什么:目前我的应用程序只告诉我当前位置的坐标.

我想要的:从gps提取的坐标中获取位置名称,以便我可以知道我到底在哪里.(位置名称)

Vin*_*kla 31

以下是从获取long-lat到获取地址的完整代码:

LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
String provider = locationManager.getBestProvider(new Criteria(), true);

Location locations = locationManager.getLastKnownLocation(provider);
List<String>  providerList = locationManager.getAllProviders();
if(null!=locations && null!=providerList && providerList.size()>0){                 
double longitude = locations.getLongitude();
double latitude = locations.getLatitude();
Geocoder geocoder = new Geocoder(getApplicationContext(), Locale.getDefault());                 
try {
    List<Address> listAddresses = geocoder.getFromLocation(latitude, longitude, 1);
    if(null!=listAddresses&&listAddresses.size()>0){
        String _Location = listAddresses.get(0).getAddressLine(0);
    }
} catch (IOException e) {
    e.printStackTrace();
}

}
Run Code Online (Sandbox Code Playgroud)

  • 有时这种反向地理编码技术不会给出任何地址.那么每当我需要时,如何从lat-long获取地址? (2认同)

Hus*_*ain 8

你可以GeoCoder在android.location中找到它们.Geocoder包.该 JavaDoc中插上U充分的解释.你的可能样本.

 List<Address> list = geoCoder.getFromLocation(location
                .getLatitude(), location.getLongitude(), 1);
        if (list != null & list.size() > 0) {
            Address address = list.get(0);
            result = address.getLocality();
            return result;
Run Code Online (Sandbox Code Playgroud)

result将返回位置的名称.


Kri*_*hna 6

在这里我给了一个只是通过这个函数的纬度和经度,然后你得到了与这个纬度和经度相关的所有信息.

public void getAddress(double lat, double lng) {
    Geocoder geocoder = new Geocoder(HomeActivity.mContext, Locale.getDefault());
    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        String add = obj.getAddressLine(0);
        GUIStatics.currentAddress = obj.getSubAdminArea() + ","
                + obj.getAdminArea();
        GUIStatics.latitude = obj.getLatitude();
        GUIStatics.longitude = obj.getLongitude();
        GUIStatics.currentCity= obj.getSubAdminArea();
        GUIStatics.currentState= obj.getAdminArea();
        add = add + "\n" + obj.getCountryName();
        add = add + "\n" + obj.getCountryCode();
        add = add + "\n" + obj.getAdminArea();
        add = add + "\n" + obj.getPostalCode();
        add = add + "\n" + obj.getSubAdminArea();
        add = add + "\n" + obj.getLocality();
        add = add + "\n" + obj.getSubThoroughfare();

        Log.v("IGA", "Address" + add);
        // Toast.makeText(this, "Address=>" + add,
        // Toast.LENGTH_SHORT).show();

        // TennisAppActivity.showDialog(add);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
Run Code Online (Sandbox Code Playgroud)

我希望你能得到你答案的解决方案.