简体   繁体   English

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

[英]How to get current location in android?

I am using the following code to get my current location, without GPS, but i want to show the co-ordinates in a toast.我正在使用以下代码获取我当前的位置,没有 GPS,但我想在祝酒词中显示坐标。 i have tried this code, but it shows my package name instead of co-ordinates!我试过这段代码,但它显示我的包名而不是坐标!

    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();
}

You only have the location in your onLocationChanged() callback.您的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);

You are trying to toast the locationListener object.You have to craete a Location location;您正在尝试敬酒 locationListener 对象。您必须创建一个 Location 位置; object and toast location.getLatitude() and location.getLongitude() method while toasting.烘烤时的对象和吐司 location.getLatitude() 和 location.getLongitude() 方法。

Your current code makes a toast when the LocationListener is registered, and calls Object.toString() on the listener, which just returns the class name of the listener.您当前的代码在注册 LocationListener 时祝酒,并在侦听器上调用 Object.toString() ,它只返回侦听器的类名。

You want to move the Toast.makeText(... line to inside the onLocationChanged function, and change the text to "location is:"+location , or maybe "location is: "+location.getLatitude()+","+location.getLongitude() .您想将 Toast.makeText(... 行移动到 onLocationChanged 函数内部,并将文本更改为"location is:"+location ,或者可能"location is: "+location.getLatitude()+","+location.getLongitude()

Also, this code will continue to make Toasts each time he gets a new location.此外,每次他获得新位置时,此代码将继续制作 Toasts。 You might want to either use LocationManager.requestSingleUpdate to only make a single toast, or put a LocationManager.removeUpdates in the onDestroy so that it stops when the activity is finished.您可能希望使用 LocationManager.requestSingleUpdate 仅制作一个 toast,或者将 LocationManager.removeUpdates 放在 onDestroy 中,以便在活动完成时停止。

create FMLocationListener class first.首先创建 FMLocationListener 类。

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

            }

        }

then use the following code然后使用以下代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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