【问题标题】:Get location name from fetched coordinates从获取的坐标中获取位置名称
【发布时间】:2011-10-18 20:03:19
【问题描述】:

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

我想要什么: 从 GPS 获取的坐标中获取位置名称,这样我就可以知道我到底在哪里。 (地点名称)

【问题讨论】:

标签: android location


【解决方案1】:

以下是从获取经纬度到获取地址的完整代码:

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

}

【讨论】:

  • 有时这种反向地理编码技术不会给出任何地址。那么如何在每次需要时从经纬度获取地址???
  • 是的,它有时不会返回地址。为此你可以实现一个备份机制,比如当你没有得到地址时你再次点击它来获取地址,或者你可以使用google api来获取地址。
  • 使用这个权限:
  • 我的位置总是空的!
【解决方案2】:

您可以使用 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 将返回位置名称。

【讨论】:

    【解决方案3】:

    这里给了我一个,只需在这个函数中传递纬度和经度,然后你就得到了与这个纬度和经度相关的所有信息。

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

    希望你能找到答案。

    【讨论】:

    • 我怎样才能得到我的位置的确切位置,它给出了最大的城市价值,我想要城市内的位置名称
    猜你喜欢
    • 1970-01-01
    • 2012-12-05
    • 2012-05-27
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 2012-09-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多