Skip to content

Commit

Permalink
Consider pixel density in coordinate<->point conversion (Android)
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgindi committed Sep 8, 2018
1 parent 496426c commit f662a13
Showing 1 changed file with 10 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -142,12 +142,14 @@ public void onSnapshotReady(@Nullable Bitmap snapshot) {

@ReactMethod
public void pointForCoordinate(final int tag, ReadableMap coordinate, final Promise promise) {
final ReactApplicationContext context = getReactApplicationContext();
final double density = (double) context.getResources().getDisplayMetrics().density;

final LatLng coord = new LatLng(
coordinate.hasKey("latitude") ? coordinate.getDouble("latitude") : 0.0,
coordinate.hasKey("longitude") ? coordinate.getDouble("longitude") : 0.0
);

final ReactApplicationContext context = getReactApplicationContext();
UIManagerModule uiManager = context.getNativeModule(UIManagerModule.class);
uiManager.addUIBlock(new UIBlock()
{
Expand All @@ -167,8 +169,8 @@ public void execute(NativeViewHierarchyManager nvhm)
Point pt = view.map.getProjection().toScreenLocation(coord);

WritableMap ptJson = new WritableNativeMap();
ptJson.putDouble("x", pt.x);
ptJson.putDouble("y", pt.y);
ptJson.putDouble("x", (double)pt.x / density);
ptJson.putDouble("y", (double)pt.y / density);

promise.resolve(ptJson);
}
Expand All @@ -177,12 +179,14 @@ public void execute(NativeViewHierarchyManager nvhm)

@ReactMethod
public void coordinateForPoint(final int tag, ReadableMap point, final Promise promise) {
final ReactApplicationContext context = getReactApplicationContext();
final double density = (double) context.getResources().getDisplayMetrics().density;

final Point pt = new Point(
point.hasKey("x") ? point.getInt("x") : 0,
point.hasKey("y") ? point.getInt("y") : 0
point.hasKey("x") ? (int)(point.getDouble("x") * density) : 0,
point.hasKey("y") ? (int)(point.getDouble("y") * density) : 0
);

final ReactApplicationContext context = getReactApplicationContext();
UIManagerModule uiManager = context.getNativeModule(UIManagerModule.class);
uiManager.addUIBlock(new UIBlock()
{
Expand Down

0 comments on commit f662a13

Please sign in to comment.