博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 获取LocationProvider以及获取定位信息
阅读量:7178 次
发布时间:2019-06-29

本文共 5255 字,大约阅读时间需要 17 分钟。

hot3.png

获取LocationProvider的三种方法

一、获取所有的LocationProvider并用TextView显示出来

//获取显示LocationProvider名称的TextView        providerTv = findViewById(R.id.act_provider_tv);        //获取位置服务        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);        //这个方法返回值是List集合,里面数据是String类型的        List
providerNames = locationManager.getAllProviders();//获取所有的LocationProvider名称 StringBuilder stringBuilder = new StringBuilder();//字符构建器 for ( Iterator
iterator = providerNames.iterator();iterator.hasNext();) { stringBuilder.append(iterator.next() + "\n"); } providerTv.setText(stringBuilder.toString());

二、通过名称获得LocationProvider

//获取显示LocationProvider名称的TextView        providerTv = findViewById(R.id.act_provider_tv);        //获取位置服务        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);        //获取基于GPS的LocationProvider        //需要加入权限  
LocationProvider provider = locationManager.getProvider(LocationManager.GPS_PROVIDER); providerTv.setText(provider.getName());

三、通过Criteria类获得LocationProvider

//获取显示LocationProvider名称的TextView        providerTv = findViewById(R.id.act_provider_tv);        //获取位置服务        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);        //获取最佳的LocationProvider        //创建一个过滤条件对象        //需要加入权限  
Criteria criteria = new Criteria(); //设置为不收费的 criteria.setCostAllowed(false); //使用精度最准确的 criteria.setAccuracy(Criteria.ACCURACY_FINE); //设置中等耗电量 criteria.setPowerRequirement(Criteria.POWER_MEDIUM); //获取最佳的LocationProvider名称 String provider = locationManager.getBestProvider(criteria, true); providerTv.setText(provider);

——————————————————————————————————————————————————————————

获取定位信息

  • 获取一个locationManager对象,主要通过getSystemService获取
  • 设置一个监听器,用来实现每隔一段时间获取定位信息
  • 编写一个locationUpdates方法,把获取到的定位信息进行输出
  • 获取locationManager对象的getLastKnownLocation方法获取最新的定位信息
  • 传到locationUpdates方法中
private TextView locationTv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                WindowManager.LayoutParams.FLAG_FULLSCREEN);     //设置全屏        //获取显示LocationProvider名称的TextView        locationTv = findViewById(R.id.act_title_tv);        //获取位置服务        LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);        //权限检查,编辑器自动添加        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;        }        assert locationManager != null;        locationManager.requestLocationUpdates(                LocationManager.GPS_PROVIDER,   //指定GPS定位的提供者                1000,                 //间隔时间                1,                 //位置间隔1米                new LocationListener() {//监听GPS定位信息是否改变                    @Override                    public void onLocationChanged(Location location) {                    //GPS信息发生改变时,回调                    }                    @Override                    public void onStatusChanged(String provider, int status, Bundle extras) {                        //GPS状态发生改变时,回调                    }                    @Override                    public void onProviderEnabled(String provider) {                    //定位提供者启动时回调                    }                    @Override                    public void onProviderDisabled(String provider) {                        //定位提供者关闭时回调                    }                }        );        //获取最新的定位信息        Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);        //将最新的定位信息传递给locationUpdates()方法        locationUpdates(location);//              需要添加两个位置权限//              近似精度的权限//            
// 更精细精度的访问权限//
} public void locationUpdates(Location location) { if (location != null) { //创建一个字符串构建器,用于记录位置信息 StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("您的位置是: \n"); stringBuilder.append("经度: "); stringBuilder.append(location.getLongitude()); stringBuilder.append("\n 纬度:"); stringBuilder.append(location.getLatitude()); locationTv.setText(stringBuilder.toString()); } else { locationTv.setText("没有获取到GPS信息"); } }

 

转载于:https://my.oschina.net/lanyu96/blog/2874571

你可能感兴趣的文章
MySQL按月/按周统计查询
查看>>
JavaScript之css操作总结
查看>>
Django1.10.x i18n国际化
查看>>
我的友情链接
查看>>
在ubuntu上安装LightTable IDE
查看>>
Dubbo 调研过程
查看>>
Vacl反写的原理
查看>>
HTTP协议入门知识
查看>>
关于利用Postfix邮件网关接收外网投递邮件失败问题的解决方法
查看>>
《史上最简单的 SpringCloud 教程》系列
查看>>
洛谷——P2239 螺旋矩阵
查看>>
vmware虚拟机无法加载U盘
查看>>
Selenium自动化测试环境搭建
查看>>
我的友情链接
查看>>
vlc+mfc,搭建简单的播放器
查看>>
Linux驱动编程--基于I2C子系统的I2C驱动
查看>>
编写iptables模块实现不连续IP地址的DNAT-POOL
查看>>
UVA 11374 Airport Express 最短路
查看>>
工作总结2010-2012-软件工程篇
查看>>
解决sqlplus历史命令和退格键问题
查看>>