Android 如何获取 GPS 位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25048834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 09:02:44  来源:igfitidea点击:

How to get GPS location?

androidandroid-studio

提问by user3864894

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

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

回答by Codeman

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 tutorialfor obtaining location information.

您不需要 @Override 当前覆盖的方法。它们已经是抽象的 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) {

    }
}

回答by geekCode

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 {