All Projects → no23reason → React Geolocated

no23reason / React Geolocated

Licence: mit
React Higher-Order Component for using Geolocation API

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to React Geolocated

trackanimation
Track Animation is a Python 2 and 3 library that provides an easy and user-adjustable way of creating visualizations from GPS data.
Stars: ✭ 74 (-60%)
Mutual labels:  gps, geolocation
Skylift
Wi-Fi Geolocation Spoofing with the ESP8266
Stars: ✭ 223 (+20.54%)
Mutual labels:  gps, geolocation
Ffwdme.js
[DEPRECATED!] 🛑 A JavaScript toolkit that aims to bring interactive GPS driving directions to the mobile browser
Stars: ✭ 150 (-18.92%)
Mutual labels:  gps, geolocation
Jpx
JPX - Java GPX library
Stars: ✭ 125 (-32.43%)
Mutual labels:  gps, geolocation
P5.geolocation
a geolocation and geofencing library for p5.js
Stars: ✭ 75 (-59.46%)
Mutual labels:  gps, geolocation
orange3-geo
🍊 🌍 Orange add-on for dealing with geography and geo-location
Stars: ✭ 22 (-88.11%)
Mutual labels:  gps, geolocation
Geolocation
Flutter geolocation plugin for Android and iOS.
Stars: ✭ 205 (+10.81%)
Mutual labels:  gps, geolocation
Ulogger Server
μlogger • web viewer for tracks uploaded with μlogger mobile client
Stars: ✭ 315 (+70.27%)
Mutual labels:  gps, geolocation
Phpgpx
Simple library for reading and creating GPX files written in PHP.
Stars: ✭ 92 (-50.27%)
Mutual labels:  gps, geolocation
Coregpx
A library for parsing and creation of GPX location files. Purely Swift.
Stars: ✭ 132 (-28.65%)
Mutual labels:  gps, geolocation
Track Ip
Advanced Ip Tracker Tool
Stars: ✭ 150 (-18.92%)
Mutual labels:  geolocation
Ulogger Android
μlogger • android application for real-time collection and publishing of geolocation data
Stars: ✭ 168 (-9.19%)
Mutual labels:  gps
Traccar Client Ios
Traccar Client for iOS
Stars: ✭ 149 (-19.46%)
Mutual labels:  gps
Speedmeter
Display the gps speed [Android]
Stars: ✭ 169 (-8.65%)
Mutual labels:  gps
Geo Three
Tile based geographic world map visualization library for threejs
Stars: ✭ 146 (-21.08%)
Mutual labels:  geolocation
Marine Api
Java Marine API - NMEA 0183 library for Java
Stars: ✭ 174 (-5.95%)
Mutual labels:  gps
Ip2location Go
Use IP2Location geolocation database to lookup the geolocation information with IP2Location Go Package. It can be used to determine country, region, city, coordinates, zip code, time zone, ISP, domain name, connection type, area code, weather, MCC, MNC, mobile brand name, elevation and usage type that any IP address or hostname originates from.
Stars: ✭ 142 (-23.24%)
Mutual labels:  geolocation
Georaptor
Python Geohash Compression Tool
Stars: ✭ 143 (-22.7%)
Mutual labels:  geolocation
React Native Android Location Services Dialog Box
React Native Android Location Services Dialog Box
Stars: ✭ 175 (-5.41%)
Mutual labels:  geolocation
Lwgps
Lightweight GPS NMEA parser for embedded systems
Stars: ✭ 172 (-7.03%)
Mutual labels:  gps

Node.js CI codecov npm version Commitizen friendly semantic-release

react-geolocated - React.js Higher-Order Component for using Geolocation API

Demo

Basic demo can be found at the demo page.

Basic Usage

Install using npm:

npm install react-geolocated --save

Then use in your application like this:

import React from "react";
import { geolocated } from "react-geolocated";

class Demo extends React.Component {
    render() {
        return !this.props.isGeolocationAvailable ? (
            <div>Your browser does not support Geolocation</div>
        ) : !this.props.isGeolocationEnabled ? (
            <div>Geolocation is not enabled</div>
        ) : this.props.coords ? (
            <table>
                <tbody>
                    <tr>
                        <td>latitude</td>
                        <td>{this.props.coords.latitude}</td>
                    </tr>
                    <tr>
                        <td>longitude</td>
                        <td>{this.props.coords.longitude}</td>
                    </tr>
                    <tr>
                        <td>altitude</td>
                        <td>{this.props.coords.altitude}</td>
                    </tr>
                    <tr>
                        <td>heading</td>
                        <td>{this.props.coords.heading}</td>
                    </tr>
                    <tr>
                        <td>speed</td>
                        <td>{this.props.coords.speed}</td>
                    </tr>
                </tbody>
            </table>
        ) : (
            <div>Getting the location data&hellip; </div>
        );
    }
}

