简体   繁体   中英

How to get GPS location?

I am trying to get the location but keep getting errors. Are any of you guys able to see where I'm going wrong? One of the errors is at least "method does not override or implement a method from supertype"- Thanks so much 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);

}

MyCurrentLoctionListener class

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" />

You don't need to @Override the methods you are currently overriding. They're already abstract void, so by implementing the class, it assumes you are. Take a look at the Android tutorial for obtaining 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) {

    }
}
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 {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM