Files
iNaturalistReactNative/src/sharedHelpers/createUTFPosition.ts
Johannes Klein c81201cffa 1843 offline taxon search (#1864)
* realm TS

* provider/contexts TS

* todo

* useIconicTaxa TS

* Refactor taxa list into component

* Use TaxaList for taxon search screen

* Refactor render decision

* Use flex 1 container instead of footer

* Show offline notice for query without results

Refetch taxa when going back online

* Update required because of useIsConnected

* Await typing in test

* Mock useIconicTaxa

* Add tests to taxon search

* emitUploadProgress TS

* createUTFPosition TS

* convertScores TS

* Add field type

* Fix imports
2024-08-03 15:13:13 +02:00

47 lines
1.5 KiB
TypeScript

const TILE_SIZE = 256;
let mPixelPositionX;
let mPixelPositionY;
let mTilePositionX;
let mTilePositionY;
/* Based on: https://developers.google.com/maps/documentation/javascript/examples/map-coordinates */
const project = ( latitude: number, longitude: number ) => {
let siny = Math.sin( ( latitude * Math.PI ) / 180 );
// Truncating to 0.9999 effectively limits latitude to 89.189. This is
// about a third of a tile past the edge of the world tile.
siny = Math.min( Math.max( siny, -0.9999 ), 0.9999 );
return [
( 256 * ( 0.5 + longitude / 360 ) ),
( 256 * ( 0.5 - Math.log( ( 1 + siny ) / ( 1 - siny ) ) / ( 4 * Math.PI ) ) )
];
};
/* Based on: https://developers.google.com/maps/documentation/javascript/examples/map-coordinates */
const createUTFPosition = ( zoomLevel: number, latitude: number, longitude: number ) => {
const scale = 2 ** Math.floor( zoomLevel );
const worldCoordinate = project( latitude, longitude );
mTilePositionX = Math.floor( ( worldCoordinate[0] * scale ) / TILE_SIZE );
mTilePositionY = Math.floor( ( worldCoordinate[1] * scale ) / TILE_SIZE );
const pixelCoordinate = [
worldCoordinate[0] * scale,
worldCoordinate[1] * scale
];
mPixelPositionX = ( pixelCoordinate[0] - mTilePositionX * TILE_SIZE );
mPixelPositionY = ( pixelCoordinate[1] - mTilePositionY * TILE_SIZE );
return {
mPixelPositionX,
mPixelPositionY,
mTilePositionX,
mTilePositionY
};
};
export default createUTFPosition;