export default geolocated({
    positionOptions: {
        enableHighAccuracy: false,
    },
    userDecisionTimeout: 5000,
})(Demo);

Props

The props passed to the wrapped component are:

{
    coords: {
        latitude,
        longitude,
        altitude,
        accuracy,
        altitudeAccuracy,
        heading,
        speed,
    },
    isGeolocationAvailable, // boolean flag indicating that the browser supports the Geolocation API
    isGeolocationEnabled, // boolean flag indicating that the user has allowed the use of the Geolocation API
    positionError, // object with the error returned from the Geolocation API call
}

The coords prop is equivalent to the Coordinates object and the positionError is equivalent to the PositionError.

Additional props the resulting component can take:

{
  // callback call on Geolocation API error, takes PositionError as the only argument
  onError,
  // callback call on Geolocation API success, takes Position as the only argument
  onSuccess,
}

PropTypes

Unfortunately, the geolocated HOC cannot add the prop types to the wrapped component directly, as the ESLint will not pick that up. For this reason, prop types are exported as the geoPropTypes object. Using them is simple with Object.assign (or if you already depend on it, lodash merge function is useful as well), or, if your environment supports it, using the object spread syntax:

import React from "react";
import { geolocated, geoPropTypes } from "react-geolocated";

class Demo extends React.Component {
    // Same as the basic example
}

// Using Object.assign
Demo.propTypes = Object.assign({}, Demo.propTypes, geoPropTypes);
// Using ES6 object spread syntax
Demo.propTypes = { ...Demo.propTypes, ...geoPropTypes };

export default geolocated()(Demo);

Configuration

The geolocated function takes optional configuration parameter:

{
    positionOptions: {
        enableHighAccuracy: true,
        maximumAge: 0,
        timeout: Infinity,
    },
    watchPosition: false,
    userDecisionTimeout: null,
    suppressLocationOnMount: false,
    geolocationProvider: navigator.geolocation,
    isOptimisticGeolocationEnabled: true
}

The positionOptions object corresponds to the PositionOptions of the Geolocation API.

By default the component only sets position once. To watch the user's position and provide live updates to position, set watchPosition = true. The geolocation event handler is unregistered when the component unmounts.

If set, the userDecisionTimeout determines how much time (in miliseconds) we give the user to make the decision whether to allow to share their location or not. In Firefox, if the user declines to use their location, the Geolocation API call does not end with an error. Therefore we want to fallback to the error state if the user declines and the API does not tell us.

The location is obtained when the component mounts by default. If you want to prevent this and get the location later, set the suppressLocationOnMount to true and using a ref in the parent component call its getLocation method (see the demo's App component for example of this).

The geolocationProvider allows to specify alternative source of the geolocation API. This was added mainly for testing purposes, however feel free to use it if need be.

The isOptimisticGeolocationEnabled allows you to set the default value of isGeolocationEnabled. By default it is true, which means isGeolocationEnabled will be true on first render. There may be cases where you don't want to assume that the user will give permission, ie you want the first value to for isGeolocationEnabled to be false. In that case, you can set isOptimisticGeolocationEnabled to false.

TypeScript

This project ships with type definitions for TypeScript provided. You can use them in your TypeScript files like this:

import * as React from "react";
import { GeolocatedProps, geolocated } from "react-geolocated";

interface IDemoProps {
    label: string;
}

class Demo extends React.Component<IDemoProps & GeolocatedProps> {
    render(): JSX.Element {
        return (
            <div>
                label: {this.props.label}
                lattitude: {this.props.coords && this.props.coords.latitude}
            </div>
        );
    }
}

export default geolocated()(Demo);

Browser support

  • Chrome ≥ 5
  • Firefox ≥ 3.5
  • Internet Explorer ≥ 9
  • Opera ≥ 10.60
  • Safari ≥ 5

Acknowledgements

Many thanks belong to @mcumpl for the original idea for this as well as many suggestions and comments.

This project uses the react-component-boilerplate.

License

react-geolocated is available under MIT. See LICENSE for more details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].