共计 1076 个字符,预计需要花费 3 分钟才能阅读完成。
-
让程序提供位置服务 -> 如何获取 LocationManager?
通过调用 android.app.Activity.getSystemService() 函数
String serviceString = Context.LOCATION_SERVICE; LocationManager locationManager =(LocationManager)getSystemService(serviceString);
-
获取 LocationManager 后指定其定位办法
String provider = LocationManager.GPS_PROVIDER; Location location = locationManager.getLastKnownLocation(provider);
-
调用 location 中的办法
double lat = location.getLatitude(); double lng = location.getLongitude();
-
监督地位变动
// 参数阐明://provider: 定位办法 //2000: 残剩地位扭转事件的工夫距离 //0: 间隔条件 //locationListener: 回调函数 locationManager.requestLocationUpdates(provider, 2000, 0, locationListener); //locationListener 的实现 private final LocationListener locationListener = new LocationListener(){ @Override public void onLocationChanged(Location location) {getLocationInfo(location); } @Override public void onProviderDisabled(String provider) {getLocationInfo(null); } @Override public void onProviderEnabled(String provider) {getLocationInfo(null); } @Override public void onStatusChanged(String provider, int status, Bundle extras) {}
-
最初要使 GPS 定位失效须要在 AndroidManifest.xml 退出用户许可
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
正文完