Skip to content

Bug in ScaleBar calculation logic #774

@iiiburnyiii

Description

@iiiburnyiii

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)) 

Metadata

Metadata

Assignees

No one assigned

    Labels

    releasedtriage meI really want to be triaged.type: bugError or flaw in code with unintended results or allowing sub-optimal usage patterns.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions