![](/img/trans.png)
[英]How often does LocationClient.getLastLocation() get updated?
[英]LocationClient getLastLocation() returning null
我是Android編程的新手,並希望從創建一個非常基本的應用程序開始,該應用程序在屏幕上顯示當前位置的緯度和經度。
我正在為我的三星Galaxy S3開發並讀取你需要啟動手機才能返回GPS 。
我已嘗試在onConnected()
方法中使用LocationManager上的requestLocationUpdates()
方法調用。 但是我發現LocationClient仍然返回一個null
位置。
誰能告訴我我做錯了什么?
這是我的MainActivity:
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.location.LocationClient;
public class MainActivity extends FragmentActivity implements
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener {
private LocationClient mLocationClient;
private Location mCurrentLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationClient = new LocationClient(this, this, this);
}
@Override
protected void onStart() {
super.onStart();
mLocationClient.connect();
}
@Override
protected void onStop() {
mLocationClient.disconnect();
super.onStop();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onConnectionFailed(ConnectionResult arg0) {
// TODO Auto-generated method stub
}
@Override
public void onConnected(Bundle arg0) {
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
LocationManager manager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
manager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0,
new LocationListener() {
@Override
public void onStatusChanged(String provider, int status,
Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(final Location location) {
}
});
mCurrentLocation = mLocationClient.getLastLocation();
TextView latTextView = (TextView) findViewById(R.id.latitude_text);
latTextView.setText(Double.toString(mCurrentLocation.getLatitude()));
TextView longTextView = (TextView) findViewById(R.id.longitude_text);
longTextView.setText(Double.toString(mCurrentLocation.getLongitude()));
}
@Override
public void onDisconnected() {
Toast.makeText(this, "Disconnected. Please re-connect.",
Toast.LENGTH_SHORT).show();
}
}
我的Android清單看起來像這樣:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.willigetwet.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
運行調試器時,我發現調用mLocationClient.getLastLocation()
后mCurrentLocation為null。
我會非常感謝任何幫助!
mLocationClient.getLastLocation();
如果您沒有設備保存的任何最后位置,則可能返回null。 如果您閱讀其文檔 ,它說;
It returns the best most recent location currently available. If a location is not available, which should happen very rarely, null will be returned
同樣在onLocationChanged
方法中,當您的位置發生變化時,您可能希望將位置設置為最新位置。
@Override
public void onLocationChanged(final Location location) {
mCurrentLocation = location;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.