标签:android blog http io ar os sp for java
一、LocationManager
LocationMangager,位置管理器。要想操作定位相关设备,必须先定义个LocationManager。
- LocationManger locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
二、LocationListener
LocationListener,位置监听,监听位置变化,监听设备开关与状态。
- LocationListener locationListener=new LocationListener(){
-
- public void onLocationChanged(Location location) {
- system.out.println("时间:"+location.getTime());
- system.out.println("经度:"+location.getLongitude());
- system.out.println("纬度:"+location.getLatitude());
- system.out.println("海拔:"+location.getAltitude());
- }
-
- public void onProviderDisabled(String provider) {
- system.out.println("当前GPS状态:禁用\n");
- }
-
- public void onProviderEnabled(String provider) {
- system.out.println("当前GPS状态:开启\n");
- }
-
- public void onStatusChanged(String provider, int status,Bundle extras) {
- if(status==LocationProvider.AVAILABLE){
- system.out.println("当前GPS状态:可见的\n");
- }else if(status==LocationProvider.OUT_OF_SERVICE){
- system.out.println("当前GPS状态:服务区外\n");
- }else if(status==LocationProvider.TEMPORARILY_UNAVAILABLE){
- system.out.println("当前GPS状态:暂停服务\n");
- }
- }
- };
- locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener);
三、Location
Location,位置信息,通过Location可以获取时间、经纬度、海拔等位置信息。上面采用locationListener里面的onLocationChanged()来获取location,下面讲述如何主动获取location。
- Location location=locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
- system.out.println("时间:"+location.getTime());
- system.out.println("经度:"+location.getLongitude());
注意:Location location=new Location(LocationManager.GPS_PROVIDER)方式获取的location的各个参数值都是为0。
四、GpsStatus.Listener
GpsStatus.Listener ,GPS状态监听,包括GPS启动、停止、第一次定位、卫星变化等事件。
- GpsStatus.Listener gpsStatusListener=new GpsStatus.Listener(){
- public void onGpsStatusChanged(int event) {
- if(event==GpsStatus.GPS_EVENT_FIRST_FIX){
-
- }else if(event==GpsStatus.GPS_EVENT_SATELLITE_STATUS){
-
- GpsStatus gpsStauts= locationManager.getGpsStatus(null);
- int maxSatellites = gpsStauts.getMaxSatellites();
- Iterator<GpsSatellite> it = gpsStatus.getSatellites().iterator();
- int count = 0;
- while (it.hasNext() && count <= maxSatellites) {
- count++;
- GpsStatellite s = it.next();
- }
- system.out.println("搜索到:"+count+"颗卫星");
- }else if(event==GpsStatus.GPS_EVENT_STARTED){
-
- }else if(event==GpsStatus.GPS_EVENT_STOPPED){
-
- }
- }
- };
- locationManager.addGpsStatusListener(gpsStatusListener);
五、GpsStatus
GpsStatus,GPS状态信息,上面在卫星状态变化时,我们就用到了GpsStatus。
- GpsStatus gpsStatus = locationManager.getGpsStatus(null);
- int maxSatellites = gpsStatus.getMaxSatellites();
- int costTime=gpsStatus.getTimeToFirstFix();
- Iterable<GpsSatellite> iterable=gpsStatus.getSatellites();
- Iterator<GpsSatellite> itrator=iterable.iterator();
六、GpsSatellite
GpsSatellite,定位卫星,包含卫星的方位、高度、伪随机噪声码、信噪比等信息。
- Iterable<GpsSatellite> iterable=gpsStatus.getSatellites();
- Iterator<GpsSatellite> itrator=iterable.iterator();
- ArrayList<GpsSatellite> satelliteList=new ArrayList<GpsSatellite>();
- int count=0;
- int maxSatellites=gpsStatus.getMaxSatellites();
- while (itrator.hasNext() && count <= maxSatellites) {
- count++;
- satellite = itrator.next();
- statelliteList.add(satellite);
- }
- System.out.println("总共搜索到"+count+"颗卫星");
- for(int i=0;i<satelliteList.size();i++){
-
- System.out.println(satelliteList.get(i).getAzimuth());
-
- System.out.println(satelliteList.get(i).getElevation());
-
- System.out.println(satelliteList.get(i).getPrn());
-
- System.out.println(satelliteList.get(i).getSnr());
-
- System.out.println(satelliteList.get(i).hasAlmanac());
-
- System.out.println(satelliteList.get(i).hasEphemeris());
-
- System.out.println(satelliteList.get(i).hasAlmanac());
- }
Android入门之GPS定位详解
标签:android blog http io ar os sp for java
原文地址:http://www.cnblogs.com/hudabing/p/4153839.html