this is my first question here:
I'm developing an Android app using Google Maps API and in my main activity I have a fragment with the map. I center the map with the moveCamera method within the event onMapLoaded but the problem is that while the map is loading, it displays the Ecuador and then it switchs to the area I have chosen.
How can I avoid this? I can't invoke moveCamera() outside the event onMapLoaded
Thanks in advance
If you want to keep track of the camera position, you can use an OnCameraChangeListener
which is set on the map by calling GoogleMap.setOnCameraChangeListener(OnCameraChangeListener).
The listener will be notified when the camera changes with an onCameraChange(CameraPosition) callback. You can then obtain the target (latitude/longitude), zoom, bearing and tilt of the camera. This callback is guaranteed to be called at the end of every animation but may not be called for intermediate frames.
Related
I want to update the value of a marker when the maps stop moving, but I canĀ“t detect it. Does someone know how?. I already tried with the PanGestureRecognizer
Implement the GoogleMap.IOnCameraIdleListener and assign it to your map instance (assumably in the MapReady callback):
Implement GoogleMap.IOnCameraIdleListener
public void OnCameraIdle()
{
// a callback that is invoked when camera movement has ended.
}
Assign the Idle listener to the GoogleMap instance:
googleMap.SetOnCameraIdleListener(this);
Re: setOnCameraIdleListener
Can anyone explain the difference between
OnMapReadyCallback.OnMapReady(GoogleMap googleMap)
and
GoogleMap.OnMapLoadedCallback.OnMapLoaded()
It is not very clear to me.
It basically depends upon what you want to do with the map.
You can safely use OnMapReadyCallback to set your pins, etc. It is called as soon as the map is ready for you to use it.
OnMapLoadedCallback, as the docs state, is called
When the map has finished rendering. This occurs after all tiles
required to render the map have been fetched, and all labeling is
complete. eg. the map's content is fully loaded and visible.
This happens later than OnMapReady. The call googleMap.setOnMapLoadedCallback even implies that OnMapReady already happened to be able to be called safely (googleMap != null).
I realise I can use a GoogleMap.OnMyLocationButtonClickListener to determine when the user has pressed the button to make the GoogleMap follow their location on screen, but is there a way I can receive an event for when the user is no longer following the device? For example, if the user has moved the map, which stops the map from locking to the user's location.
You could add a OnCameraChangeListener, it will get called if the map is dragged. In its onCameraChange compare the CameraPosition#target LatLng with MyLocation.
Here are the camera change and my location listeners in the same API:
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnCameraChangeListener
https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap.OnMyLocationChangeListener
I suggest using a View overlapping MapFragment and overriding its onTouchEvent, checking DOWN event to stop following position. This is when user starts any interaction with map.
I am doing a project related to android Google Map Api V2. If I set OnCameraChangeListener on GoogleMap. whenever GoogleMap calls the moveCamera(), the logic in the CameraChangeListener() should be executed?
Yes, your OnCameraChangeListener callback is triggered after you call moveCamera. It may be not called if the call would result in no change, e.g. moving to the same LatLng twice.
As the OnCameraChangeListener Documentation states, the onCameraChange method:
Called after the camera position has changed. During an animation, this listener may not be notified of intermediate camera positions. It is always called for the final position in the animation.
So yest this listener will be invoked on camera position change.
I need to get the map bound when my app starts, but
GoogleMap.getProjection().getVisibleRegion().latLngBounds;
returns the bounds 0,0,0,0. Is there some kind of listener I could use to see when the
Projection
is loaded?
Thank you in advance.
Register (temporarily, if needed) an OnCameraChangeListener. It will get called whenever the camera changes, which will include the first change when the default map is displayed.