如何使用定位工具,我得到了移动设备的当前经度和纬度的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()
不阻塞 - 这意味着它会返回空
如果没有位置目前可用 - 所以你可能想看看传递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.
上一篇:适合图像转换成ImageView的,保持宽高比,然后调整ImageView的图像尺寸是多少?图像、转换成、尺寸、适合
下一篇:Android的需要编译器遵从性级别5.0或6.0。找到“1.7”代替。请使用Android的工具&gt;修复项目属性请使用、编译器、属性、级别