我正在使用 google fused location API,并且能够成功实现它,但看起来像这样我有日志:
Location[fused 31******,30****** acc=27 et=+16h8m55s272ms]
我想不出问题。
我正在使用 google fused location API,并且能够成功实现它,但看起来像这样我有日志:
Location[fused 31******,30****** acc=27 et=+16h8m55s272ms]
我想不出问题。
这是在后台运行并经过测试的定位服务
public class LocationTrackingService extends Service implements
GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener {
protected static final String TAG = "location-updates-sample";
private Context context;
public static final long UPDATE_INTERVAL_IN_MILLISECONDS = 1000;
public static final long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
protected Location mCurrentLocation;
protected Boolean mRequestingLocationUpdates;
private String locationIntent;
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "Connected to GoogleApiClient");
if (mCurrentLocation == null) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
startLocationUpdates();
}
@Override
public void onConnectionSuspended(int i) {
mGoogleApiClient.disconnect();
}
@Override
public void onLocationChanged(Location location) {
if (locationIntent.equals(AppConstants.LOCATION_INTENT)) {
EventBus.getDefault().post(new CurrentLocation(location));
Log.e(TAG, "LOCATION CHANGED : " + " " + DateFormat.getTimeInstance().format(new Date()) + " " + location.getLatitude() + " " + location.getLongitude());
//stopLocationUpdates();
//mGoogleApiClient.disconnect();
} else if (location.equals(AppConstants.LOCATION_TRACK_INTENT)) {
startLocationUpdates();
Log.e(TAG, "TRACK LOCATION : " + " " + DateFormat.getTimeInstance().format(new Date()) + " " + location.getLatitude() + " " + location.getLongitude());
}
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
buildGoogleApiClient();
mGoogleApiClient.connect();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent != null) {
context = this;
locationIntent = intent.getStringExtra(AppConstants.LOCATION_INTENT);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
}
return START_STICKY;
}
@Override
public void onDestroy() {
// handler.removeCallbacks(sendUpdatesToUI);
super.onDestroy();
Log.e("STOP_SERVICE", "STOP_SERVICE");
stopLocationUpdates();
mGoogleApiClient.disconnect();
}
@Override
public void onCreate() {
super.onCreate();
buildGoogleApiClient();
mGoogleApiClient.connect();
Toast.makeText(this, "service", Toast.LENGTH_LONG).show();
}
/**
* Builds a GoogleApiClient. Uses the {@code #addApi} method to request the
* LocationServices API.
*/
protected synchronized void buildGoogleApiClient() {
Log.e(TAG, "Building GoogleApiClient");
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
createLocationRequest();
}
/**
* Sets up the location request. Android has two location request settings:
* {@code ACCESS_COARSE_LOCATION} and {@code ACCESS_FINE_LOCATION}. These settings control
* the accuracy of the current location. This sample uses ACCESS_FINE_LOCATION, as defined in
* the AndroidManifest.xml.
* <p>
* When the ACCESS_FINE_LOCATION setting is specified, combined with a fast update
* interval (5 seconds), the Fused Location Provider API returns location updates that are
* accurate to within a few feet.
* <p>
* These settings are appropriate for mapping applications that show real-time location
* updates.
*/
protected void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
}
/**
* Requests location updates from the FusedLocationApi.
*/
protected void startLocationUpdates() {
mRequestingLocationUpdates = true;
return;
}
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
/**
* Removes location updates from the FusedLocationApi.
*/
protected void stopLocationUpdates() {
// It is a good practice to remove location requests when the activity is in a paused or
// stopped state. Doing so helps battery performance and is especially
// recommended in applications that request frequent location updates.
// The final argument to {@code requestLocationUpdates()} is a LocationListener
// (http://developer.android.com/reference/com/google/android/gms/location/LocationListener.html).
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
}}
我设法通过将它加倍来得到它
Double.toString(location.getLatitude())