//3.获取上次的位置,一般第一次运行,此值为null
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
Location location = locationManager.getLastKnownLocation(locationProvider);
if (location != null) {
showLocation(location);
} else {
// 监视地理位置变化,第二个和第三个参数分别为更新的最短时间minTime和最短距离minDistace
locationManager.requestLocationUpdates(locationProvider, MIN_TIME, MIN_DISTANCE, mListener);
}
}
LocationListener mListener = 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);
}
Purpose:通过GPS或NetWork来获取当前移动终端设备的经纬度功能
前提:设备需要带有GPS模块或NetWork模块;
}
LocationListener mListener = new LocationListener() { @Override public void onStatusChanged(String provider, int status, Bundle extras) { }
};
/**