I am trying to make a listener for LocationUpdates and when I am using both developer/androids tutorial and the tutorial I find on the web the method requestLocationUpdates() is used with googleClientApi, locationRequest and "this" as a third parameter.
This is not working as a parameter for some reason. I have tried different advices from simular questions here on stackoverflow.
I have changed the imoport to
import android.location.LocationListener;
I have implemented all methods neccesary
public class MapsActivity extends FragmentActivity implements
OnMapReadyCallback,GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener, LocationListener {
private GoogleApiClient mGoogleApiClient=null;
LocationRequest mLocationRequest = null;
private Location mLastLocation;
private GoogleMap mMap;
All works until there is time to use requestLocationUpdates-method at startLocationUpdates in the bottom
public void buildGoogleMapApi()
{
if (mGoogleApiClient == null) {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
mGoogleApiClient.connect();
}
protected LocationRequest createLocationRequest(LocationRequest mLocationRequest) {
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
return mLocationRequest;
}
protected void startLocationUpdates() {
createLocationRequest(mLocationRequest);
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
@Override
public void onConnected(Bundle connectionHint)
{
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED)
{
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
}
else
{
}
}
else
{
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
}
LatLng latLngPosition = new LatLng(mLastLocation.getLatitude(), mLastLocation.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLngPosition).title("YOU ARE HERE!!"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLngPosition));
mMap.setMapType(mMap.MAP_TYPE_HYBRID);
startLocationUpdates();
}
I have tried to change this to getApplicationContext() and it did not work.
I have followed a lot of tutorials but nothing have worked so far. Anyone see any mistakes in my code? Thanks