3

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

}
Tushar Kotecha
  • 764
  • 2
  • 7
  • 27

2 Answers2

4

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);
            }
        });
tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
  • so where can i put that code to show the current location once? – Tushar Kotecha Apr 05 '17 at 11:43
  • but i want to show current location once while loading first time – Tushar Kotecha Apr 05 '17 at 11:47
  • look at here http://stackoverflow.com/a/13905821/7784663 in onLocationChanged listener – Tushar Kotecha Apr 05 '17 at 11:51
  • but this will not update camera and move camera while we are moving on map or to any other location – Tushar Kotecha Apr 05 '17 at 12:05
  • Check my updated answer. It will check *If it first time the activity is loading* . If yes - Then *Move the camera* . `onLocationChanged()` is continuously getting current location update and storing it to `mLastLocation. After first loading, The camera will move only if user clicks the button to the current location. Hope this will suffice. – tahsinRupam Apr 05 '17 at 12:21
0

its very easy. i always put a marker on the map on new location.

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

            }
        }

I move the camera to the marker postion. let me know if its work or not i will help you out :)