From 7bbb6e0a1c5895f4825ed60ac55aca41e0a35835 Mon Sep 17 00:00:00 2001 From: Benedict Strube Date: Sat, 29 Jan 2022 21:57:45 +0100 Subject: [PATCH 1/2] feat(projectBack): added projectBack method as inverse method to project projectBack maps an on screen coordinate to a map coordinate. This is the inverse method to project which maps a map coordinate to an on screen coordinate. --- src/ui-mapbox/common.ts | 5 +++++ src/ui-mapbox/index.android.ts | 8 ++++++++ src/ui-mapbox/index.d.ts | 3 ++- src/ui-mapbox/index.ios.ts | 13 +++++++++++++ 4 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/ui-mapbox/common.ts b/src/ui-mapbox/common.ts index e64fba1..523a1c0 100755 --- a/src/ui-mapbox/common.ts +++ b/src/ui-mapbox/common.ts @@ -703,6 +703,8 @@ export interface MapboxApi { removeImage(imageId: string, nativeMap?: any): Promise; project(data: LatLng): { x: number; y: number }; + + projectBack(point: { x: number, y: number }): LatLng; } // ------------------------------------------------------------ @@ -1061,6 +1063,9 @@ export abstract class MapboxViewCommonBase extends ContentView implements Mapbox project(data: LatLng) { return this.mapbox && this.mapbox.project(data); } + projectBack(screenCoordinate: { x: number, y: number }): LatLng { + return this.mapbox && this.mapbox.projectBack(screenCoordinate); + } } // ----------------------------------------------------------------- diff --git a/src/ui-mapbox/index.android.ts b/src/ui-mapbox/index.android.ts index 8e6d60e..406e902 100755 --- a/src/ui-mapbox/index.android.ts +++ b/src/ui-mapbox/index.android.ts @@ -3391,4 +3391,12 @@ export class Mapbox extends MapboxCommon implements MapboxApi { const screenLocation = this._mapboxMapInstance.getProjection().toScreenLocation(mapboxPoint); return { x: Utils.layout.toDeviceIndependentPixels(screenLocation.x), y: Utils.layout.toDeviceIndependentPixels(screenLocation.y) }; } + projectBack(screenCoordinate: { x: number, y: number }): LatLng { + const pointf = new android.graphics.PointF(screenCoordinate.x, screenCoordinate.y); + const coordinate = this._mapboxMapInstance.getProjection().fromScreenLocation(pointf); + return { + lat: coordinate.getLatitude(), + lng: coordinate.getLongitude() + } + } } diff --git a/src/ui-mapbox/index.d.ts b/src/ui-mapbox/index.d.ts index dce8922..ac35fca 100644 --- a/src/ui-mapbox/index.d.ts +++ b/src/ui-mapbox/index.d.ts @@ -97,4 +97,5 @@ export declare class Mapbox extends MapboxCommon implements MapboxApi { x: number; y: number; }; -} \ No newline at end of file + projectBack(screenCoordinate: { x: number, y: number }): LatLng; +} diff --git a/src/ui-mapbox/index.ios.ts b/src/ui-mapbox/index.ios.ts index e7769b4..de82922 100755 --- a/src/ui-mapbox/index.ios.ts +++ b/src/ui-mapbox/index.ios.ts @@ -3159,6 +3159,19 @@ export class Mapbox extends MapboxCommon implements MapboxApi { const { x, y } = theMap.convertCoordinateToPointToView({ latitude: data.lat, longitude: data.lng }, theMap); return { x, y }; } + + projectBack(screenCoordinate: { x: number, y: number }): LatLng { + const theMap: MGLMapView = this._mapboxViewInstance; + const cgPoint = { + x: screenCoordinate.x, + y: screenCoordinate.y + } + const coordinate = theMap.convertPointToCoordinateFromView(cgPoint, theMap); + return { + lat: coordinate.latitude, + lng: coordinate.longitude + } + } } const _addObserver = (eventName, callback) => NSNotificationCenter.defaultCenter.addObserverForNameObjectQueueUsingBlock(eventName, null, NSOperationQueue.mainQueue, callback); From 99d06422767834046018ba2aace6257a413b720e Mon Sep 17 00:00:00 2001 From: Benedict Strube Date: Sun, 6 Feb 2022 22:16:07 +0100 Subject: [PATCH 2/2] fix: added project methods to API --- src/ui-mapbox/common.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/ui-mapbox/common.ts b/src/ui-mapbox/common.ts index 523a1c0..b64260a 100755 --- a/src/ui-mapbox/common.ts +++ b/src/ui-mapbox/common.ts @@ -872,6 +872,13 @@ export interface MapboxViewApi { onLowMemory(): Promise; onDestroy(): Promise; + + project(data: LatLng): { + x: number; + y: number; + }; + + projectBack(screenCoordinate: { x: number, y: number }): LatLng; } // ----------------------------------------------------------------------------------------