gpt4 book ai didi

即使设备在同一个地方,Android Google Maps API GPS 位置也会发生变化

转载 作者:行者123 更新时间:2023-12-04 17:59:09 25 4
gpt4 key购买 nike

我正在为我的应用程序使用 Google Maps API。即使设备在同一个地方,我的 GPS 位置也会发生变化,据说该位置每 6 秒更新一次,因此它会不断更新,但每次更新时它也会移动一点,有时甚至移动很多。我的代码如下。我还将添加屏幕截图。

package b.a.location_tracker;

import android.graphics.Color;
import android.location.Location;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.maps.model.Polyline;
import com.google.android.gms.maps.model.PolylineOptions;


public class Map extends FragmentActivity implements OnMapReadyCallback, LocationListener, GoogleApiClient.ConnectionCallbacks {

private GoogleMap mMap;
Marker m;
private GoogleApiClient mGoogleApiClient;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_map);

SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);

this.buildGoogleApiClient();

mGoogleApiClient.connect();

}

private synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this).addApi(LocationServices.API)
.build();
}

@Override
public void onMapReady(GoogleMap googleMap) {

mMap = googleMap;
LatLng sydney = new LatLng(28.6139, 77.2090);
m=mMap.addMarker(new MarkerOptions().position(sydney).title("Delhi City"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 20));
}

@Override
public void onConnected(Bundle bundle) {
LocationRequest mLocationRequest = createLocationRequest();
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (LocationListener) this);

}

@Override
public void onConnectionSuspended(int i) {
Log.d("TAG", "Connection to Google API suspended");

}

private LocationRequest createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(6000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
return mLocationRequest;
}

@Override
public void onLocationChanged(Location location) {

LatLng sydney = new LatLng(location.getLatitude(), location.getLongitude());
LatLng a=m.getPosition();
m.remove();
m=mMap.addMarker(new MarkerOptions().position(sydney).title("Your Location"));
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, mMap.getCameraPosition().zoom));
Polyline line = mMap.addPolyline(new PolylineOptions().add(a,sydney).width(5).color(Color.RED));

}

请记住我的手机在同一个地方,因此它不应该移动。

This is a screenshot of the app

最佳答案

您是否尝试将 minDisplacementDistance 添加到您的 LocationRequest 中?

private LocationRequest createLocationRequest() {
LocationRequest mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(6000);
mLocationRequest.setFastestInterval(1000);
mLocationRequest.setSmallestDisplacement(200);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
return mLocationRequest;
}

如果您将设置此参数(默认情况下它等于 0),如果您的 GPS 找到的新位置在先前找到的位置的 200 米半径范围内,则不会每次调用 LocationChanged 监听器。

现在,您的 onLocationChange 监听器每 6 秒调用一次,最小距离等于 0。由于 GPS 并非 100% 正确,您将获得许多不同的位置。将 smallestDisplacement 更改为更高后,将不会经常调用它。

关于即使设备在同一个地方,Android Google Maps API GPS 位置也会发生变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37629236/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com