Guest User

Untitled

a guest
Jan 21st, 2016
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.79 KB | None | 0 0
  1. public class MyLocationListener implements
  2.         GoogleApiClient.ConnectionCallbacks,
  3.         GoogleApiClient.OnConnectionFailedListener,
  4.         LocationListener {
  5.     private final Context context;
  6.     private GoogleMap mMap;
  7.     private BreakDownOnMaps breakDownOnMaps = new BreakDownOnMaps();
  8.     protected GoogleApiClient mGoogleApiClient;
  9.     private LocationRequest mLocationRequest =  new LocationRequest();
  10.  
  11.     public MyLocationListener(Context context) {
  12.         this.context = context;
  13.         buildApi();
  14.     }
  15.  
  16.     @Override
  17.     public void onLocationChanged(Location location) {
  18.         breakDownOnMaps.handleNewLocation(location);
  19.     }
  20.  
  21.     @Override
  22.     public void onConnected(Bundle bundle) {
  23.         System.out.println("onConnected");
  24.         if (ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
  25.                 && ActivityCompat.checkSelfPermission(context, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
  26.         }
  27.         Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
  28.         if (location == null) {
  29.             LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);
  30.         }
  31.         else {
  32.             breakDownOnMaps.handleNewLocation(location);
  33.         };
  34.     }
  35.  
  36.     private void buildApi() {
  37.         mGoogleApiClient = new GoogleApiClient.Builder(context)
  38.                 .addConnectionCallbacks(this)
  39.                 .addOnConnectionFailedListener(this)
  40.                 .addApi(LocationServices.API)
  41.                 .build();
  42.     }
  43.  
  44.     public void connect() {
  45.         mGoogleApiClient.connect();
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment