如何移动每10个步骤更新当前位置

时间:2017-07-17 06:33:50

标签: java android google-play-services google-maps-android-api-2 fusedlocationproviderapi

我已经实现了这个代码,每移动10步就可以获得当前的位置更新,但是当我移动10步或更多步时,我没有获得当前的lat lng,我有疑问是设置最小距离10步是否现实?

每当我的代码运行时,我会在十进制后的数字方面得到不同的lat lng,如果我比较每次我的应用程序与google maps网站上的当前位置运行时得到的这些lat lng,其大约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
    }

如果我在连接回调中连接播放服务后移动10步,则获取当前位置更新的代码

 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());

1 个答案:

答案 0 :(得分:0)

with