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).
Related
I'm creating a navigation app which uses Google Maps Android API v2. When the Activity with the map (MapFragment) is being started, a PolylineOptions (a line) and an array of MarkerOptions' is being added to the map.
This may take long time in some cases, so I wonder if it is possible to keep the map loaded when the Activity is being restarted, for example due to screen orientation change, so that I don't have to "re-add" all the markers and the line again, which may sometimes take quite some time.
Right now, I get the MapFragment and set its OnMapReadyCallback every time the activity is started, then, in the callback method, the line and markers are being added (after every restart).
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.
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.