Giter Site home page Giter Site logo

reyadrahman / locationprovider Goto Github PK

View Code? Open in Web Editor NEW

This project forked from david-serrano/locationprovider

0.0 1.0 0.0 141 KB

A tiny library that abstracts getting a users location using resources on the phone, without the need for API calls, and provides a configurable and scalable implementation

License: MIT License

Java 100.00%

locationprovider's Introduction

LocationProvider

LocationProvider is a tiny library that abstracts away a lot of the pain of getting a users location from the phone (without having to call location APIs), provides a fully configurable standard Builder pattern and a set of callbacks.

With the most basic usage, you give it a context and a callback and you're good to go!

DISCLAIMER: This library needs the COARSE and FINE permissions, but it assumes they have been given beforehand

Install instructions

  1. add the jitpack dependency:
	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
  1. add a dependency to your app/build.gradle:
dependencies {
           implementation 'com.github.david-serrano:locationprovider:v1'
   }

Example usage:

//create a callback
LocationProvider.LocationCallback callback = new LocationProvider.LocationCallback() {
          @Override
          public void onNewLocationAvailable(float lat, float lon) {
              //location update
          }

          @Override
          public void locationServicesNotEnabled() {
           	//failed finding a location
          }

          @Override
          public void updateLocationInBackground(float lat, float lon) {
              //if a listener returns after the main locationAvailable callback, it will go here
          }

          @Override
          public void networkListenerInitialised() {
            //when the library switched from GPS only to GPS & network
          }
      };
  
  //initialise an instance with the two required parameters
  LocationProvider locationProvider = new LocationProvider.Builder()
  .setContext(this)
  .setListener(callback)
  .create();
  
  //start getting location
  locationProvider.requestLocation();

How it works:

The library provides a standard Builder pattern with a few configurable options but uses default values if none are set. In the most standard use case it will:

  1. Look for last known locations in order the following order passive, gps, network. If those return a value onNewLocationAvailable will be called.
  2. Initialise a GPS listener or network listener depending on availability of service (high accuracy vs battery saving for example)
  3. Return location based on available providers:
  • If GPS initialised but taking too long, start a network listener after X (configurable) seconds.
  • If only network initialised, some phones fail to update the listener but update the passive values correctly, so after Y (configurable) seconds, try the last known locations again.
  1. Return values for GPS/network when available and shut down self.

License

MIT License

locationprovider's People

Contributors

david-serrano avatar

Watchers

Mostafa Mohammad Reyad Rahman 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.