2012-02-15 80 views
0

我试过使用此代码。它没有给出位置名称。它只给出经度和纬度。获取当前位置使用网络提供商的名称

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); 
+0

你正在尝试在真实的设备或模拟器? – Pratik 2012-02-15 04:55:34

+1

这不是“位置”应该给的东西吗? – 2012-02-15 04:59:26

+0

我正在尝试在真实的设备上,但它不给 – vinodkumar 2012-02-15 06:32:42

回答

-1

试试这个:

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

位置默认情况下是指坐标是纬度和经度 ,这是你做了什么。要获得实际地址,您需要 来对坐标进行地理编码。

要获取当前位置名称有2个步骤。

1)为了得到当前lovation你已经做了,

现在

2)使用地理编码器类来转换纬度长到地址

this answer链接和细节表达。

+1

只是扩大了上面的答案。默认位置意味着坐标和纬度坐标,这就是你所得到的。要获取实际地址,您需要对坐标进行地理编码。 – PravinCG 2012-02-15 17:01:47

+0

谢谢@PravinCG – MKJParekh 2012-02-16 03:46:45

+0

但要使用geocorder类获取位置名称,我们需要Internet连接。但是,如果没有连接到互联网,是不是有办法获得位置名称? – 2015-05-06 14:39:18

相关问题