Git Product home page Git Product logo

locationhelper's Introduction

LocationHelper

This is a sample Android application to show how to track user's location and manage the updates

ScreenShot

Getting current location through FusedLocationAPI provided by Google is a bit tricky to implement, but the below step-by-step guidelines will explain the procedure.

  1. First, you need to add the needed dependency in your build.gradle file.
     compile 'com.google.android.gms:play-services-location:10.+โ€™
  1. Then add the following permissions into the AndroidManifest.xml file.
     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
  1. Next is permission check, From Android 6.0 user can deny the permission request, so before try to get the location we need to check the status of the location access. For this use PermissionUtil class.

  2. For making a connection with Location API, need to create an instance of GoogleApiCLient.

    GoogleApiClient mGoogleApiClient;

    mGoogleApiClient = new GoogleApiClient.Builder(this)
                           .addConnectionCallbacks(this)
                           .addOnConnectionFailedListener(this)
                           .addApi(LocationServices.API).build();

    mGoogleApiClient.connect();
  1. At next implement the OnConnectionFailedListener and ConnectionCallbacks for managing the GoogleApiclient connection.
    public class MyLocationUsingLocationAPI extends AppCompatActivity implements ConnectionCallbacks,
    OnConnectionFailedListener,OnRequestPermissionsResultCallback,                  
    {

      @Override
      public void onConnectionFailed(ConnectionResult result) {
         Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = "+ result.getErrorCode());
      }

      @Override
      public void onConnected(Bundle arg0) {
        // Once connected with google api, get the location
      }

      @Override
      public void onConnectionSuspended(int arg0) {
          mGoogleApiClient.connect();
      }
    }
  1. For refreshing the location of the device at regular intervals, use LocationRequest objects.
    LocationRequest mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(10000);
    mLocationRequest.setFastestInterval(5000);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                                              .addLocationRequest(mLocationRequest);
  1. Then check the google play service availability, if available get the last known location of the user's device from the Location service as follows.
     mLastLocation = LocationServices.FusedLocationApi
                        .getLastLocation(mGoogleApiClient);
  1. Using the Geocoder class you can get the address from the above location.
     public Address getAddress(double latitude,double longitude)
     {
         Geocoder geocoder;
         List addresses;
         geocoder = new Geocoder(this, Locale.getDefault());

         try 
         {
             // Here 1 represent max location result to returned, by documents it recommended 1 to 5
             addresses = geocoder.getFromLocation(latitude,longitude, 1); 
             return addresses.get(0);
          } catch (IOException e) {
             e.printStackTrace();
          }
        return null;
      }

For more information, check out my detailed guide here : http://droidmentor.com/get-the-current-location-in-android/

locationhelper's People

Contributors

jaisonfdo avatar

Watchers

 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.