0
我已经实现了这个代码,以获得当前位置更新每移动10步,但我没有获得当前的经纬度,因为我移动10步或更多,我有问题是现实的设置最小距离10步?如何获得当前位置更新每移动10步,我就会得到当前位置更新
此外,每次我的代码运行时,我都会得到不同的经纬度数字,如果我比较这些经纬度,我每次运行应用程序时都会在Google地图网站上显示当前位置,其大约400米远离我目前的位置,为什么会发生。
private FusedLocationProviderClient mFusedLocationClient;
private GoogleApiClient mGoogleApiClient;
private GoogleMap mMap;
private TextView tvLat;
private TextView tvLng;
Map<String, Double> latlng = new HashMap<>();
DatabaseReference data;
private double lat;
private double lng;
CameraPosition CurrentLocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SingletonConnectClass instence = SingletonConnectClass.getInstance();
data = instence.firebaseSetup();
setContentView(R.layout.activity_maps);
initView();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
mFusedLocationClient
= LocationServices.getFusedLocationProviderClient(this);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
mMap.setMyLocationEnabled(true);
mMap.setMapType(GoogleMap.MAP_TYPE_TERRAIN);
getCurrentLocation();
buildClient();
}
private void getCurrentLocation(){
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(this, new OnSuccessListener<Location>() {
@Override
public void onSuccess(final Location location) {
CurrentLocation = new CameraPosition.Builder().target(new
LatLng(location.getLatitude(),
location.getLongitude()))
.zoom(15.5f)
.bearing(0)
.tilt(25)
.build();
//changed camera position to current location and added marker there
}
这里是获取当前位置更新,如果我在连接回调onConnected
public void onConnected(Bundle bundle) {
mFusedLocationClient.requestLocationUpdates(requestLocation(),
new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
for (final Location location : locationResult.getLocations()) {
//not getting changing current lat lng if i move 10 steps
tvLat.setText(Double.toString(location.getLatitude()));
tvLng.setText(Double.toString(location.getLongitude()));
lat = location.getLatitude();
lng = location.getLongitude();
//as result not camera position is not changed
changeCameraPosition(CameraUpdateFactory.newLatLngZoom(
new LatLng(location.getLatitude(), location.getLong)
}}, Looper.myLooper());
玩什么服务我在我的代码后失踪移动10步的代码? onLocationChanged?我必须手动实现onLocationChanged吗? – blackHawk
然后requestLocationUpdates做什么? – blackHawk