如何通过单击 Android 中的按钮找到我当前的位置(纬度和经度)?

发布于 2024-11-02 21:49:55 字数 117 浏览 4 评论 0原文

我看过各种代码片段,它们从各种方法(如 onlocationupdate() 或 onstatusChanged())中找出坐标......我想要的是一个简单的代码,只需单击按钮即可获取我当前的 GPS 坐标......

I have seen various code snippets which find outs co-ordinates from various methods like onlocationupdate() or onstatusChanged().... what i want is a simple code that fetches my current GPS co-ordinates on click of a button...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

情未る 2024-11-09 21:49:55

Yogsma 的答案解决了如何接收自动更新。他引用的链接提供了您需要的所有内容,但这里是如何进行手动更新的摘要版本:

假设您已经阅读了有关如何制作按钮的教程,那么您只需为按钮添加一个侦听器,然后然后让侦听器调用一个函数来查询您的位置管理器。下面的代码全部内联,向您展示如何操作,但我会在其他地方(例如您的活动)实例化 LocationManager,并且我会为单击侦听器创建一个单独的方法来调用以执行更新。

// getLocationButton is the name of your button.  Not the best name, I know.
getLocationButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // instantiate the location manager, note you will need to request permissions in your manifest
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // get the last know location from your location manager.
        Location location= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        // now get the lat/lon from the location and do something with it.
        nowDoSomethingWith(location.getLatitude(), location.getLongitude());
    }
});

当然,您还需要在清单 xml 文件中使用位置管理器服务注册您的活动:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Yogsma's answer addresses how to receive automatic updates. The link he references provides all you need, but here is the summarized version of how to do a manual update:

Assuming you've read the tutorials on how to make a button, then you simply need to add a listener for your button, and then have the listener call a function to query your location manager. The code below does it all inline to show you how, but I'd instantiate LocationManager somewhere else (eg your activity) and I'd create a separate method for the on click listener to call to perform the update.

// getLocationButton is the name of your button.  Not the best name, I know.
getLocationButton.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        // instantiate the location manager, note you will need to request permissions in your manifest
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // get the last know location from your location manager.
        Location location= locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
        // now get the lat/lon from the location and do something with it.
        nowDoSomethingWith(location.getLatitude(), location.getLongitude());
    }
});

Of course you will also need to register your activity with the location manager service in your manifest xml file:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
四叶草在未来唯美盛开 2024-11-09 21:49:55
LocationManager mLocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                LocationListener mLocListener = new MyLocationListener();
                mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocListener);

public class MyLocationListener implements LocationListener{        

        public void onLocationChanged(Location loc) {           
            String message = String.format(
                        "New Location \n Longitude: %1$s \n Latitude: %2$s",
                        loc.getLongitude(), loc.getLatitude()
                );
                Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
        }
        public void onProviderDisabled(String arg0) {

        }
        public void onProviderEnabled(String provider) {

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

        }       
    }

阅读此内容了解详细信息 http://www.javacodegeeks.com/ 2010/09/android-location-based-services.html

LocationManager mLocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
                LocationListener mLocListener = new MyLocationListener();
                mLocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mLocListener);

public class MyLocationListener implements LocationListener{        

        public void onLocationChanged(Location loc) {           
            String message = String.format(
                        "New Location \n Longitude: %1$s \n Latitude: %2$s",
                        loc.getLongitude(), loc.getLatitude()
                );
                Toast.makeText(LbsGeocodingActivity.this, message, Toast.LENGTH_LONG).show();
        }
        public void onProviderDisabled(String arg0) {

        }
        public void onProviderEnabled(String provider) {

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

        }       
    }

Read this for detail http://www.javacodegeeks.com/2010/09/android-location-based-services.html

悟红尘 2024-11-09 21:49:55

没有这样的机制可以根据需要立即获取当前位置。由于您需要查询网络或 GPS 提供商,这可能需要一些时间才能实际获取位置。

一种方法是使用立即返回的 getLastKnownLocation。然而这个位置可能已经过时了。另一种方法是注册 PASSIVE_PROVIDER,通过它您可以获得位置修复,并希望其他应用程序已请求位置。

There is no such mechanism to get the current location immediately on demand. Since you need to query the Netowork or the GPS provider which may take time to actually get the location.

One way is to use the getLastKnownLocation which returns immediately. However this location may be stale. Another way is to register for a PASSIVE_PROVIDER by which you get a location fix with the hope that some other application has requested for location.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文