Felipe Felipe - 3 months ago 9
Android Question

Android - LocationListener cant define "this" on methods

Im creating an app to show users location on a map. The problem is that when calling the methods requestLocationUpdates, or removeUpdates, i get an error in the LocationListener parameter. I know this may be due to importing the wrong files, but i did import the com.google.android.gms.location.LocationListener one.
I did my research but really cant find the solution to this. I´ll put some of my code so it may help.
Thanks you for your help.

import com.google.android.gms.location.LocationListener;

import android.content.Context;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationManager;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;

import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,LocationListener {



private GoogleMap mMap;

LocationManager locationManager;
String provider;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
provider = locationManager.getBestProvider(new Criteria(),false);

}


@Override
public void onResume(){
super.onResume();
locationManager.requestLocationUpdates(provider,400,1,this);

}


@Override
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;

// Add a marker in Sydney and move the camera
LatLng cali = new LatLng(3.451234, -76.532694);
mMap.moveCamera(CameraUpdateFactory.newLatLng(cali));
mMap.animateCamera(CameraUpdateFactory.zoomTo(15));
}

@Override
public void onLocationChanged(Location location) {

Double lat = location.getAltitude();
Double lng = location.getLongitude();

}



@Override
protected void onPause(){
super.onPause();
locationManager.removeUpdates(this);
}




}


As i told up the problem here are in the lines:

locationManager.requestLocationUpdates(provider,400,1,this);


And

locationManager.removeUpdates(this);


BTW if i remove those 2 lines and just work on showing a map and markers, the code works with no problem.

Answer

Change ,following line

import com.google.android.gms.location.LocationListener;

With

import android.location.LocationListener ;