CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-tarojs--taro-rn

React Native implementation layer for the Taro cross-platform framework, providing React Native-specific APIs and utilities.

Pending
Overview
Eval results
Files

location-sensors.mddocs/

Location & Sensor APIs

Geolocation services and device sensor access including accelerometer, gyroscope, device motion, and location tracking for Taro React Native applications.

Capabilities

Location Services

Access device location and monitor location changes.

/**
 * Get current device location
 * @param options Location options
 */
function getLocation(options?: {
  type?: 'wgs84' | 'gcj02';
  altitude?: boolean;
  isHighAccuracy?: boolean;
  highAccuracyExpireTime?: number;
  success?: (res: {
    latitude: number;
    longitude: number;
    speed: number;
    accuracy: number;
    altitude?: number;
    verticalAccuracy?: number;
    horizontalAccuracy?: number;
  }) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: any) => void;
}): Promise<{
  latitude: number;
  longitude: number;
  speed: number;
  accuracy: number;
  altitude?: number;
  verticalAccuracy?: number;
  horizontalAccuracy?: number;
}>;

/**
 * Start continuous location updates
 * @param options Location update options
 */
function startLocationUpdate(options?: {
  type?: 'wgs84' | 'gcj02';
  success?: (res: TaroGeneral.CallbackResult) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: TaroGeneral.CallbackResult) => void;
}): Promise<TaroGeneral.CallbackResult>;

/**
 * Stop continuous location updates
 * @param options Stop location options
 */
function stopLocationUpdate(options?: {
  success?: (res: TaroGeneral.CallbackResult) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: TaroGeneral.CallbackResult) => void;
}): Promise<TaroGeneral.CallbackResult>;

/**
 * Listen to location change events
 * @param callback Location change callback
 */
function onLocationChange(callback: (res: {
  latitude: number;
  longitude: number;
  speed: number;
  accuracy: number;
  altitude?: number;
  verticalAccuracy?: number;
  horizontalAccuracy?: number;
}) => void): void;

/**
 * Stop listening to location change events
 * @param callback Optional callback to remove specific listener
 */
function offLocationChange(callback?: (res: {
  latitude: number;
  longitude: number;
  speed: number;
  accuracy: number;
}) => void): void;

Usage Examples:

import { 
  getLocation, 
  startLocationUpdate, 
  onLocationChange, 
  stopLocationUpdate 
} from "@tarojs/taro-rn";

// Get current location
const location = await getLocation({
  type: 'wgs84',
  altitude: true,
  isHighAccuracy: true
});

console.log('Current location:', location.latitude, location.longitude);
console.log('Altitude:', location.altitude);
console.log('Accuracy:', location.accuracy);

// Start continuous location tracking
await startLocationUpdate({ type: 'wgs84' });

const locationChangeHandler = (res) => {
  console.log('Location changed:', res.latitude, res.longitude);
  console.log('Speed:', res.speed, 'm/s');
};

onLocationChange(locationChangeHandler);

// Stop tracking when done
setTimeout(async () => {
  await stopLocationUpdate();
  offLocationChange(locationChangeHandler);
}, 60000); // Stop after 1 minute

Accelerometer

Access device accelerometer for motion detection.

/**
 * Start accelerometer monitoring
 * @param options Accelerometer options
 */
function startAccelerometer(options?: {
  interval?: 'game' | 'ui' | 'normal';
  success?: (res: TaroGeneral.CallbackResult) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: TaroGeneral.CallbackResult) => void;
}): Promise<TaroGeneral.CallbackResult>;

/**
 * Stop accelerometer monitoring
 * @param options Stop accelerometer options
 */
function stopAccelerometer(options?: {
  success?: (res: TaroGeneral.CallbackResult) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: TaroGeneral.CallbackResult) => void;
}): Promise<TaroGeneral.CallbackResult>;

/**
 * Listen to accelerometer data changes
 * @param callback Accelerometer change callback
 */
function onAccelerometerChange(callback: (res: {
  x: number;
  y: number;
  z: number;
}) => void): void;

/**
 * Stop listening to accelerometer changes
 * @param callback Optional callback to remove specific listener
 */
function offAccelerometerChange(callback?: (res: {
  x: number;
  y: number;
  z: number;
}) => void): void;

Usage Examples:

import { 
  startAccelerometer, 
  onAccelerometerChange, 
  stopAccelerometer 
} from "@tarojs/taro-rn";

// Start accelerometer with high frequency
await startAccelerometer({ interval: 'game' });

const accelerometerHandler = (res) => {
  console.log('Acceleration X:', res.x);
  console.log('Acceleration Y:', res.y);
  console.log('Acceleration Z:', res.z);
  
  // Detect shake gesture
  const totalAcceleration = Math.sqrt(res.x * res.x + res.y * res.y + res.z * res.z);
  if (totalAcceleration > 15) {
    console.log('Shake detected!');
  }
};

onAccelerometerChange(accelerometerHandler);

// Stop after 30 seconds
setTimeout(async () => {
  await stopAccelerometer();
  offAccelerometerChange(accelerometerHandler);
}, 30000);

Gyroscope

Access device gyroscope for rotation detection.

/**
 * Start gyroscope monitoring
 * @param options Gyroscope options
 */
function startGyroscope(options?: {
  interval?: 'game' | 'ui' | 'normal';
  success?: (res: TaroGeneral.CallbackResult) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: TaroGeneral.CallbackResult) => void;
}): Promise<TaroGeneral.CallbackResult>;

/**
 * Stop gyroscope monitoring
 * @param options Stop gyroscope options
 */
function stopGyroscope(options?: {
  success?: (res: TaroGeneral.CallbackResult) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: TaroGeneral.CallbackResult) => void;
}): Promise<TaroGeneral.CallbackResult>;

/**
 * Listen to gyroscope data changes
 * @param callback Gyroscope change callback
 */
function onGyroscopeChange(callback: (res: {
  x: number;
  y: number;
  z: number;
}) => void): void;

/**
 * Stop listening to gyroscope changes
 * @param callback Optional callback to remove specific listener
 */
function offGyroscopeChange(callback?: (res: {
  x: number;
  y: number;
  z: number;
}) => void): void;

Device Motion

Access comprehensive device motion data combining accelerometer and gyroscope.

/**
 * Start device motion listening
 * @param options Device motion options
 */
function startDeviceMotionListening(options?: {
  interval?: 'game' | 'ui' | 'normal';
  success?: (res: TaroGeneral.CallbackResult) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: TaroGeneral.CallbackResult) => void;
}): Promise<TaroGeneral.CallbackResult>;

/**
 * Stop device motion listening
 * @param options Stop device motion options
 */
function stopDeviceMotionListening(options?: {
  success?: (res: TaroGeneral.CallbackResult) => void;
  fail?: (res: TaroGeneral.CallbackResult) => void;
  complete?: (res: TaroGeneral.CallbackResult) => void;
}): Promise<TaroGeneral.CallbackResult>;

/**
 * Listen to device motion changes
 * @param callback Device motion change callback
 */
function onDeviceMotionChange(callback: (res: {
  alpha: number;
  beta: number;
  gamma: number;
}) => void): void;

/**
 * Stop listening to device motion changes
 * @param callback Optional callback to remove specific listener
 */
function offDeviceMotionChange(callback?: (res: {
  alpha: number;
  beta: number;
  gamma: number;
}) => void): void;

Usage Examples:

import { 
  startGyroscope, 
  onGyroscopeChange, 
  startDeviceMotionListening,
  onDeviceMotionChange,
  stopGyroscope,
  stopDeviceMotionListening
} from "@tarojs/taro-rn";

// Start gyroscope monitoring
await startGyroscope({ interval: 'ui' });

const gyroscopeHandler = (res) => {
  console.log('Rotation X:', res.x);
  console.log('Rotation Y:', res.y);
  console.log('Rotation Z:', res.z);
};

onGyroscopeChange(gyroscopeHandler);

// Start device motion for orientation
await startDeviceMotionListening({ interval: 'ui' });

const motionHandler = (res) => {
  console.log('Alpha (Z-axis):', res.alpha);
  console.log('Beta (X-axis):', res.beta);
  console.log('Gamma (Y-axis):', res.gamma);
  
  // Detect device orientation
  if (Math.abs(res.gamma) > 45) {
    console.log('Device tilted horizontally');
  }
  if (Math.abs(res.beta) > 45) {
    console.log('Device tilted vertically');
  }
};

