mapbox-maps-android
mapbox-maps-android copied to clipboard
Zoom in to encompass list of points
New Feature
Following up on this issue: https://github.com/mapbox/mapbox-maps-android/issues/1562
I can center the camera to encompase two points but it does not zoom in or out as needed. Here is what I'm doing:
val origin = Point.fromLngLat(lonOrigin, latOrigin)
val destination = Point.fromLngLat(lonDestination, latDestination)
val cameraOptions = map.getMapboxMap().cameraForCoordinates(coordinates = listOf<Point>(origin, destination))
map.getMapboxMap().easeTo(cameraOptions)
Why
This is a feature needed by so many people, yet I can't seem to find it in the tutorials or examples for Android SDK v10. If there is a way to do it then I would appreciate that you point me in the right direction for centering and zooming to show points
@manuelsilverio Can you try passing a EdgeInsets
for your map?
@dudeuter Do you have an example or link to Docs on how to pass an EdgeInsets? I've never done that before.
https://docs.mapbox.com/android/maps/api/10.11.2/mapbox-maps-android/com.mapbox.maps/-mapbox-map/camera-for-coordinates.html
@manuelsilverio Works for me after adding this code.
CameraOptions.Builder builder = new CameraOptions.Builder();
builder.zoom(10.0);
binding.mapView.getMapboxMap()
.setCamera(builder.build());
As a temporary workaround I added:
EdgeInsets(
-150.0,
-150.0,
-150.0,
-150.0,
)
But this issue really need to be fixed
Hi everyone, sorry for my late response. It's been a busy month. I tried the edgeInsets but it did not work.
- I found a solution for this (I'm working on Android, using Kotlin). Here it goes:
`private fun zoomFit(origin: Point, destination: Point){
//1. Create a line with point 1 and 2
val lineGeo = LineString.fromLngLats( listOf<Point>(origin, destination))
//2. Use line geometry to create camera options
val cameraOptions = binding.map.getMapboxMap().cameraForGeometry(lineGeo)
//3. Use camera options and point 1 and 2 to calculate zoom level and add this to new camera options
val newCamOptions = CameraOptions.Builder()
.zoom(calculateZoomLevel(cameraOptions.center!!, origin, destination))
.bearing(cameraOptions.bearing)
.padding(cameraOptions.padding)
.center(cameraOptions.center)
.build()
//4. Use "easeTo" to zoom int to camera options
binding.map.getMapboxMap().easeTo(newCamOptions)
}`
-
The complicated bit is calculating the zoom level. For this I built a function based on this post from stackoverflow: https://stackoverflow.com/questions/71882172/zoom-level-by-distance-in-mapbox-map-for-xamarin-forms
-
Here is the function for calculating zoom level:
`private fun calculateZoomLevel(centerPoint: Point, origin: Point, destination: Point): Double{
//1. Calculate distance between two points
val distance = calculateDistance(origin, destination)
val earthCircumpherence = 40075016.686
val latInRadians = convertDegreesToRadian(centerPoint.latitude())
//2. Use log function, earth circumpherence and distance to calculate the zoom level
val logNum = earthCircumpherence * Math.cos(latInRadians) / distance
val zoomLevel = log(logNum, 2.1) //the 2.1 can be changed/tweeked to adjust the zoom level
return zoomLevel
}`
The only limitations here is that this only works for 2 points, if we have a list of points we need to do a different workaround.
- I did not add the functions for calculating distance and convertingDegreesToRadian. I'll leave those for "homework" ;)
is there any update on this and not a workaround please ?
@manuelsilverio @ArtemHryhorovGeniusee Sorry for the delayed response, I've been a little busy with other actions on my end.
As of yet, I haven't been able to reproduce such behavior. I created a sample application to attempt to reproduce based off 10.14.x branch of the Maps SDK.
Could you provide the preconditions, and points you're trying to frame that are causing this unexpected behavior?