Android开发使用LocationManager实现定位服务

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为可⽤状态!";

本文发布于:2024-09-22 15:25:29,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/1/300184.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:位置   获取   改变   判断   实现   经纬度   信息
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议