這是代碼,您需要:
protected LocationCallback mLocationCallback = new LocationCallback()
{
@Override
public void onLocationAvailability(LocationAvailability locationAvailability) {
super.onLocationAvailability(locationAvailability);
if(!locationAvailability.isLocationAvailable())
{// No location available
checkLocationSettings();
Snackbar snackLocationUnavailable = Snackbar.make(findViewById(R.id.myCoordinatorLayout), R.string.location_unavailable, Snackbar.LENGTH_LONG);
snackLocationUnavailable.show();
}
else
{
Snackbar snackLocationAvailable = Snackbar.make(findViewById(R.id.myCoordinatorLayout), R.string.location_available, Snackbar.LENGTH_LONG);
snackLocationAvailable.show();
}
}
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
mLastLocation = locationResult.getLastLocation();
mMyLatLng = new LatLng(valueOf(mLastLocation.getLatitude()), valueOf(mLastLocation.getLongitude()));
meMarker.setPosition(mMyLatLng);
mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(mMyLatLng, 15));
Snackbar snak2 = Snackbar.make(findViewById(R.id.myCoordinatorLayout), R.string.location_updated, Snackbar.LENGTH_INDEFINITE);
snak2.show();
}
};
...
onCreate(){
// Build GoogleApiClient
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
//`Build LocationRequest
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(2000) // 2 seconds, in milliseconds
.setFastestInterval(1000); // 1 second, in milliseconds
`
//
Build LocationSettingsRequest
LocationSettingsRequest.Builder lRbuilder = new LocationSettingsRequest.Builder();
lRbuilder.addLocationRequest(mLocationRequest);
mLocationSettingsRequest = lRbuilder.build();
lRbuilder.setAlwaysShow(true);
// ...
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, mLocationCallback, null);
//...
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, mLocationCallback);
這個問題和它的答案是將近三十歲,在這一點上。在Android年代,這是一個次要的永恆。您可以考慮閱讀文檔以獲取更新的指導:http://developer.android.com/guide/topics/location/strategies.html – CommonsWare 2013-03-05 01:35:31
您是否發現了某些內容? – Castiblanco 2013-09-10 18:21:14