Android开发使⽤LocationManager实现定位服务 智能飞行器技术做项⽬需要获取经纬度信息,学习了下android⾃带的定位API,简单实现了⼀下,这⾥记录⼀下。废话不多说,先上代码: private String locationStr = "";
private String message = "";
private static final int REQUEST_CODE = 10;
private void getLocation() {
if (Build.VERSION.SDK_INT >= 23) {// android6 执⾏运⾏时权限
if (ActivityCompat.checkSelfPermission(this,Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSel // TODO: Consider calling
// Activity#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 Activity#requestPermissions for more details.
萌发菌
}
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_COARSE);//低精度,如果设置为⾼精度,依然获取不了location。
室外1V2
criteria.setAltitudeRequired(false);//不要求海拔
criteria.setBearingRequired(false);//不要求⽅位
criteria.setCostAllowed(true);//允许有花费
criteria.setPowerRequirement(Criteria.POWER_LOW);//低功耗
//获取LocationManager
LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// 获取最好的定位⽅式
String provider = BestProvider(criteria, true); // true 代表从打开的设备中查
List<String> providerList = Providers(true);
if (ains(LocationManager.NETWORK_PROVIDER)) {
provider = LocationManager.NETWORK_PROVIDER;
} else if (ains(LocationManager.GPS_PROVIDER)) {
provider = LocationManager.GPS_PROVIDER;
} else {
// 当没有可⽤的位置提供器时,弹出Toast提⽰⽤户
Toast.makeText(this, "Please Open Your GPS or Location Service", Toast.LENGTH_SHORT).show();
return;
}
医用手腕带LocationListener locationListener = new LocationListener(){
@Override
public void onLocationChanged(Location location) {
//经度
double longitude = Longitude();
//纬度
double latitude = Latitude();
//海拔
double altitude = Altitude();
locationStr = longitude+"_"+latitude;
launcher.callExternalInterface("getLocationSuccess", locationStr);
}
/
/当GPS状态发⽣改变的时候调⽤民宿管理系统
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
switch (status) {
case LocationProvider.AVAILABLE:
message = "当前GPS为可⽤状态!";