Environment details
Library version: 6.10.0
Description
The ScaleBar composable displays a significantly incorrect scale on devices with screen densities greater than 1.0x. This is caused by two separate bugs in the logic that calculates the geographical distance represented by the scale bar.
Bug 1: Incorrect Unit Conversion (Dp vs. Px)
The logic incorrectly uses a density-independent pixel (Dp) value where a raw screen pixel (px) value is required.
- API Requirement: The official Google Maps SDK documentation for
projection.fromScreenLocation() explicitly states that the Point object must be specified in screen pixels.
- Impact: On a device with a 3.0x screen density, a
100.dp wide ScaleBar is 300 physical pixels wide. The current implementation passes 100 to the function instead of 300, causing the calculated geographical distance to be underestimated by a factor of 3.
Bug 2: Swapped Coordinates in Point Constructor
The logic incorrectly swaps the x and y coordinates when determining the top-right point of the scale bar area.
- Incorrect Implementation: The code uses a pattern similar to
Point(0, width).
- Impact: This defines a vertical line from
(0, 0) to (0, width) instead of the intended horizontal line. This leads to an incorrect distance calculation based on the composable's height instead of its width.
The combination of these two bugs renders the ScaleBar component functionally unusable and misleading on most devices.
Proposed Solution
The fix requires both converting Dp to pixels and using the correct coordinate order.
val widthInPixels = with(LocalDensity.current) { width.toPx().toInt() }
// Correct: Use widthInPixels for the X coordinate
val upperRightLatLng = projection.fromScreenLocation(Point(widthInPixels, 0))
Environment details
Library version: 6.10.0
Description
The
ScaleBarcomposable displays a significantly incorrect scale on devices with screen densities greater than 1.0x. This is caused by two separate bugs in the logic that calculates the geographical distance represented by the scale bar.Bug 1: Incorrect Unit Conversion (Dp vs. Px)
The logic incorrectly uses a density-independent pixel (
Dp) value where a raw screen pixel (px) value is required.projection.fromScreenLocation()explicitly states that thePointobject must be specified in screen pixels.100.dpwideScaleBaris300physical pixels wide. The current implementation passes100to the function instead of300, causing the calculated geographical distance to be underestimated by a factor of 3.Bug 2: Swapped Coordinates in
PointConstructorThe logic incorrectly swaps the
xandycoordinates when determining the top-right point of the scale bar area.Point(0, width).(0, 0)to(0, width)instead of the intended horizontal line. This leads to an incorrect distance calculation based on the composable's height instead of its width.The combination of these two bugs renders the
ScaleBarcomponent functionally unusable and misleading on most devices.Proposed Solution
The fix requires both converting Dp to pixels and using the correct coordinate order.