好的,我花了大量的時間搜索和閱讀,而不僅僅是這個網站。大多數線程處理GPS或試圖讓它在模擬器中工作。 developer.android.com上的文檔只解釋了一點。我無法得到一個簡單的工作。與大多數遇到此問題的人一樣,我只想從網絡提供商處獲得最後一個已知位置,即使在我使用Google地圖(使用網絡提供商,因爲GPS已關閉)之後,它也不會得到它,並且它向我展示了只有1/8到1/4英里遠。好吧,所以「提供者」(手機信號塔/ Wi-Fi如果打開)正在修復Google地圖。我嘗試我的應用程序,無法獲取最後一個已知位置。我甚至沒有試圖在地圖中顯示它,我只是在按下按鈕來獲取位置後,將其經緯度和長座標打印到屏幕上。該文件沒有說明最後一個地點必須從同一活動中完成。它表示它將返回指定提供者的最後一個已知位置。我正在指定NETWORK_PROVIDER,文檔中提到的是手機信號塔和Wi-Fi。 (是的,我已在清單中列出)getLastKnownLocation from NETWORK_PROVIDER on phone returns null
有沒有人知道這是如何工作的?我還沒有看到一個很好的解釋,即使在谷歌的網站上。如果你說不出來,我很沮喪。這顯然是我正在做(或不是)的事情,所以我不會責怪除了我自己以外的任何人/別人,除非文檔對於我的需求來說太短了。
更新:我現在正在使用位置監聽器並獲取位置,我推測最後一個位置。似乎合乎邏輯的是能夠在沒有通過位置偵聽器啓動位置更新的情況下獲取最後一個位置,但我沒有,現在我正在使用位置偵聽器。
現在我的問題是我無法獲取位置改變。我的代碼現在不執行任何操作,除非嘗試通過偵聽器獲取位置更新,並且接受按鈕按下以搶奪最後一個已知位置。谷歌地圖會告訴我我在哪裏,但我一直在20英里外的地方找到我的代碼。我有一種感覺,我可能真的會得到一個新的位置,但我可能會錯誤地使用最終/靜態聲明,這些聲明使我無法在需要時存儲和使用該位置。我的清單具有internet和fine_location權限,活動的xml工作正常 - 它只是提供一個按鈕來打開/關閉位置的實時搜索,一個手動檢索lastknownlocation的按鈕以及一個顯示經緯度的文本視圖。我試圖去除非必需品以防止它太長。
public class RMain extends Activity {
public boolean islivesearchon;
public static double netlistentime = 0 * 60 * 1000; // minutes * 60 sec/min * 1000 for milliseconds
public static double netlistendistance = 0 * 1609.344; // miles * conversion to meters
public static double gpslistentime = 30 * 60 * 1000; // minutes * 60 sec/min * 1000 for milliseconds
public static double gpslistendistance = 0 * 1609.344; // miles * conversion to meters
private static Location currentlocation;
private LocationManager locationManager;
private LocationListener locationListener;
public static Criteria criteria;
public TextView latview;
public TextView longview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rmain);
latview = (TextView) findViewById(R.id.display_latitude);
longview = (TextView) findViewById(R.id.display_longitude);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// setup bestProvider
criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
String bestprovider = locationManager.getBestProvider(criteria, true);
// get an initial current location
currentlocation = locationManager.getLastKnownLocation(bestprovider);
latview.setText(Double.toString(currentlocation.getLatitude()));
longview.setText(Double.toString(currentlocation.getLongitude()));
// Define locationListener interface
locationListener = new LocListener();
// React to LiveSearch toggle button press
final Button livesearch = (Button) findViewById(R.id.button_livesearch);
livesearch.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView livesearchview = (TextView) findViewById(R.id.button_livesearch);
boolean isNetEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
if(islivesearchon) {
locationManager.removeUpdates(locationListener);
islivesearchon = false;
livesearchview.setText("Live Search is OFF");
}
else {
// ideally should check for !isProviderEnabled first, then provide option for turning it on
if(isNetEnabled) {
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, (long) netlistentime, (float) netlistendistance, locationListener);
islivesearchon = true;
livesearchview.setText("Live Search is ON");
}
// ideally should check for !isProviderEnabled first, then provide option for turning it on
if(isGpsEnabled) {
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, (long) gpslistentime, (float) gpslistendistance, locationListener);
islivesearchon = true;
livesearchview.setText("Live Search is ON");
}
}
}
});
// Respond to Get Location button click to get last known location
final Button getlocation = (Button) findViewById(R.id.button_getlocation);
getlocation.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String bestprovider = locationManager.getBestProvider(criteria, true);
Location lastlocation = locationManager.getLastKnownLocation(bestprovider);
latview.setText(Double.toString(lastlocation.getLatitude()));
longview.setText(Double.toString(lastlocation.getLongitude()));
}
});
}
private class LocListener implements LocationListener {
@Override
public void onLocationChanged(Location latestlocation) {
// Called when a new location is found by the network location provider.
currentlocation.set(latestlocation);
latview.setText(Double.toString(currentlocation.getLatitude()));
longview.setText(Double.toString(currentlocation.getLongitude()));
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {}
@Override
public void onProviderEnabled(String provider) {}
@Override
public void onProviderDisabled(String provider) {}
};
}
你在手機裏模擬嗎? – 2012-12-11 06:42:53
只需在剛啓動應用程序時設置任何位置,然後檢索getLastKnownLocation ... –
謝謝你們。是的,我只在手機內部模擬 - Eclipse中的模擬器並不總是適合我。 – crispy