How to get GPS location?

I try to get the location, but keep getting errors. Can any of you guys see where I'm wrong? One mistake is at least "the method does not override or does not implement the method from the supertype." - Many thanks to the guys

MAIN

@Override

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //get Your Current Location
    LocationManager locationManager=    (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    MyCurrentLoctionListener locationListener = new MyCurrentLoctionListener();
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) locationListener);

}

Class MyCurrentLoctionListener

public class MyCurrentLoctionListener implements LocationListener {

    @Override
    public void onLocationChanged(Location location) {
        location.getLatitude();
        location.getLongitude();

        String myLocation = "Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude();

        //I make a log to see the results
        Log.e("MY CURRENT LOCATION", myLocation);

    }

    @Override
    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    @Override
    public void onProviderEnabled(String s) {

    }

    @Override
    public void onProviderDisabled(String s) {

    }
}

In android manifest:

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+4
source share
2 answers

You do not need @Override methods that you are currently overriding. They are already abstract, therefore, realizing the class, it assumes that you are. Take a look at the Android tutorial for location information.

public class MyCurrentLoctionListener implements LocationListener {

    public void onLocationChanged(Location location) {
        location.getLatitude();
        location.getLongitude();

        String myLocation = "Latitude = " + location.getLatitude() + " Longitude = " + location.getLongitude();

        //I make a log to see the results
        Log.e("MY CURRENT LOCATION", myLocation);

    }

    public void onStatusChanged(String s, int i, Bundle bundle) {

    }

    public void onProviderEnabled(String s) {

    }

    public void onProviderDisabled(String s) {

    }
}
+3
source
LocationManager locationManager=    (LocationManager)getSystemService(Context.LOCATION_SERVICE);
MyCurrentLoctionListener locationListener = new MyCurrentLoctionListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);

and change this line

public class MyCurrentLoctionListener implements android.location.LocationListener {
+2

All Articles