0.1.0 • Published 2 years ago

@aldegad/capacitor-geolocation v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

@aldegad/capacitor-geolocation

This is a capacitor plugin, let you receive geolocation updates either forground and background. Android and iOS platforms are suppoerted.

Install

npm install @aldegad/capacitor-geolocation
npx cap sync

Quick Example

typescript

import { Geolocation, GeolocationAlert, GeolocationConnect } from '@aldegad/capacitor-geolocation';

async startLocationUpdates() {
    const { state } = await Geolocation.requestPermission();

    if(state !== 'granted') return;

    Geolocation.startLocationUpdates(null, ({latitude, longitude}) => {
        console.log("location updates", `${latitude}/${longitude}`);
    });
}
stopLocationUpdates() {
    Geolocation.startLocationUpdates();
}

API

requestPermission(...)

requestPermission(options?: GeolocationPermissionOptions | undefined) => Promise<{ state: GeolocationPermissionState; }>

Request and check geolocation permissions. You can define alert cotext.

ParamType
optionsGeolocationPermissionOptions

Returns: Promise<{ state: GeolocationPermissionState; }>

Since: 0.0.1


startLocationUpdates(...)

startLocationUpdates(options?: GeololocationUpdatesOptions | undefined, callback?: GeolocationUpdatesCallback | undefined) => Promise<void>

Start location updates.

ParamType
optionsGeololocationUpdatesOptions
callbackGeolocationUpdatesCallback

Since: 0.0.1


stopLocationUpdates()

stopLocationUpdates() => Promise<void>

Stop location updates.

Since: 0.0.1


Interfaces

GeolocationPermissionOptions

Geolocation permission options.

PropTypeDescriptionSince
promptAlertGeolocationAlertOptionsAndroid only. If user ignore geolocation permission, notice why this application needs geolocation permissions.0.0.1
deniedAlertGeolocationAlertOptionsIf user denied geolocation permission, notice why this application needs geolocation permissions and tells how to reset permissions.0.0.1

GeolocationAlertOptions

Geolocation alert options.

PropTypeDescriptionSince
headerstringAlert header0.0.1
messagestringAlert message0.0.1
okTextstringAlert ok text0.0.1
cancelTextstringAlert cancel text0.0.1

GeololocationUpdatesOptions

Geolocation updates options.

PropTypeDescriptionDefaultSince
backgroundbooleanToggle background geolocation enable or not.true0.0.1
notificationGeolocationNotificationOptionsThis is Android forground notification option. If you need to run background geolocation on Android, you must define notification.0.0.1
connectGeolocationConnectOptionsAfter location updates, upload data to server. It uses multipart-formdata.Nothing to do is default.0.0.1

GeolocationNotificationOptions

This is Android forground notification module. If you need to run background Geolocation on Android, you must define notification.

PropTypeDescriptionDefaultSince
channelIDstringAndroid notification channel id"LOCATION_SERVICE_CHANNEL"0.0.1
channelNamestringAndroid notification cannel name"Geolocation tracking notification"0.0.1
headerstringAndroid notification header"Geolocation tracker"0.0.1
messagestringAndroid notification message"Geolocation tracking now."0.0.1
iconstringAndroid notification icon. Icon's path should be in android/app/res folder. Do not use image mine-type like .png, .jpeg etc."mipmap/ic_launcher"0.0.1

GeolocationConnectOptions

After location updates, upload data to server. multipart-formdata

PropTypeDescriptionDefaultSince
urlstringUpdate urlnull0.0.1
body{ name: string: string | number; }Update body. You can get latitude and longitude data as @latitude and @longitude.null0.0.1

GeolocationUpdatesCallbackEvent

PropTypeDescriptionSince
longitudenumberUser location longitude0.0.1
latitudenumberUser location latitude0.0.1

Type Aliases

GeolocationUpdatesCallback

Each time Geolocation updates, It fires. It's only for forground state.

(data: GeolocationUpdatesCallbackEvent): void

Enums

GeolocationPermissionState

MembersValueDescription
granted"granted"Geolocation permissions granted
denied"denied"Geolocation permissions denied
prompt"prompt"User has not yet set permissions.

Full Example

import { Geolocation, GeolocationAlert, GeolocationConnect } from '@aldegad/capacitor-geolocation';

async startLocationUpdates() {
    const permissionOptions:GeolocationPermissionOptions = {
        promptAlert: null,
        deniedAlert: null
    }
    const promptAlert:GeolocationAlertOptions = {
        header: '위치권한 필요',
        message: '이 앱은 원활한 사용을 위해 위치권한을 필요로 합니다.\n위치 권한을 허용해주세요.',
        cancelText: '거부',
        okText: '확인'
    }
    const deniedAlert:GeolocationAlertOptions = {
        header: '위치권한 거부됨',
        message: '이 앱은 원활한 사용을 위해 위치권한을 필요로 합니다.\n[권한 -> 위치]로 이동하여 권한을 허용해주세요.',
        cancelText: '거부',
        okText: '이동'
    }
    permissionOptions.promptAlert = promptAlert;
    permissionOptions.deniedAlert = deniedAlert;
    const { state } = await Geolocation.requestPermission(permissionOptions);
    
    if(state !== 'granted') return;
    
    const updatesOptions:GeololocationUpdatesOptions = {
        background: null,
        notification: null,
        connect: null
    }
    const background:boolean = true;
    const notification:GeolocationNotificationOptions = {
        channelID: 'LOCATION_SERVICE_CHANNEL',
        channelName: '근로자 안전 위치 관리',
        header: '근로자 안전 관리 시스템',
        message: '안전한 근무를 위해 위치관리 시스템을 작동 중 입니다.',
        icon: 'drawable/default_dark'
    }
    const connect:GeolocationConnectOptions = {
        url: 'https://devmonster.co.kr/api/test_log',
        body: {
            user_id: 'ef34f3f3',
            user_position: 'User position is @latitude and @longitude'
        }
    }
    updatesOptions.background = background;
    updatesOptions.notification = notification;
    updatesOptions.connect = connect;
    Geolocation.startLocationUpdates(updatesOptions, ({latitude, longitude}) => {
        console.log();
    });
}
stopLocationUpdates() {
    Geolocation.startLocationUpdates();
}
0.1.0

2 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.3

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.6

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago