• Stars
    star
    198
  • Rank 196,898 (Top 4 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created almost 10 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Utility class for easy access to the device location on Android

Android-SimpleLocation

Utility class for easy access to the device location on Android

Requirements

  • Android 2.2+

Installation

  • Add this library to your project
    • Declare the Gradle repository in your root build.gradle

      allprojects {
          repositories {
              maven { url "https://jitpack.io" }
          }
      }
    • Declare the Gradle dependency in your app module's build.gradle

      dependencies {
          compile 'com.github.delight-im:Android-SimpleLocation:v1.1.0'
      }

Usage

Decide for the required granularity

  • If you want to get the device's location with fine granularity (between 2m and 100m precision), GPS will be required. This consumes more battery but is most precise.
  • If you want to get the device's location with coarse granularity only (precise to several hundred meters), the location will be retrieved from the network (Wi-Fi and cell towers). This saves battery but is less precise.

Add the required permissions

For fine location (GPS location), add the following permission in your AndroidManifest.xml:

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

For coarse location (network location), add the following permission in your AndroidManifest.xml:

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

Retrieve the location from the device

public class MyActivity extends Activity {

    private SimpleLocation location;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // ...

        // construct a new instance of SimpleLocation
        location = new SimpleLocation(this);

        // if we can't access the location yet
        if (!location.hasLocationEnabled()) {
            // ask the user to enable location access
            SimpleLocation.openSettings(this);
        }

        findViewById(R.id.someView).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                final double latitude = location.getLatitude();
                final double longitude = location.getLongitude();

                // TODO
            }

        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        // make the device update its location
        location.beginUpdates();

        // ...
    }

    @Override
    protected void onPause() {
        // stop location updates (saves battery)
        location.endUpdates();

        // ...

        super.onPause();
    }

}

Getting notified about location updates

location.setListener(new SimpleLocation.Listener() {

    public void onPositionChanged() {
        // new location data has been received and can be accessed
    }

});

Extended constructor options

Context context = this;
boolean requireFineGranularity = false;
new SimpleLocation(context, requireFineGranularity);

// or

Context context = this;
boolean requireFineGranularity = false;
boolean passiveMode = false;
new SimpleLocation(context, requireFineGranularity, passiveMode);

// or

Context context = this;
boolean requireFineGranularity = false;
boolean passiveMode = false;
long updateIntervalInMilliseconds = 10 * 60 * 1000;
new SimpleLocation(context, requireFineGranularity, passiveMode, updateIntervalInMilliseconds);

// or

Context context = this;
boolean requireFineGranularity = false;
boolean passiveMode = false;
long updateIntervalInMilliseconds = 10 * 60 * 1000;
boolean requireNewLocation = false;
new SimpleLocation(context, requireFineGranularity, passiveMode, updateIntervalInMilliseconds, requireNewLocation);

Retrieving available data

SimpleLocation.Point position = location.getPosition();
// double latitude = position.latitude;
// and
// double longitude = position.longitude;

// or

double latitude = location.getLatitude();
// and
double longitude = location.getLongitude();

// or

long timestampInMilliseconds = location.getTimestampInMilliseconds();

// or

long elapsedTimeInNanoseconds = location.getElapsedTimeInNanoseconds();

// or

float speed = location.getSpeed();

// or

double altitude = location.getAltitude();

Calculating the distance between two locations

double startLatitude = 59.95;
double startLongitude = 30.3;
double endLatitude = 44.84;
double endLongitude = -0.58;
location.calculateDistance(startLatitude, startLongitude, endLatitude, endLongitude);

// or

Point startPoint = new SimpleLocation.Point(59.95, 30.3);
Point endPoint = new SimpleLocation.Point(44.84, -0.58);
location.calculateDistance(startPoint, endPoint);

Blurring the location (for privacy reasons)

// reduce the precision to 10,000m (for privacy reasons)
int blurRadiusInMeters = 10000;
location.setBlurRadius(blurRadiusInMeters);

License

Copyright (c) delight.im <[email protected]>

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

More Repositories

1

Android-AdvancedWebView

Enhanced WebView component for Android that works as intended out of the box
Java
2,387
star
2

FreeGeoDB

Free database of geographic place names and corresponding geospatial data
PHP
1,586
star
3

HTML-Sheets-of-Paper

Word processor in your browser using HTML and CSS (for invoices, legal notices, etc.)
CSS
1,208
star
4

PHP-Auth

Authentication for PHP. Simple, lightweight and secure.
PHP
1,073
star
5

Faceless

[UNMAINTAINED] Faceless is where you can talk freely
Java
488
star
6

ShortURL

Bijective conversion between natural numbers (IDs) and short strings
C
381
star
7

Android-DDP

[UNMAINTAINED] Meteor's Distributed Data Protocol (DDP) for clients on Android
Java
273
star
8

PHP-PrivacyPolicy

Programmatically composable privacy policies for humans and machines
PHP
257
star
9

Knowledge

Random pieces of knowledge โ€” with anecdotes and quotes
189
star
10

PHP-Cookie

Modern cookie management for PHP
PHP
165
star
11

MovieContentFilter

Watch movies with the freedom (not) to filter
PHP
142
star
12

Emoji

[UNMAINTAINED] Emoji (Unicode) and emoticon support for Java and Android
Java
95
star
13

AppRater

Android library that lets you prompt users to rate your application on their appstore (e.g. Google Play)
Java
75
star
14

Android-Languages

Custom language selection and access to language names for Android
Java
74
star
15

PHP-Str

Convenient object-oriented operations on strings
PHP
70
star
16

Android-Commons

[UNMAINTAINED] Reusable components and utilities for Android
Java
69
star
17

AndroidPatternLock

List of all combinations for the Android pattern lock
Java
69
star
18

PHP-FileUpload

Simple and convenient file uploads โ€” secure by default
PHP
63
star
19

PHP-I18N

Internationalization and localization for PHP
PHP
57
star
20

Javadoc-to-Markdown

[UNMAINTAINED] Generate Markdown from your Javadoc, PHPDoc or JSDoc comments
JavaScript
56
star
21

JS-NeuralNetwork

Neural networks in JavaScript. Well-documented and object-oriented.
JavaScript
56
star
22

OpenSoccer

[UNMAINTAINED] Online Soccer Manager
PHP
51
star
23

PHP-DB

Safe and convenient SQL database access in a driver-agnostic way
PHP
47
star
24

Localize

[UNMAINTAINED] Collaborative Translation for Android
PHP
46
star
25

PHP-Router

Router for PHP. Simple, lightweight and convenient.
PHP
38
star
26

Android-Audio

High-level library for efficient playback of sounds and music on Android
Java
35
star
27

htaccess

.htaccess with reasonable defaults for most sites
ApacheConf
29
star
28

PHP-Foundation

Writing modern PHP applications efficiently
Shell
29
star
29

PHP-IDs

Short, obfuscated and efficient IDs for PHP
PHP
27
star
30

Secure-Firefox

Hardening Mozilla Firefox for maximum privacy and security
27
star
31

Android-Identicons

[UNMAINTAINED] Identicons for Android โ€” turn any data into a visual hash
Java
23
star
32

PHP-Random

The most convenient way to securely generate anything random in PHP
PHP
22
star
33

PHP-GitScraper

Downloads entire Git repositories from publicly accessible ".git" folders over HTTP
PHP
20
star
34

PHP-HTTP

Hypertext Transfer Protocol (HTTP) utilities for PHP
PHP
18
star
35

Android-Countries

Provides country codes in accordance with ISO-3166-1 and localized names for each country
Java
18
star
36

PHP-Base64

Simple and convenient Base64 encoding and decoding for PHP
PHP
15
star
37

PHP-BaseConvert

Conversion of arbitrarily large numbers between any two bases or alphabets
PHP
15
star
38

NationSoccer

[UNMAINTAINED] Free 1-vs-1 soccer game for Android
Java
14
star
39

PHP-Temporal

Immutable date and time for PHP with a convenient interface
PHP
13
star
40

Java-Shapefile-Parser

Parses ESRI shapefiles and extracts all spatial/geometric data with attributes
Java
11
star
41

Android-WebRequest

Fluent interface for easy HTTP requests to web servers (GET, POST, PUT or DELETE)
Java
11
star
42

Android-Time

Library for displaying locale-specific time in Android
Java
10
star
43

Android-Tasks

[UNMAINTAINED] Helper classes for recurring and automatic tasks in Android
Java
9
star
44

AndroidDrawableResizer

[UNMAINTAINED] Automatic resizing of Android drawables from one density to all others
PHP
8
star
45

Java-Crash-ID

Generates unique fingerprints for crashes on Android and the JVM
Java
8
star
46

AndroidEmoji-PNG

[UNMAINTAINED] Android emoji from AndroidEmoji.ttf as single icons in PNG format
PHP
8
star
47

Android-KeyValueSpinner

Spinner component that works with normal values but additionally lets you use keys of an arbitrary class
Java
7
star
48

AndroidImageScraper

Extracts all image URLs from a given website ordered by file size
Java
7
star
49

MusikPlayer

Simple Music Player for Windows/Linux/Mac
Java
6
star
50

PHP-Foundation-Core

Core of โ€œPHP-Foundationโ€
PHP
6
star
51

Android-InfiniteScrolling

Easily add infinite scrolling to ListView or GridView instances in Android
Java
5
star
52

2048

Fork of the popular game "2048"
CSS
5
star
53

Tools-for-Spotify

Tools and utilities for Spotify using the Spotify Web API
PHP
5
star
54

PHP-Alphabets

Sets of digits or characters that may be used for base conversions, encoding and decoding tasks, and input validation
PHP
4
star
55

Weather

Current weather data and hourly weather forecasts
PHP
4
star
56

JS-AbstractStorage

Flexible data storage for JavaScript backed by the Web Storage API
JavaScript
3
star
57

Maintenance

Simple and effective maintenance page for websites
PHP
3
star
58

Android-Monkey

[UNMAINTAINED] Library that generates pseudo-random streams of user input for Android apps
Java
3
star
59

JS-MediaPlayer

Convenient media playback with powerful controls in JavaScript
JavaScript
3
star
60

JS-PRNG

Pseudorandom number generator (PRNG) for JavaScript
JavaScript
2
star
61

2FA-Backup-Sheet

Create backup sheets for any service where you use two-factor authentication (2FA)
Shell
2
star
62

Batch-Renamer

Platform-independent utility for renaming batches of files
PHP
2
star
63

News60

Simple news aggregator and Twitter charts
PHP
2
star
64

PHP-OTP

One-time password (OTP) implementation for two-factor authentication with TOTP in accordance with RFC 6238 and RFC 4226
PHP
2
star
65

Requests

Logging and analysis of requests with optional static responses and redirects
Shell
2
star
66

Simple-ACRA-Storage

[UNMAINTAINED] Simple storage system for crash reports from ACRA on Android
PHP
2
star
67

Twitter-Charts-DE-AT-CH

Inofficial Twitter charts for Germany, Austria and Switzerland
1
star
68

Social-News-Germany

500 most-shared pieces from German online newspapers (March '13 โ€” May '14)
1
star