Please use the fused location client
Documentation
It's battery efficient and very easy to work with and is pretty much the standard in location based apps.
A youtube tutorial
An example from one of my apps:
@Override
public void onStart() {
super.onStart();
fusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(getActivity());
}
@Override
public void onMapReady(GoogleMap googleMap) {
if (googleMap == null) return;// GUARD
map = googleMap;
getDeviceLocationUpdates();
}
/**
* Start listening to location changes
*/
private void getDeviceLocationUpdates() {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
return;
}
locationRequest = new LocationRequest();
locationRequest.setInterval(Constants.DRIVING_TIME_INTERVAL);
locationRequest.setFastestInterval(Constants.DRIVING_TIME_INTERVAL);
locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
fusedLocationProviderClient.requestLocationUpdates(locationRequest, locationCallback, Looper.myLooper());
}
LocationCallback locationCallback = new LocationCallback() {
@Override
public void onLocationResult(LocationResult locationResult) {
List<Location> locationList = locationResult.getLocations();
if (locationList.size() > 0) {
//The last location in the list is the newest
Location location = locationList.get(locationList.size() - 1);
Log.i(TAG, "Location: " + location.getLatitude() + " " + location.getLongitude());
lastLocation = location;
if (currentLocationMarker != null) {
currentLocationMarker.remove();
}
if (mapPin == null) {
mapPin = GraphicUtils.resizeImage(getActivity(), Constants.MAP_MARKER_IMAGE_NAME, 100, 100);
}
LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
placeCurrentLocationMarker(latLng);
//move map camera
map.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, Constants.DEFAULT_ZOOM));
}
}
};
/**
* Places a marker with user image on the map on given location
*
* @param location GPS Location
*/
private void placeCurrentLocationMarker(LatLng location) {
Bitmap bitmap = GraphicUtils.createContactBitmap(getActivity(), BitmapFactory.decodeResource(
getActivity().getResources(),
Integer.valueOf(((MainActivity) getActivity()).currentUser.getImgUrl())));
MarkerOptions markerOptions = new MarkerOptions();
markerOptions.position(location);
markerOptions.title(getString(R.string.you_are_here));
markerOptions.icon((bitmap == null) ?
BitmapDescriptorFactory.fromBitmap(mapPin) :
BitmapDescriptorFactory.fromBitmap(bitmap));
currentLocationMarker = map.addMarker(markerOptions);
}