我想每分钟获取一次用户的GPS位置。我正在使用以下服务类别。但有时位置错误。我没有关闭GPS或数据。我正在使用Samsung Mobile(J7 Max)。
例如,在所附的图像( GPS位置图像)中,我位于位置 B 。但是GPS给出了位置 A 。
如何解决此错误并获得较高的准确位置?
LocationService.java
public class LocationService extends Service {
private FusedLocationProviderClient mFusedLocationProviderClient;
private SettingsClient mSettingsClient;
private LocationCallback mLocationCallback;
private LocationRequest mLocationRequest;
private LocationSettingsRequest mLocationSettingsRequest;
private long UPDATE_INTERVAL_IN_MILLISECONDS;
private long FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS;
@Override
public void onCreate() {
super.onCreate();
mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);
mSettingsClient = LocationServices.getSettingsClient(this);
UPDATE_INTERVAL_IN_MILLISECONDS = 1 * 60 * 1000;
FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS = UPDATE_INTERVAL_IN_MILLISECONDS / 2;
createLocationCallback();
createLocationRequest();
buildLocationSettingsRequest();
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startLocationUpdates();
return START_STICKY;
}
@Override
public void onDestroy() {
stopLocationUpdates();
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
private void createLocationCallback() {
mLocationCallback = new LocationCallback() {
@SuppressLint("MissingPermission")
@Override
public void onLocationResult(LocationResult locationResult) {
super.onLocationResult(locationResult);
try {
Location location = locationResult.getLastLocation();
//.... my code ....//
} catch (Exception e) {
e.printStackTrace();
}
}
};
}
@SuppressLint("RestrictedApi")
private void createLocationRequest() {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_IN_MILLISECONDS);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
}
private void buildLocationSettingsRequest() {
LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder();
builder.addLocationRequest(mLocationRequest);
mLocationSettingsRequest = builder.build();
}
private void startLocationUpdates() {
mSettingsClient.checkLocationSettings(mLocationSettingsRequest)
.addOnSuccessListener(new OnSuccessListener<LocationSettingsResponse>() {
@SuppressLint("MissingPermission")
@Override
public void onSuccess(LocationSettingsResponse locationSettingsResponse) {
mFusedLocationProviderClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
}
});
}
private void stopLocationUpdates() {
mFusedLocationProviderClient.removeLocationUpdates(mLocationCallback)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
}
});
}
}
清单
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-feature android:name="android.hardware.location.gps" />
build.gradle(项目)
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.1.3'
classpath 'com.google.gms:google-services:3.1.1'
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
build.gradle(模块)
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "xxx.xxx.xxxxxxx"
minSdkVersion 18
targetSdkVersion 26
versionCode 100
versionName "1.0.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.google.android.gms:play-services-location:12.0.1'
implementation 'com.google.android.gms:play-services-maps:12.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
apply plugin: 'com.google.gms.google-services'
答案 0 :(得分:0)
我认为造成这种情况的原因不是您的应用,而是错误是由电话造成的。为此,您可以在另一部手机上测试您的应用。