Include device location and multiple map markers in camera bounds

Stack Overflow Asked by Orbit on December 4, 2020

We’re displaying a number of map markers on the map representing various objects which are retrieved either from a cache or from an asynchronous network call. When these objects are retrieved, we update the camera to ensure all markers are within view:

 LatLngBounds bounds =;
 mMapboxMap.animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 50, 100, 50, 30));

We’re interested in adding the user’s location to the map, and have done so using the LocationComponent provided by MapBox as shown in this example.

The problem is that when the LocationComponent has locationComponent .setCameraMode(CameraMode.TRACKING) set as shown, the camera will focus on the user’s location, without accommodating the markers previously set. Setting the camera mode to CameraMode.NONE allows the markers to be shown using the code snippet above, however in this case the user’s location will not be accommodated in the mBoundsBuilder.

The obvious solution seemed to be to listen for location updates and manually add the coordinates to the mBoundsBuilder so that the user’s location is taken into account. This, however, has a number of problems, such as:

  1. Callbacks for location changes can be invoked many times, while the mBoundsBuilder should only contain a single set of coordinates for the user’s location, the rest of the objects being the previously added markers.
  2. Location updates are asynchronous, and getLastKnownLocation will often return null.


  1. Is there a default, built in way to show a user’s location while making sure the camera accommodates map markers?
  2. If no default option exists, how can this be properly implemented?


One Answer

Take a look at this -> it helps in drawing polygons on a map.

Answered by Darsh Reddy on December 4, 2020

Add your own answers!

Related Questions

Call parent class method (with this context) in child class

2  Asked on December 27, 2021 by rjoxford


Replace .(dot) inside number Java between two elements

2  Asked on December 27, 2021 by alex-danilov


Filtering an array of objects Vue

2  Asked on December 27, 2021 by graham-morby


Is there any good idea to ceil or floor datetime in python3?

2  Asked on December 27, 2021 by asknature


How do I use django’s Q with django taggit?

4  Asked on December 27, 2021 by shino


When to use UserPassesTestMixin vs PermissionRequiredMixin

2  Asked on December 27, 2021 by david-jay-brady


nuxt: pass data from server plugin to client

1  Asked on December 27, 2021


Am I Using JDBC Connection Pooling?

4  Asked on December 27, 2021 by ryandlf


Ask a Question

Get help from others!

© 2023 All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP