메인 콘텐츠로 건너뛰기
Back to plugins
@capgo/capacitor-background-geolocation
튜토리얼
@capgo/capacitor-background-geolocation

배경 위치 추적

iOS와 Android의 네이티브 지오펜싱 및 웹훅 전환과 함께 정확한 배경 위치 추적

Guide

배경 위치 추적에 대한 튜토리얼

장치에서 테스트

다운로드한 Capgo 앱을 열고 QR코드 code을 스캔하세요.

code QR 코드 미리보기

@capgo/background-geolocation 사용

Capacitor 앱에 대한 정확한 배경 지오로케이션 및 네이티브 지오펜싱을 iOS 및 Android에서 제공합니다. 정확한 위치 업데이트 스트리밍, circular 지역 모니터링 및 JavaScript 또는 백엔드에 지오펜스 입/출 transition을 전달하세요.

설치

bun add @capgo/background-geolocation
bunx cap sync

이 플러그인은 노출합니다.

  • start - 전면 또는 배경 위치 업데이트 스트리밍.
  • stop - 활성 위치 추적 중지.
  • openSettings - 사용자가 위치 권한을 수정해야 할 때 native 설정 열기.
  • setPlannedRoute - 사용자가 계획된 경로를 떠날 때 native 사운드 재생.
  • setupGeofencing - 네이티브 지오펜스 기본값 구성 및 옵션 transition webhook 전달.
  • addGeofence - iOS 또는 Android circular 지오펜스 지역 모니터링.
  • removeGeofence / removeAllGeofences - 등록된 지역 중 하나 또는 모두 중지.
  • getMonitoredGeofences - 지역 식별자 목록을 표시합니다.
  • geofenceTransition listener - 앱이 활성화된 동안 입/퇴장 이벤트를 수신합니다.
  • geofenceError listener - 전환 이벤트와 native monitoring 오류를 별도로 처리합니다.

예시 사용

start

장치의 위치를 변경하는 변경 사항을 듣기 시작하려면 이 메서드를 호출하세요. Promise는 호출이 완료되었음을 나타내며, callback은 새로운 위치가 사용 가능한 경우 또는 이 메서드를 호출할 때 오류가 발생한 경우 호출됩니다. 이 Promise의 거부를 의존하지 마세요.

import { BackgroundGeolocation } from '@capgo/background-geolocation';

await BackgroundGeolocation.start(
  {
    backgroundMessage: "App is using your location in the background",
    backgroundTitle: "Location Service",
    requestPermissions: true,
    stale: false,
    distanceFilter: 10
  },
  (location, error) => {
    if (error) {
      console.error('Location error:', error);
      return;
    }
    if (location) {
      console.log('New location:', location.latitude, location.longitude);
    }
  }
);

stop

위치 업데이트를 중지합니다.

import { BackgroundGeolocation } from '@capgo/background-geolocation';

await BackgroundGeolocation.stop();

openSettings

장치의 위치 설정 페이지를 열어줍니다. 사용자에게 위치 서비스를 활성화하거나 권한을 조정하도록 안내할 때 유용합니다.

import { BackgroundGeolocation } from '@capgo/background-geolocation';

// Direct user to location settings
await BackgroundGeolocation.openSettings();

setPlannedRoute

계획된 경로에서 사용자가 벗어났을 때 사운드 파일을 재생합니다. 이 기능은 native에서만 사용할 수 있으며, 백그라운드에서도 재생할 수 있습니다.

import { BackgroundGeolocation } from '@capgo/background-geolocation';

await BackgroundGeolocation.setPlannedRoute({
  soundFile: "notification.mp3",
  route: [[-74.0060, 40.7128], [-118.2437, 34.0522]]
});

native geofencing

iOS 및 Android native geofences를 사용하여 매장, 작업장, 배송 구역, 캠퍼스, 또는 체크인 지역을 모니터링합니다. HTTP 또는 HTTPS를 추가하여 native __CAPGO_KEEP_0__ POST 전환 패킷을 WebView가 중단된 상태에서 전송할 수 있습니다. url to let native code POST transition payloads while the WebView is suspended.

import { BackgroundGeolocation } from '@capgo/background-geolocation';

await BackgroundGeolocation.setupGeofencing({
  url: 'https://api.example.com/geofences',
  notifyOnEntry: true,
  notifyOnExit: true,
  payload: { userId: '123' },
});

await BackgroundGeolocation.addGeofence({
  identifier: 'warehouse',
  latitude: 40.7128,
  longitude: -74.006,
  radius: 200,
});

const listener = await BackgroundGeolocation.addListener(
  'geofenceTransition',
  (event) => console.log(event.identifier, event.transition),
);

const errorListener = await BackgroundGeolocation.addListener(
  'geofenceError',
  (event) => console.error(event.identifier, event.message),
);

await BackgroundGeolocation.removeGeofence({ identifier: 'warehouse' });
await listener.remove();
await errorListener.remove();

Android에서 추가 ACCESS_BACKGROUND_LOCATION __CAPGO_KEEP_0__:

<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

Full Reference

capgo에서 계속하기: Using @capgo/background-geolocation

__CAPGO_KEEP_0__을 사용하고 있다면 @capgo/background-geolocation을 사용하여 __CAPGO_KEEP_0__을 사용하여 native 플러그인 작업을 계획할 때, __CAPGO_KEEP_0__을 연결하세요. @capgo/background-geolocation에서 capgo 구현 세부 정보를 참조하세요. Getting Started with capgo __CAPGO_KEEP_0__: for the implementation detail in Getting Started, Capgo 플러그인 디렉토리 for the product workflow in Capgo 플러그인 디렉토리, Capacitor 플러그인들에 의해 Capgo for the implementation detail in Capacitor 플러그인들에 의해 Capgo, 그리고 플러그인 추가 또는 업데이트 for the implementation detail in 플러그인 추가 또는 업데이트.