Fix getPointInView function return wrong point position on Android#3997
Merged
mfazekas merged 1 commit intornmapbox:mainfrom Sep 26, 2025
Merged
Conversation
Contributor
Author
|
Patch: diff --git a/node_modules/@rnmapbox/maps/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt b/node_modules/@rnmapbox/maps/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt
index c340bd8..308b27e 100644
--- a/node_modules/@rnmapbox/maps/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt
+++ b/node_modules/@rnmapbox/maps/android/src/main/java/com/rnmapbox/rnmbx/components/mapview/RNMBXMapView.kt
@@ -901,11 +901,13 @@ open class RNMBXMapView(private val mContext: Context, var mManager: RNMBXMapVie
fun getPointInView(coordinate: Point, response: CommandResponse) {
val point = mMap!!.pixelForCoordinate(coordinate)
+ val density = getDisplayDensity()
+ val pointInView = PointF((point.x / density).toFloat(), (point.y / density).toFloat())
response.success {
val array: WritableArray = WritableNativeArray()
- array.pushDouble(point.x)
- array.pushDouble(point.y)
+ array.pushDouble(pointInView.x.toDouble())
+ array.pushDouble(pointInView.y.toDouble())
it.putArray("pointInView", array)
}
}
|
Contributor
|
@cuongncwork thanks much for the PR, looks great! Can you please add a minimal component to reproduce the issue you're fixing? |
Contributor
Author
|
I’ve added a minimal repro component. It shows that on Android the getPointInView function was returning the wrong position. After applying this PR, the point now matches the correct screen position of the marker. import React, { useRef, useEffect, useState } from "react"
import { View, Text, StyleSheet } from "react-native"
import MapboxGL from "@rnmapbox/maps"
import Config from "react-native-config"
MapboxGL.setAccessToken(Config.MAP_BOX_TOKEN)
export const DrivingTargetScreen = () => {
const mapRef = useRef(null)
const [point, setPoint] = useState(null)
useEffect(() => {
async function test() {
if (mapRef.current) {
const result = await mapRef.current.getPointInView([105.85, 21.025])
setPoint(result)
}
}
setTimeout(test, 2000)
}, [])
return (
<View style={styles.flex}>
<MapboxGL.MapView ref={mapRef} style={styles.flex} styleURL={MapboxGL.StyleURL.Street}>
<MapboxGL.Camera
bounds={{
ne: [105.8, 21.0],
sw: [105.9, 21.05],
}}
heading={45}
animationMode="flyTo"
animationDuration={2000}
/>
<MapboxGL.PointAnnotation id="center" coordinate={[105.85, 21.025]} />
</MapboxGL.MapView>
{point && (
<>
<View
style={{
backgroundColor: "red",
position: "absolute",
borderRadius: 15,
borderWidth: 2,
borderColor: "#FFFFFF",
borderStyle: "solid",
width: 30,
height: 30,
top: point[1] - 15,
left: point[0] - 15,
}}
/>
<Text
style={{
backgroundColor: "white",
position: "absolute",
padding: 20,
bottom: 20,
left: 20,
right: 20,
}}
>
PointInView: {JSON.stringify(point)}
</Text>
</>
)}
</View>
)
}
const styles = StyleSheet.create({
flex: {
flex: 1,
},
}) |
Contributor
|
@cuongncwork thanks much, great work! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Description
Fixes: getPointInView function return wrong point position on Android.
Checklist
CONTRIBUTING.mdyarn generatein the root folder/exampleapp./example)