Created
September 7, 2015 12:59
-
-
Save Krishan14sharma/6a4559961416ce384cd7 to your computer and use it in GitHub Desktop.
Find location
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.zapbuild.myi.interactors; | |
| import android.location.Location; | |
| import android.os.Bundle; | |
| import com.google.android.gms.common.ConnectionResult; | |
| import com.google.android.gms.common.api.GoogleApiClient; | |
| import com.google.android.gms.location.LocationServices; | |
| import static com.zapbuild.myi.base.BaseApp.getContext; | |
| /** | |
| * Created by krishan on 27/5/15. | |
| * provides last known fused location else null in case of no location found. | |
| * Don't forget to add this in manifest file - | |
| * <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> | |
| */ | |
| public class LocationService implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { | |
| /** | |
| * Provides the entry point to Google Play services. | |
| */ | |
| protected GoogleApiClient mGoogleApiClient; | |
| protected LocationCallBack mCallBack; | |
| public LocationService() { | |
| buildGoogleApiClient(); | |
| } | |
| public LocationService(LocationCallBack mCallBack) { | |
| buildGoogleApiClient(); | |
| this.mCallBack = mCallBack; | |
| } | |
| /** | |
| * Connects the location api | |
| */ | |
| public void connectApi() { | |
| mGoogleApiClient.connect(); | |
| } | |
| /** | |
| * disconnects the api | |
| */ | |
| public void disconnectApi() { | |
| if (mGoogleApiClient.isConnected()) { | |
| mGoogleApiClient.disconnect(); | |
| } | |
| } | |
| /** | |
| * Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API. | |
| */ | |
| protected synchronized void buildGoogleApiClient() { | |
| mGoogleApiClient = new GoogleApiClient.Builder(getContext()) | |
| .addConnectionCallbacks(this) | |
| .addOnConnectionFailedListener(this) | |
| .addApi(LocationServices.API) | |
| .build(); | |
| } | |
| @Override | |
| public void onConnected(Bundle bundle) { | |
| Location mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); | |
| if (mLastLocation != null) { | |
| if (mCallBack != null) mCallBack.onLastLocationReceived(mLastLocation); | |
| } else { | |
| if (mCallBack != null) mCallBack.onLocationFailed(); | |
| } | |
| } | |
| @Override | |
| public void onConnectionSuspended(int i) { | |
| mGoogleApiClient.connect(); | |
| } | |
| @Override | |
| public void onConnectionFailed(ConnectionResult connectionResult) { | |
| // GooglePlayServicesUtil.showErrorNotification(connectionResult.getErrorCode(), getContext()); | |
| if (mCallBack != null) mCallBack.onLocationFailed(); | |
| } | |
| public LocationCallBack getmCallBack() { | |
| return mCallBack; | |
| } | |
| public void setmCallBack(LocationCallBack mCallBack) { | |
| this.mCallBack = mCallBack; | |
| } | |
| public interface LocationCallBack { | |
| void onLastLocationReceived(Location location); | |
| void onLocationFailed(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment