How to get Latitude and Longitude of the mobile device in android?(如何在android中获取移动设备的经纬度?)
问题描述
如何使用定位工具在android中获取移动设备的当前纬度和经度?
How do I get the current Latitude and Longitude of the mobile device in android using location tools?
推荐答案
使用 LocationManager
.
LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
double longitude = location.getLongitude();
double latitude = location.getLatitude();
对 getLastKnownLocation()
的调用不会阻塞 - 这意味着如果当前没有可用位置,它将返回 null
- 所以你可能想看看将 LocationListener
传递给 requestLocationUpdates()
方法,它将为您提供位置的异步更新.
The call to getLastKnownLocation()
doesn't block - which means it will return null
if no position is currently available - so you probably want to have a look at passing a LocationListener
to the requestLocationUpdates()
method instead, which will give you asynchronous updates of your location.
private final LocationListener locationListener = new LocationListener() {
public void onLocationChanged(Location location) {
longitude = location.getLongitude();
latitude = location.getLatitude();
}
}
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10, locationListener);
您需要为您的应用程序提供 ACCESS_FINE_LOCATION
权限 如果你想使用 GPS.
You'll need to give your application the ACCESS_FINE_LOCATION
permission if you want to use GPS.
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
您可能还想添加 ACCESS_COARSE_LOCATION
当 GPS 不可用时的权限 并使用 getBestProvider()
方法.
You may also want to add the ACCESS_COARSE_LOCATION
permission for when GPS isn't available and select your location provider with the getBestProvider()
method.
这篇关于如何在android中获取移动设备的经纬度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在android中获取移动设备的经纬度?


基础教程推荐
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01