Giter VIP home page Giter VIP logo

react-native-geo-fencing's Introduction

react-native-geo-fencing

Native modules to determine if a location is within defined geographical boundaries using Google Geometry library for ios and android.

Usage

import GeoFencing from 'react-native-geo-fencing';
// with navigator geolocation
componentDidMount() {
  const polygon = [
    { lat: 3.1336599385978805, lng: 101.31866455078125 },
    { lat: 3.3091633559540123, lng: 101.66198730468757 },
    { lat: 3.091150714460597,  lng: 101.92977905273438 },
    { lat: 2.7222113428196213, lng: 101.74850463867188 },
    { lat: 2.7153526167685347, lng: 101.47933959960938 },
    { lat: 3.1336599385978805, lng: 101.31866455078125 } // last point has to be same as first point
  ];

  navigator.geolocation.getCurrentPosition(
    (position) => {
      let point = {
        lat: position.coords.latitude,
        lng: position.coords.longitude
      };

      GeoFencing.containsLocation(point, polygon)
        .then(() => console.log('point is within polygon'))
        .catch(() => console.log('point is NOT within polygon'))
    },
    (error) => alert(error.message),
    { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
  );
}
// with only point and polygon
componentDidMount() {
  const polygon = [
    { lat: 3.1336599385978805, lng: 101.31866455078125 },
    { lat: 3.3091633559540123, lng: 101.66198730468757 },
    { lat: 3.091150714460597,  lng: 101.92977905273438 },
    { lat: 3.1336599385978805, lng: 101.31866455078125 } // last point has to be same as first point
  ];

  let point = {
    lat: 2.951269758090068,
    lng: 101.964111328125
  };

  GeoFencing.containsLocation(point, polygon)
    .then(() => console.log('point is within polygon'))
    .catch(() => console.log('point is NOT within polygon'))
}

Installation

$ npm install --save react-native-geo-fencing

ios

Within ios/ directory of your react-native app:

  1. Create a Podfile manually or simply

    $ pod init

# Podfile for cocoapods 1.0
platform :ios, '7.0'

target 'yourAppTarget' do
  pod 'React', path: '../node_modules/react-native'
  pod 'react-native-geo-fencing', path: '../node_modules/react-native-geo-fencing'
end
# for older version of CocoaPods
platform :ios, '7.0'

pod 'React', path: '../node_modules/react-native'
pod 'react-native-geo-fencing', path: '../node_modules/react-native-geo-fencing'
  1. Then

     $ pod install
    
  2. Add $(inherited) for Other Linker Flags to your project's build settings linking_library

  3. Link the static library linking_binaries

android

  1. Add the following to android/settings.gradle
include ':react-native-geo-fencing'
project(':react-native-geo-fencing').projectDir = new File(settingsDir, '../node_modules/react-native-geo-fencing/android')
  1. Add the following to android/app/build.gradle
dependencies {
  // ...
  compile project(':react-native-geo-fencing')
}
  1. Edit android/src/.../MainActivity.java
// ...
import com.surialabs.rn.geofencing.GeoFencingPackage;// <--

public class MainActivity extends ReactActivity {
    @Override
    protected List<ReactPackage> getPackages() {
        return Arrays.<ReactPackage>asList(
            new MainReactPackage(),
            new GeoFencingPackage() // <--
        );
    }
}
  1. Enable access to location when using the app from your AndroidManifest.xml

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

react-native-geo-fencing's People

Contributors

chinloongtan avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.