如何获得GPS位置?

时间:2014-07-30 23:51:06

标签: android android-studio

我正在尝试获取位置但不断出错。你们中的任何人都能看到我出错的地方吗?其中一个错误至少是"方法不会覆盖或实现超类型的方法" - 非常感谢你们

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类

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

2 个答案:

答案 0 :(得分:3)

您不需要@Override您当前覆盖的方法。它们已经抽象为void,因此通过实现该类,它假定您是。请查看Android tutorial以获取位置信息。

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

    }
}

答案 1 :(得分: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 {