android第一行代码笔记16-位置

2020-07-03  本文已影响0人  刚刚8888
// 获取 LocationManager 的实例
LocationManager locationManager = (LocationManager)
    getSystemService(Context.LOCATION_SERVICE);

// 获取位置
String provider = LocationManager.NETWORK_PROVIDER;
    Location location = locationManager.getLastKnownLocation(provider);


// 实时位置
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10, new LocationListener() {
@Override
            public void onStatusChanged(String provider, int status, Bundle
    extras) {
}
            @Override
            public void onProviderEnabled(String provider) {
            }
            @Override
            public void onProviderDisabled(String provider) {
            }
            @Override
            public void onLocationChanged(Location location) {
            }
});

// 这里 requestLocationUpdates()方法接收四个参数,第一个参数是位置提供器的类型,第 二个参数是监听位置变化的时间间隔,以毫秒为单位,第三个参数是监听位置变化的距离间 隔,以米为单位,第四个参数则是 LocationListener 监听器。这样的话,LocationManager 每隔 5 秒钟会检测一下位置的变化情况,当移动距离超过 10 米的时候,就会调用 LocationListener 的 onLocationChanged()方法,并把新的位置信息作为参数传入。


//完整代码
public class MainActivity extends Activity {
        private TextView positionTextView;
        private LocationManager locationManager;
        private String provider;
@Override
    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
positionTextView = (TextView) findViewById(R.id.position_text_view); locationManager = (LocationManager) getSystemService(Context.
LOCATION_SERVICE);
// 获取所有可用的位置提供器
        List<String> providerList = locationManager.getProviders(true);
        if (providerList.contains(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
} else if (providerList.contains(LocationManager.NETWORK_PROVIDER)) {
            provider = LocationManager.NETWORK_PROVIDER;
        } else {
// 当没有可用的位置提供器时,弹出Toast提示用户
            Toast.makeText(this, "No location provider to use",
Toast.LENGTH_SHORT).show();
return; }
Location location = locationManager.getLastKnownLocation(provider); if (location != null) {
// 显示当前设备的位置信息
            showLocation(location);
        }
        locationManager.requestLocationUpdates(provider, 5000, 1,
locationListener);
}
    protected void onDestroy() {
        super.onDestroy();
        if (locationManager != null) {
// 关闭程序时将监听器移除
            locationManager.removeUpdates(locationListener);
        }
    }
    LocationListener locationListener = new LocationListener() {
@Override
            public void onStatusChanged(String provider, int status, Bundle
    extras) {
}
            @Override
            public void onProviderEnabled(String provider) {
            }
            @Override
            public void onProviderDisabled(String provider) {
            }
            @Override
            public void onLocationChanged(Location location) {
// 更新当前设备的位置信息
                showLocation(location);
            }
};
private void showLocation(Location location) {
String currentPosition = "latitude is " + location.getLatitude() + "\n"
    + "longitude is " + location.getLongitude();
            positionTextView.setText(currentPosition);
} }


...

  <manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.locationtest"
        android:versionCode="1"
        android:versionName="1.0" >
......
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
......
</manifest>
上一篇下一篇

猜你喜欢

热点阅读