其他分享
首页 > 其他分享> > android-通过拖动在相机中移动时,它始终每秒移动到当前位置

android-通过拖动在相机中移动时,它始终每秒移动到当前位置

作者:互联网

在通过拖动始终移动到地图中时,它总是每秒移回当前位置,但是我无法在地图上看到我所在位置附近的视图,如何阻止它每秒移动到当前位置?我只想单击我的位置按钮才能移到我的位置.那么有人可以帮助我吗?提前致谢.我的代码是

@Override
public void onLocationChanged(Location location)
{
mLastLocation = location;
if (mCurrLocationMarker != null) {
mCurrLocationMarker.remove();
}

//Place current location marker
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
/*MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(latLng);
markerOptions.title("Current Position");
markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
*/

CameraUpdate center=
CameraUpdateFactory.newLatLng(latLng);
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15.0f);

mGoogleMap.moveCamera(center);
mGoogleMap.animateCamera(zoom);
//move map camera
// mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,15.0f));

}

解决方法:

我已经实现了达到您目标的条件.首次调用onLocationChanged()时,它将把摄像机移动到当前位置.之后,它将每秒捕获一次当前位置,并将其存储到mLastLocation = location;但不会移动相机.当用户按下按钮时,它将从mLastLocation获取LatLong,然后根据需要将相机移动到当前位置.

boolean isFirstTime = true;   

    @Override
public void onLocationChanged(Location location)
{
   mLastLocation = location;
   if (mCurrLocationMarker != null) {
     mCurrLocationMarker.remove();
   }

  if(isFirstTime){
    //Place current location marker
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
    /*MarkerOptions markerOptions = new MarkerOptions();
    markerOptions.position(latLng);
    markerOptions.title("Current Position");
    markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
    mCurrLocationMarker = mGoogleMap.addMarker(markerOptions);
    */

    CameraUpdate center=
    CameraUpdateFactory.newLatLng(latLng);
    CameraUpdate zoom=CameraUpdateFactory.zoomTo(15.0f);

    mGoogleMap.moveCamera(center);
    mGoogleMap.animateCamera(zoom);
    //move map camera
   // mGoogleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng,15.0f));
   isFirstTime = false;
  }

}

在按钮上单击:

btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                 LatLng latLng = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
                 CameraUpdate center=
                 CameraUpdateFactory.newLatLng(latLng);
                 CameraUpdate zoom=CameraUpdateFactory.zoomTo(15.0f);    
                 mGoogleMap.moveCamera(center);
                 mGoogleMap.animateCamera(zoom);
            }
        });

标签:android-maps-v2,move,zoom,camera,android
来源: https://codeday.me/bug/20191111/2020448.html