如何获取 android 中的当前位置?

How to get current location in android?

我正在使用以下代码获取我的当前位置,没有 GPS,但我想在 toast 中显示坐标。我试过这段代码,但它显示的是我的包裹名称而不是坐标!

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

    // Define a listener that responds to location updates

    LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
          //makeUseOfNewLocation(location);
        }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}

    };

    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
    Toast.makeText(getBaseContext(), "location is:"+locationListener, Toast.LENGTH_LONG).show();
}

您的 onLocationChanged() 回调中只有位置。

// Acquire a reference to the system Location Manager
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

// Define a listener that responds to location updates
LocationListener locationListener = new LocationListener() {

     public void onLocationChanged(Location location) {

        // Called when a new location is found by the network location provider.
        Toast.makeText(getBaseContext(), "location is:"+location, Toast.LENGTH_LONG).show();
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {}

    public void onProviderEnabled(String provider) {}

    public void onProviderDisabled(String provider) {}
};

// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);

您正在尝试为 locationListener 干杯 object.You 必须创建一个 Location 位置;敬酒时对象和敬酒 location.getLatitude() 和 location.getLongitude() 方法。

您当前的代码会在注册 LocationListener 时敬酒,并在侦听器上调用 Object.toString(),这只是 returns 侦听器的 class 名称。

您想将 Toast.makeText(... 行移动到 onLocationChanged 函数内部,并将文本更改为 "location is:"+location,或者 "location is: "+location.getLatitude()+","+location.getLongitude().

此外,每次他获得新位置时,此代码将继续制作 Toasts。您可能想使用 LocationManager.requestSingleUpdate 只做一个祝酒词,或者在 onDestroy 中放置一个 LocationManager.removeUpdates 以便它在 activity 完成时停止。

先创建 FMLocationListener class。

 private static class FMLocationListener implements LocationListener {

                public void onLocationChanged(Location location) {

                }

                public void onProviderDisabled(String provider) {

                }

            public void onProviderEnabled(String provider) {

            }

            public void onStatusChanged(String provider, int status, Bundle extras) {

            }

        }

然后使用下面的代码