热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

Android下GPRS定位

转载地址:http:www.cnblogs.comlinjiqinarchive201111012231598.html一、LocationManagerLocati

转载地址:http://www.cnblogs.com/linjiqin/archive/2011/11/01/2231598.html

一、LocationManager

LocationMangager,位置管理器。要想操作定位相关设备,必须先定义个LocationManager。我们可以通过如下代码创建LocationManger对象。


LocationManger locationManager=(LocationManager)this.getSystemService(Context.LOCATION_SERVICE);

         

二、LocationListener

LocationListener,位置监听,监听位置变化,监听设备开关与状态。


private LocationListener locationListener=new LocationListener() {/*** 位置信息变化时触发*/public void onLocationChanged(Location location) {updateView(location);Log.i(TAG, "时间:"+location.getTime()); Log.i(TAG, "经度:"+location.getLongitude()); Log.i(TAG, "纬度:"+location.getLatitude()); Log.i(TAG, "海拔:"+location.getAltitude()); }/*** GPS状态变化时触发*/public void onStatusChanged(String provider, int status, Bundle extras) {switch (status) {//GPS状态为可见时
case LocationProvider.AVAILABLE:Log.i(TAG, "当前GPS状态为可见状态");break;//GPS状态为服务区外时
case LocationProvider.OUT_OF_SERVICE:Log.i(TAG, "当前GPS状态为服务区外状态");break;//GPS状态为暂停服务时
case LocationProvider.TEMPORARILY_UNAVAILABLE:Log.i(TAG, "当前GPS状态为暂停服务状态");break;}}/*** GPS开启时触发*/public void onProviderEnabled(String provider) {Location location=lm.getLastKnownLocation(provider);updateView(location);}/*** GPS禁用时触发*/public void onProviderDisabled(String provider) {updateView(null);}};


      

三、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 listener = new GpsStatus.Listener() {public void onGpsStatusChanged(int event) {switch (event) {//第一次定位
case GpsStatus.GPS_EVENT_FIRST_FIX:Log.i(TAG, "第一次定位");break;//卫星状态改变
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:Log.i(TAG, "卫星状态改变");//获取当前状态
GpsStatus gpsStatus=lm.getGpsStatus(null);//获取卫星颗数的默认最大值
int maxSatellites = gpsStatus.getMaxSatellites();//创建一个迭代器保存所有卫星
Iterator iters &#61; gpsStatus.getSatellites().iterator();int count &#61; 0; while (iters.hasNext() && count <&#61; maxSatellites) { GpsSatellite s &#61; iters.next(); count&#43;&#43;; } System.out.println("搜索到&#xff1a;"&#43;count&#43;"颗卫星");break;//定位启动
case GpsStatus.GPS_EVENT_STARTED:Log.i(TAG, "定位启动");break;//定位结束
case GpsStatus.GPS_EVENT_STOPPED:Log.i(TAG, "定位结束");break;}};};
//绑定监听状态
lm.addGpsStatusListener(listener);


          

五、GpsStatus


GpsStatus&#xff0c;GPS状态信息&#xff0c;上面在卫星状态变化时&#xff0c;我们就用到了GpsStatus。//实例化
GpsStatus gpsStatus &#61; locationManager.getGpsStatus(null); // 获取当前状态
//获取默认最大卫星数
int maxSatellites &#61; gpsStatus.getMaxSatellites();
//获取第一次定位时间&#xff08;启动到第一次定位&#xff09;
int costTime&#61;gpsStatus.getTimeToFirstFix();
//获取卫星
Iterable iterable&#61;gpsStatus.getSatellites();
//一般再次转换成Iterator
Iterator itrator&#61;iterable.iterator();


             

六、GpsSatellite
    
GpsSatellite&#xff0c;定位卫星&#xff0c;包含卫星的方位、高度、伪随机噪声码、信噪比等信息。



