2013-06-23 13 views
-4

我想在Android中獲取經緯度形式的手機gps位置。我需要它的代碼?有誰能夠幫助我?我想要在android中獲得gps位置的移動設備。我需要代碼?

+0

我認爲好友,只要用第一行代碼就能讓你開始。在問任何問題之前,請做一些背景研究。由於您今天加入,如果您閱讀以下內容,將會對您有所幫助:http://stackoverflow.com/questions/how-to-ask –

回答

2

This可以提供幫助。 否則,只需在上述搜索表單中搜索Android gps位置即可。

1

使用LocationManager。

LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
double longitude = location.getLongitude(); 
double latitude = location.getLatitude(); 

到getLastKnownLocation()的調用不會阻塞 - 這意味着它會返回null,如果沒有位置是當前可用 - 所以你可能想在傳遞一個LocationListener的到requestLocationUpdates看看()方法相反,它會給你的位置異步更新。

private final LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     longitude = location.getLongitude(); 
     latitude = location.getLatitude(); 
    } 
} 

lm.requestLocationUpdates(LocationManager.GPS,2000,10,locationListener);

如果您想使用GPS,你需要給你的應用程序的權限ACCESS_FINE_LOCATION:

+0

如果您想使用帶唯一Wi-Fi的LocationManager,則需要ACCESS_COARSE_LOCATION ;) – Rob013

相關問題