Android – when you move through the camera by dragging, it always moves to the current position per second
When I always move to the map by dragging, it always moves back to the current position every second, but I can't see the view near my position on the map. How can I prevent it from moving to the current position every second? I just want to click my location button to move to my location. Can someone help me? Thank you 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));
}
resolvent:
I have achieved the condition to reach your goal. When onlocationchanged() is called for the first time, it will move the camera to the current position. After that, it will capture the current position once per second and store it in mlastlocation = location; But it does not move the camera. When the user presses the button, it will get latlong from mlastlocation and move the camera to the current position as needed
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;
}
}
Click on the button:
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);
}
});