I decided to implement the "Location" button with all the states, like on Google maps.

More details:

  • Inactive - When the user scamps on the map.

enter image description here

  • Active - When the user has determined his location on the map, and the camera follows its movement.

enter image description here

Now to the code:

<android.support.design.widget.FloatingActionButton android:layout_margin="16dp" android:layout_width="wrap_content" android:layout_height="wrap_content" app:elevation="1dp" android:src="@drawable/ic_my_location_black_24dp" android:tint="@android:color/background_dark" //серый цвет т.е. неактивная изначально app:backgroundTint="@android:color/white" android:layout_gravity="bottom|end"/> 

Location Button:

 FAB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { cameraFromPosition(); followUser = true; FAB.setColorFilter(ContextCompat.getColor(getApplicationContext(),R.color.primary)); // синий цвет т.е. активная } }); 

Location tracking:

 @Override public void onLocationChanged(Location location) { if (followUser) { // начинаем слежку т.к. нажата кнопка "определения местоположения" и цвет ей синий cameraFromPosition(); } } 

What is the best way to implement the " Catch " method , when the user again began to run hither and so on the map, and thereby make the button inactive, ie gray?

  • one
    If I am not mistaken, then you can catch a scrolling card event, in the callback of which a boolean value comes in, indicating whether the user initiated the movement. I think this should solve your problem - YuriySPb
  • one
    @ YuriySPb Thank you for the "kolbek"! This word served as a starting point for the open spaces of Google :) - iFr0z
  • Please) ... - YuriySPb

1 answer 1

I still dug up the official series of new methods from Google [ Doc ], which catch the scroll on the map and not only.

PS all inclusive.

To solve my problem, only the following was enough:

  1. implements GoogleMap.OnCameraMoveStartedListener .
  2. In the method public void onMapReady(GoogleMap googleMap) add map.setOnCameraMoveStartedListener(this); .

And the last:

 @Override public void onCameraMoveStarted(int reason) { if (reason == GoogleMap.OnCameraMoveStartedListener.REASON_GESTURE) { followUser = false; FAB.setColorFilter(ContextCompat.getColor(getApplicationContext(),R.color.grey)); } } 

PS Ready Callback from Google as a gift.

  • I am trying to implement something like this, but in your example it is a bit unsuccessful due to my inexperience, and I don’t want to duplicate the question, I hope you will. I added all your changes in general, but the cameraFromPosition, followUser is highlighted in red, could you complement your code + gray / primary conversion is not very clear, you just wrote a color value there, did I understand that correctly? and perhaps the last one, we add the location button ourselves, or you can use map.getUiSettings (). setMyLocationButtonEnabled (true); ? - Morozov
  • @Morozov cameraFromPosition is my method, add boolean followUser before onCreate (). Gray and primary are the colors that are in color.xml. The button itself, map, getUI ... (false), and not (true). - iFr0z