2012-02-15 78 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

相關問題