【问题标题】:Get Current Location Name using Network Provider使用网络提供商获取当前位置名称
【发布时间】:2012-02-15 04:49:35
【问题描述】:

我已尝试使用此代码。它没有给出位置名称。它只给出纬度和经度。

String latLongString;

if (location != null) 
{       
  double lat = location.getLatitude();    
  double lng = location.getLongitude();    
  latLongString = "Lat:" + lat + "\nLong:" + lng;    
   }
      else 
   {
       latLongString = "No location found"; 
   }

myLocationText.setText("Your Current Position is:\n" + latLongString);  

【问题讨论】:

  • 您是在真机还是模拟器中尝试?
  • 这不是Location 应该给予的吗?
  • 我在真实设备上尝试,但它没有给出
  • 不,它应该只给我当前的 loation 名称。但它给了我当前位置的纬度和经度值。

标签: android geolocation


【解决方案1】:

位置默认是指经纬度坐标 这就是你得到的。要获得您需要的实际地址 对坐标进行地理编码。

要获取当前位置名称,需要执行 2 个步骤。

1) 得到你目前的喜爱,

现在

2) 使用 Geocoder 类将经纬度转换为地址

请参阅 this answer 了解链接和详细表达方式。

【讨论】:

  • 只是扩展上面的答案。默认情况下,位置表示坐标,即纬度和经度,这就是您所得到的。要获取实际地址,您需要对坐标进行地理编码。
  • 但是要使用 geocorder 类来获取位置名称,我们需要互联网连接。但是有没有办法在不连接互联网的情况下获取位置名称?
【解决方案2】:

试试这个:

LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
bestProvider = locationManager.getBestProvider(criteria, true);
location = locationManager.getLastKnownLocation(bestProvider);
if (location != null) {
    lat = location.getLatitude();
    lng = location.getLongitude();
} else {
    location = new Location("");
    location.setLatitude((double) 38.0000000000);
    location.setLongitude((double) -97.0000000000);
    lat = location.getLatitude();
    lng = location.getLongitude();
}

【讨论】:

    猜你喜欢
    • 2013-03-16
    • 1970-01-01
    • 1970-01-01
    • 2014-09-06
    • 1970-01-01
    • 2011-10-10
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    相关资源
    最近更新 更多