如何通过单击 Android 中的按钮找到我当前的位置(纬度和经度)?
我看过各种代码片段,它们从各种方法(如 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Yogsma 的答案解决了如何接收自动更新。他引用的链接提供了您需要的所有内容,但这里是如何进行手动更新的摘要版本:
假设您已经阅读了有关如何制作按钮的教程,那么您只需为按钮添加一个侦听器,然后然后让侦听器调用一个函数来查询您的位置管理器。下面的代码全部内联,向您展示如何操作,但我会在其他地方(例如您的活动)实例化 LocationManager,并且我会为单击侦听器创建一个单独的方法来调用以执行更新。
当然,您还需要在清单 xml 文件中使用位置管理器服务注册您的活动:
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.
Of course you will also need to register your activity with the location manager service in your manifest xml file:
阅读此内容了解详细信息 http://www.javacodegeeks.com/ 2010/09/android-location-based-services.html
Read this for detail http://www.javacodegeeks.com/2010/09/android-location-based-services.html
没有这样的机制可以根据需要立即获取当前位置。由于您需要查询网络或 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.