Git Product home page Git Product logo

react-native-android-location's Introduction

react-native-android-location

Location acquisition through Network or GPS from android device without using Google Play Service.

Installation

Install the npm package

npm i --save react-native-android-location

Add it to your android project

  • In android/settings.gradle
...
include ':RNAlocation', ':app'
project(':RNAlocation').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-android-location')
  • In android/app/build.gradle
...
dependencies {
    ...
    compile project(':RNAlocation')
}
  • register module (in MainActivity.java)
import com.syarul.rnalocation.RNALocation;  // <--- import

public class MainActivity extends Activity implements DefaultHardwareBackBtnHandler {
  ......

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mReactRootView = new ReactRootView(this);

    mReactInstanceManager = ReactInstanceManager.builder()
      .setApplication(getApplication())
      .setBundleAssetName("index.android.bundle")
      .setJSMainModuleName("index.android")
      .addPackage(new MainReactPackage())
      .addPackage(new RNALocation()) // <-- Register package here
      .setUseDeveloperSupport(BuildConfig.DEBUG)
      .setInitialLifecycleState(LifecycleState.RESUMED)
      .build();

    mReactRootView.startReactApplication(mReactInstanceManager, "example", null);

    setContentView(mReactRootView);
  }

  ......

}

Add permissions to your Project

Add this to your AndroidManifest file;

// file: android/app/src/main/AndroidManifest.xml

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

Example

'use strict';

var React = require('react-native');

// For registering the Callback-Listener
var { DeviceEventEmitter } = require('react-native');

var RNALocation = require('react-native-android-location');

var {
  AppRegistry,
  StyleSheet,
  Text,
  View,
} = React;

var RNALocation = React.createClass({
  // Create and Reset initial State Longitude (lng) and Latitude (lat)	
  getInitialState: function() { return { lng: 0.0, lat: 0.0}; },

  componentDidMount: function() {
  	  // Register Listener Callback !Important!
      DeviceEventEmitter.addListener('updateLocation', function(e: Event) {
          this.setState({lng: e.Longitude, lat: e.Latitude });
      }.bind(this));
      // Initialize RNALocation
      RNALocation.getLocation();
  },

  render() {
    return (
      <View style={styles.container}>
        <Text style={styles.location}>
          Lng: {this.state.lng} Lat: {this.state.lat}
        </Text>
      </View>
    );
  }
});

var styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  location: {
    textAlign: 'center'
  }
});

AppRegistry.registerComponent('RNALocation', () => RNALocation);

react-native-android-location's People

Contributors

dan-lee avatar syarul avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

react-native-android-location's Issues

Update README.md on npmjs.com

The documentation on npmjs.com is outdated. Would be great if you could update it. Was struggling with the tutorial because of the difference ;-)
Cheers! ๐Ÿ‘

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.