2011-06-02 59 views
0

所有我想要做的是返回從最後一個知道位置的長/緯度值,如果這是null然後拉新的coords。Android位置管理器?

我很新的這一點,這是我有:

import android.app.Activity; 
import android.content.Context; 
import android.location.Location; 
import android.location.LocationManager; 
import android.os.Bundle; 
import android.widget.TextView; 

public class GPS extends Activity { 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


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


     TextView output; 

     output = (TextView) findViewById(R.id.output); 


     output.append("\n\nLocations (starting with last known):"); 
     Location location = locationManager.getLastKnownLocation(bestProvider); 
     printLocation(location); 



    } 
} 
+0

你得到了什麼錯誤或者你的EditText輸出是什麼? – 2011-06-02 16:40:19

+0

它只是強制關閉,我甚至不能運行它。 – Nick 2011-06-02 16:44:35

+0

在這種情況下,您應該發佈LogCat輸出。 – 2011-06-02 16:53:38

回答

1

而且不要忘了ACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION權限添加到您的AndoidManifest.xml。

+0

我有Fine_location但我添加了coarse_location - 仍然強制關閉。謝謝你的建議,我確信這是許多錯誤之一。 – Nick 2011-06-02 16:52:54

+0

如果您要求'ACCESS_FINE_LOCATION'(並授予)您不需要'ACCESS_COARSE_LOCATION',則會自動授予。 – slinden77 2012-08-22 01:51:42

1

檢查位置,如果他們都爲空,那麼你必須使用 locationManager.requestLocationUpdates(...)函數。

我想你也必須實現一個位置監聽器,以在現場條件下有效地處理這個事情。

我已經回答here