如何获取GPS位置?

4

我试图获取位置信息,但一直出现错误。你们当中有人能看到我的问题在哪里吗?其中一个错误至少是"方法不覆盖或实现超类型的方法"。非常感谢大家。

主要内容

@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);

}

我的当前位置监听器类

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

    }
}

在Android清单文件中:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

这个错误意味着你在一个不覆盖任何内容的函数上有一个 @Override 注释。通常这意味着你弄错了签名,打错了名称,或者它无法识别你正在实现或扩展的某些类型。 - Gabe Sechan
2个回答

3

您不需要覆盖当前正在覆盖的方法。它们已经是抽象void,因此通过实现类,它会认为您已经覆盖了这些方法。请参阅Android教程以获取位置信息。

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

    }
}

2
LocationManager locationManager=    (LocationManager)getSystemService(Context.LOCATION_SERVICE);
MyCurrentLoctionListener locationListener = new MyCurrentLoctionListener();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0,locationListener);

并更改此行

public class MyCurrentLoctionListener implements android.location.LocationListener {

1
好的,但是如果它在不同的类中,你如何获取实际的纬度/经度值呢?谢谢。 - FlorianB

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接