Search code examples
androidcamerazoomingmoveandroid-maps-v2

while moving in camera by dragging it always moves to current location every second


while moving in to map by dragging it always move back to current location every second, i'm unable to see the view in map near my location, how to stop it from moving to current location every second?. i want to move to my location only if my location button is clicked.. so can any one help me? thanks in advance. my code is

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

}

Solution

  • I've implemented a condition to achieve your goal. It will move the camera to the current location when the first time onLocationChanged() is called. After that it will catch current location in each second and store it to mLastLocation = location; but it will not move the camera. When user presses the button, it will fetch LatLong from mLastLocation and move the camera to current location as you desired.

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

    On button Click:

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