onDeviceMotionChange(motionHandler);

// Clean up
setTimeout(async () => {
  await stopGyroscope();
  await stopDeviceMotionListening();
}, 60000);

Window & Screen Events

Monitor window resize and screen capture events.

/**
 * Listen to window resize events
 * @param callback Window resize callback
 */
function onWindowResize(callback: (res: {
  size: {
    windowWidth: number;
    windowHeight: number;
  };
}) => void): void;

/**
 * Stop listening to window resize events
 * @param callback Optional callback to remove specific listener
 */
function offWindowResize(callback?: (res: {
  size: {
    windowWidth: number;
    windowHeight: number;
  };
}) => void): void;

/**
 * Listen to user screen capture events
 * @param callback Screen capture callback
 */
function onUserCaptureScreen(callback: () => void): void;

/**
 * Stop listening to user screen capture events
 * @param callback Optional callback to remove specific listener
 */
function offUserCaptureScreen(callback?: () => void): void;

Usage Examples:

import { 
  onWindowResize, 
  onUserCaptureScreen,
  offWindowResize,
  offUserCaptureScreen
} from "@tarojs/taro-rn";

// Monitor window resize
const resizeHandler = (res) => {
  console.log('Window resized:', res.size.windowWidth, 'x', res.size.windowHeight);
  
  // Adapt layout based on new size
  if (res.size.windowWidth < res.size.windowHeight) {
    console.log('Portrait mode');
  } else {
    console.log('Landscape mode');
  }
};

onWindowResize(resizeHandler);

// Monitor screen capture
const captureHandler = () => {
  console.log('User took a screenshot');
  // Optionally show a message or log the event
  showToast({ title: 'Screenshot taken', icon: 'none' });
};

onUserCaptureScreen(captureHandler);

// Clean up listeners when component unmounts
// offWindowResize(resizeHandler);
// offUserCaptureScreen(captureHandler);

Sensor Data Processing

Motion Detection Patterns

Common patterns for processing sensor data:

import { 
  startAccelerometer, 
  onAccelerometerChange,
  startGyroscope,
  onGyroscopeChange
} from "@tarojs/taro-rn";

// Shake detection
let lastShakeTime = 0;
const shakeThreshold = 15;
const shakeInterval = 1000; // Minimum time between shakes

const shakeDetector = (res) => {
  const acceleration = Math.sqrt(res.x * res.x + res.y * res.y + res.z * res.z);
  const now = Date.now();
  
  if (acceleration > shakeThreshold && (now - lastShakeTime) > shakeInterval) {
    lastShakeTime = now;
    console.log('Shake gesture detected!');
    // Trigger shake action
  }
};

// Tilt detection
const tiltDetector = (res) => {
  const tiltX = Math.atan2(res.y, res.z) * (180 / Math.PI);
  const tiltY = Math.atan2(res.x, res.z) * (180 / Math.PI);
  
  if (Math.abs(tiltX) > 30) {
    console.log('Device tilted left/right:', tiltX);
  }
  if (Math.abs(tiltY) > 30) {
    console.log('Device tilted forward/back:', tiltY);
  }
};

// Rotation tracking
let rotationData = { x: 0, y: 0, z: 0 };

const rotationTracker = (res) => {
  // Integrate gyroscope data for rotation tracking
  const deltaTime = 0.1; // Assume 10Hz update rate
  rotationData.x += res.x * deltaTime;
  rotationData.y += res.y * deltaTime;
  rotationData.z += res.z * deltaTime;
  
  console.log('Cumulative rotation:', rotationData);
};

// Start monitoring
startAccelerometer({ interval: 'game' });
onAccelerometerChange(shakeDetector);
onAccelerometerChange(tiltDetector);

startGyroscope({ interval: 'game' });
onGyroscopeChange(rotationTracker);

Install with Tessl CLI

npx tessl i tessl/npm-tarojs--taro-rn

docs

device-system.md

file-system.md

hooks.md

index.md

location-sensors.md

media.md

navigation.md

network.md

storage.md

ui-apis.md

tile.json