如何在Android中获取移动设备的纬度和经度?

如何在Android中获取移动设备的纬度和经度?

如何使用定位工具获取Android中移动设备的当前纬度和经度?


汪汪一只猫
浏览 241回答 3
3回答

阿晨1998

使用LocationManager.LocationManager&nbsp;lm&nbsp;=&nbsp;(LocationManager)getSystemService(Context.LOCATION_SERVICE); &nbsp;Location&nbsp;location&nbsp;=&nbsp;lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); &nbsp;double&nbsp;longitude&nbsp;=&nbsp;location.getLongitude();double&nbsp;latitude&nbsp;=&nbsp;location.getLatitude();打电话给getLastKnownLocation()不会阻塞-这意味着它会返回null如果当前没有可用的职位,那么您可能希望查看一下LocationListener到requestLocationUpdates()方法相反,它将为您提供位置的异步更新。private&nbsp;final&nbsp;LocationListener&nbsp;locationListener&nbsp;=&nbsp;new&nbsp;LocationListener()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;void&nbsp;onLocationChanged(Location&nbsp;location)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;longitude&nbsp;=&nbsp;location.getLongitude(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;latitude&nbsp;=&nbsp;location.getLatitude(); &nbsp;&nbsp;&nbsp;&nbsp;}}lm.requestLocationUpdates(LocationManager.GPS_PROVIDER,&nbsp;2000,&nbsp;10,&nbsp;locationListener);您需要给您的应用程序ACCESS_FINE_LOCATION许可如果你想用GPS。<uses-permission&nbsp;android:name="android.permission.ACCESS_FINE_LOCATION"&nbsp;/>您还可以添加ACCESS_COARSE_LOCATION许可当GPS不可用时,请使用getBestProvider()方法.
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java