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

时间:2011-08-03 06:02:58

标签: android location

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

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

3 个答案:

答案 0 :(得分:31)

以下是从获取长地址到获取地址的完整代码:

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();
}

}

答案 1 :(得分:8)

您可以使用android.location。GeoCoder包中提供的GeocoderJavaDocs给出了完整的解释。你可能的样本。

 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;

result将返回该位置的名称。

答案 2 :(得分: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();
    }
}

我希望你能得到答案的答案。