//获取卫星
Iterable iterable&#61;gpsStatus.getSatellites();
//再次转换成Iterator
Iterator itrator&#61;iterable.iterator();
//通过遍历重新整理为ArrayList
ArrayList satelliteList&#61;new ArrayList();
int count&#61;0;
int maxSatellites&#61;gpsStatus.getMaxSatellites();
while (itrator.hasNext() && count <&#61; maxSatellites) { GpsSatellite satellite &#61; itrator.next(); satelliteList.add(satellite); count&#43;&#43;;
}
System.out.println("总共搜索到"&#43;count&#43;"颗卫星");
//输出卫星信息
for(int i&#61;0;i//卫星的方位角&#xff0c;浮点型数据
System.out.println(satelliteList.get(i).getAzimuth()); //卫星的高度&#xff0c;浮点型数据
System.out.println(satelliteList.get(i).getElevation()); //卫星的伪随机噪声码&#xff0c;整形数据
System.out.println(satelliteList.get(i).getPrn()); //卫星的信噪比&#xff0c;浮点型数据
System.out.println(satelliteList.get(i).getSnr()); //卫星是否有年历表&#xff0c;布尔型数据
System.out.println(satelliteList.get(i).hasAlmanac()); //卫星是否有星历表&#xff0c;布尔型数据
System.out.println(satelliteList.get(i).hasEphemeris()); //卫星是否被用于近期的GPS修正计算
System.out.println(satelliteList.get(i).hasAlmanac());
}


         

           

为了便于理解&#xff0c;接下来模拟一个案例&#xff0c;如何在程序代码中使用GPS获取位置信息。

第一步&#xff1a;新建一个Android工程项目&#xff0c;命名为mygps&#xff0c;目录结构如下

         

第二步&#xff1a;修改main.xml布局文件&#xff0c;修改内容如下&#xff1a;


xml version&#61;"1.0" encoding&#61;"utf-8"?>
<LinearLayout xmlns:android&#61;"http://schemas.android.com/apk/res/android"android:orientation&#61;"vertical"android:layout_width&#61;"fill_parent"android:layout_height&#61;"fill_parent"><EditText android:layout_width&#61;"fill_parent"android:layout_height&#61;"wrap_content"android:cursorVisible&#61;"false"android:editable&#61;"false"android:id&#61;"&#64;&#43;id/editText"/>
LinearLayout>


           

第三步&#xff1a;实用Adnroid平台的GPS设备&#xff0c;需要添加上权限


<uses-permission android:name&#61;"android.permission.ACCESS_FINE_LOCATION"/><uses-permission android:name&#61;"android.permission.ACCESS_COARSE_LOCATION"/>

          

第四步&#xff1a;修改核心组件activity&#xff0c;修改内容如下


package com.ljq.activity;import java.util.Iterator;import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.location.Criteria;
import android.location.GpsSatellite;
import android.location.GpsStatus;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.widget.EditText;
import android.widget.Toast;public class GpsActivity extends Activity {private EditText editText;private LocationManager lm;private static final String TAG&#61;"GpsActivity";

 &#64;Overrideprotected void onDestroy() {// TODO Auto-generated method stubsuper.onDestroy();lm.removeUpdates(locationListener);}

&#64;Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);editText&#61;(EditText)findViewById(R.id.editText);lm&#61;(LocationManager)getSystemService(Context.LOCATION_SERVICE);//判断GPS是否正常启动
if(!lm.isProviderEnabled(LocationManager.GPS_PROVIDER)){Toast.makeText(this, "请开启GPS导航...", Toast.LENGTH_SHORT).show();//返回开启GPS导航设置界面
Intent intent &#61; new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivityForResult(intent,0); return;}//为获取地理位置信息时设置查询条件
String bestProvider &#61; lm.getBestProvider(getCriteria(), true);//获取位置信息//如果不设置查询要求&#xff0c;getLastKnownLocation方法传人的参数为LocationManager.GPS_PROVIDER
Location location&#61; lm.getLastKnownLocation(bestProvider); updateView(location);//监听状态
lm.addGpsStatusListener(listener);//绑定监听&#xff0c;有4个参数 //参数1&#xff0c;设备&#xff1a;有GPS_PROVIDER和NETWORK_PROVIDER两种//参数2&#xff0c;位置信息更新周期&#xff0c;单位毫秒 //参数3&#xff0c;位置变化最小距离&#xff1a;当位置距离变化超过此值时&#xff0c;将更新位置信息 //参数4&#xff0c;监听 //备注&#xff1a;参数2和3&#xff0c;如果参数3不为0&#xff0c;则以参数3为准&#xff1b;参数3为0&#xff0c;则通过时间来定时更新&#xff1b;两者为0&#xff0c;则随时刷新 // 1秒更新一次&#xff0c;或最小位移变化超过1米更新一次&#xff1b;//注意&#xff1a;此处更新准确度非常低&#xff0c;推荐在service里面启动一个Thread&#xff0c;在run中sleep(10000);然后执行handler.sendMessage(),更新位置
lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 1, locationListener);}//位置监听
private LocationListener locationListener&#61;new LocationListener() {/*** 位置信息变化时触发*/public void onLocationChanged(Location location) {updateView(location);Log.i(TAG, "时间&#xff1a;"&#43;location.getTime()); Log.i(TAG, "经度&#xff1a;"&#43;location.getLongitude()); Log.i(TAG, "纬度&#xff1a;"&#43;location.getLatitude()); Log.i(TAG, "海拔&#xff1a;"&#43;location.getAltitude()); }/*** GPS状态变化时触发*/public void onStatusChanged(String provider, int status, Bundle extras) {switch (status) {//GPS状态为可见时
case LocationProvider.AVAILABLE:Log.i(TAG, "当前GPS状态为可见状态");break;//GPS状态为服务区外时
case LocationProvider.OUT_OF_SERVICE:Log.i(TAG, "当前GPS状态为服务区外状态");break;//GPS状态为暂停服务时
case LocationProvider.TEMPORARILY_UNAVAILABLE:Log.i(TAG, "当前GPS状态为暂停服务状态");break;}}/*** GPS开启时触发*/public void onProviderEnabled(String provider) {Location location&#61;lm.getLastKnownLocation(provider);updateView(location);}/*** GPS禁用时触发*/public void onProviderDisabled(String provider) {updateView(null);}};//状态监听
GpsStatus.Listener listener &#61; new GpsStatus.Listener() {public void onGpsStatusChanged(int event) {switch (event) {//第一次定位
case GpsStatus.GPS_EVENT_FIRST_FIX:Log.i(TAG, "第一次定位");break;//卫星状态改变
case GpsStatus.GPS_EVENT_SATELLITE_STATUS:Log.i(TAG, "卫星状态改变");//获取当前状态
GpsStatus gpsStatus&#61;lm.getGpsStatus(null);//获取卫星颗数的默认最大值
int maxSatellites &#61; gpsStatus.getMaxSatellites();//创建一个迭代器保存所有卫星
Iterator iters &#61; gpsStatus.getSatellites().iterator();int count &#61; 0; while (iters.hasNext() && count <&#61; maxSatellites) { GpsSatellite s &#61; iters.next(); count&#43;&#43;; } System.out.println("搜索到&#xff1a;"&#43;count&#43;"颗卫星");break;//定位启动
case GpsStatus.GPS_EVENT_STARTED:Log.i(TAG, "定位启动");break;//定位结束
case GpsStatus.GPS_EVENT_STOPPED:Log.i(TAG, "定位结束");break;}};};/*** 实时更新文本内容* * &#64;param location*/private void updateView(Location location){if(location!&#61;null){editText.setText("设备位置信息\n\n经度&#xff1a;");editText.append(String.valueOf(location.getLongitude()));editText.append("\n纬度&#xff1a;");editText.append(String.valueOf(location.getLatitude()));}else{//清空EditText对象
editText.getEditableText().clear();}}/*** 返回查询条件* &#64;return*/private Criteria getCriteria(){Criteria criteria&#61;new Criteria();//设置定位精确度 Criteria.ACCURACY_COARSE比较粗略&#xff0c;Criteria.ACCURACY_FINE则比较精细
criteria.setAccuracy(Criteria.ACCURACY_FINE); //设置是否要求速度
criteria.setSpeedRequired(false);// 设置是否允许运营商收费
criteria.setCostAllowed(false);//设置是否需要方位信息
criteria.setBearingRequired(false);//设置是否需要海拔信息
criteria.setAltitudeRequired(false);// 设置对电源的需求
criteria.setPowerRequirement(Criteria.POWER_LOW);return criteria;}
}


       

第五步&#xff1a;启动模拟器&#xff0c;如果启动完成&#xff0c;请在Myeclipse中按如下选项一步一步操作&#xff1a;Emulator Control->Location Controls->Manual->选中Decimal->输入经纬度&#xff0c;类似如下

      

演示效果如下&#xff1a;


推荐阅读
author-avatar
nzl
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有