通过拖动进入相机时,它总是每秒移动到当前位置

时间:2017-04-05 11:35:09

标签: android camera zoom move android-maps-v2

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

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

}

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

非常容易。我总是在新地点的地图上放置一个标记。

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == PLACE_PICKER_REQUEST) {
            if (resultCode == RESULT_OK) {

                Place place = PlacePicker.getPlace(this,data);

                CameraPosition cameraPosition = new CameraPosition.Builder()
                        .target(place.getLatLng())
                        .zoom(17).build();

                Marker marker = mMap.addMarker(new MarkerOptions().position(place.getLatLng()).title(place.getName().toString()));
                markers.add(marker);

                mMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));

            }
        }

我将相机移动到标记位置。让我知道它的工作与否我会帮助你:)