Need help upgrading to Ionic Framework 4.0? Get assistance with our Enterprise Migration Services EXPLORE NOW

Geofence

Improve this doc

Monitors circular geofences around latitude/longitude coordinates, and sends a notification to the user when the boundary of a geofence is crossed. Notifications can be sent when the user enters and/or exits a geofence. Geofences persist after device reboot. Geofences will be monitored even when the app is not running.

Repo: https://github.com/cowbell/cordova-plugin-geofence

Installation

  1. Install the Cordova and Ionic Native plugins:
    $ ionic cordova plugin add cordova-plugin-geofence
    $ npm install --save @ionic-native/geofence@4
    
  2. Add this plugin to your app's module

Supported platforms

Usage

import { Geofence } from '@ionic-native/geofence';

...

constructor(private geofence: Geofence) {
  // initialize the plugin
  geofence.initialize().then(
    // resolved promise does not return a value
    () => console.log('Geofence Plugin Ready'),
    (err) => console.log(err)
  )
}

...

private addGeofence() {
  //options describing geofence
  let fence = {
    id: '69ca1b88-6fbe-4e80-a4d4-ff4d3748acdb', //any unique ID
    latitude:       37.285951, //center of geofence radius
    longitude:      -121.936650,
    radius:         100, //radius to edge of geofence in meters
    transitionType: 3, //see 'Transition Types' below
    notification: { //notification settings
        id:             1, //any unique ID
        title:          'You crossed a fence', //notification title
        text:           'You just arrived to Gliwice city center.', //notification body
        openAppOnClick: true //open app when notification is tapped
    }
  }

  this.geofence.addOrUpdate(fence).then(
     () => console.log('Geofence added'),
     (err) => console.log('Geofence failed to add')
   );
}

Transition Types

Transition type specifies whether the geofence should trigger when the user enters and/or leaves the geofence.

Supported values

Defining a Geofence

Geofences are defined by an object that is passed to addOrUpdate(). Object properties are:

Troubleshooting

I get compile errors when I run ionic build ios or ionic run ios.

This could be caused by the Cordova project directory in /platforms/ios not being named correctly. Try running ionic cordova platform rm <platform> then run ionic cordova platform add <platform> to recreate the platform directories.

Instance Members

TransitionType

onTransitionReceived()

Subscribe to get notified when a transition is received

Returns: Observable<any>

initialize()

Initializes the plugin. User will be prompted to allow the app to use location and notifications.

Returns: Promise<void>

addOrUpdate()

Adds a new geofence or array of geofences. For geofence object, see above.

Returns: Promise<void>

remove()

Removes a geofence or array of geofences. geofenceID corresponds to one or more IDs specified when the geofence was created.

Returns: Promise<void>

removeAll()

Removes all geofences.

Returns: Promise<void>

getWatched()

Returns an array of geofences currently being monitored.

Returns: Promise<Array<string>>

onNotificationClicked()

Called when the user clicks a geofence notification. iOS and Android only.

Returns: Observable<any>

API

Native

General