如何使用GPS追踪器获得精确的经度和纬度?

时间:2017-12-05 04:44:20

标签: android google-maps

我使用GPS追踪器获得了长途电话,但我没有得到我当前的确切位置,距离我当前位置50米的位置我只使用GPS追踪器。每个答案都很明显。我根据上一个问题的提及,位置不会因设备而异。我没有得到任何有用的答案。

3 个答案:

答案 0 :(得分:1)

使用google客户端api获取准确的位置

你可以参考 - https://www.androidhive.info/2015/02/android-location-api-using-google-play-services/

答案 1 :(得分:0)

您可以使用Google位置API中的FusedLocationProviderClient 只需在gradle(应用程序级别)中应用位置库。

compile 'com.google.android.gms:play-services-location:11.6.0'

并在app level gradle的底部添加此行

apply plugin: 'com.google.gms.google-services'

这是我在mainActivity中的代码

public class MainActivity extends Service implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
LocationRequest mLocationRequest;
GoogleApiClient mGoogleApiClient;
Location mLastLocation;
FusedLocationProviderClient mFusedLocationClient;

private static final String TAG="Logs Details";


@Override
public int onStartCommand(Intent intent, int flags, int startId) {

     buildGoogleApiClient();
    mFusedLocationClient = LocationServices.getFusedLocationProviderClient(this);
    return super.onStartCommand(intent, flags, startId);

}

@Override
public void onCreate() {
    super.onCreate();

}


@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}
protected synchronized void buildGoogleApiClient() {
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
    mGoogleApiClient.connect();
}


@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.e(TAG,"app is connected");

    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(10000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    if (ContextCompat.checkSelfPermission(this,
            android.Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        mFusedLocationClient.requestLocationUpdates(mLocationRequest, mLocationCallback, Looper.myLooper());
    }

}
LocationCallback mLocationCallback = new LocationCallback() {
    @Override
    public void onLocationResult(LocationResult locationResult) {
        for (Location location : locationResult.getLocations()) {
            mLastLocation = location;

            Log.e("latitude",location.latitude);
            Log.e("longitude",location.longitude);



        }
    }



};

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

}

希望这会有所帮助。:)

答案 2 :(得分:0)

使用谷歌的融合位置api,我们可以获得更精确的纬度和长度,但不是100%总是。融合的位置api比GPS追踪器更好。