使用
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如果当前没有可用的位置,它将返回-因此,你可能希望查看将
a传递
LocationListener给该
requestLocationUpdates()方法,这将为你提供位置的异步更新。
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);如果要使用GPS,则需要向你的应用程序
ACCESS_FINE_LOCATION授予权限。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
你可能还想添加GPS不可用时的
ACCESS_COARSE_LOCATION权限,然后使用
getBestProvider()方法选择位置提供商。



