欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现GPS定位代码实例

程序员文章站 2023-02-02 12:35:32
通过gps取得的是一个location类型的经纬度, 可以转换为两个double 纬度和经度. 纬度: 23.223871812820435 纬度: 113.58986...

通过gps取得的是一个location类型的经纬度, 可以转换为两个double 纬度和经度.
纬度: 23.223871812820435
纬度: 113.58986039161628
首先创建一个textview和两个button

<textview 
 android:id="@+id/text"
 android:layout_width="fill_parent" 
  android:layout_height="wrap_content"  />
 
 <button
  android:id="@+id/btnstart"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="定位" />
 <button
  android:id="@+id/btnstop"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:text="停止" />


然后添加主activity的代码
location 是存放经纬度的一个类型
locationmanager是位置管理服务类型

private button btnstart;
private button btnstop;
private textview textview;
private location mlocation;
private locationmanager mlocationmanager;
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate)
{
 
 super.oncreate(savedinstancestate);
 setcontentview(r.layout.main);
 
 btnstart = (button)findviewbyid(r.id.btnstart);
 btnstop = (button)findviewbyid(r.id.btnstop);
 textview = (textview)findviewbyid(r.id.text);
 btnstart.setonclicklistener(btnclicklistener); //开始定位
 btnstop.setonclicklistener(btnclicklistener); //结束定位按钮
}
gpsisopen是自己写的查看当前gps是否开启
getlocation 是自己写的一个获取定位信息的方法
mlocationmanager.removeupdates()是停止当前的gps位置监听
public button.onclicklistener btnclicklistener = new button.onclicklistener()
{
 public void onclick(view v)
 {
  button btn = (button)v;
  if(btn.getid() == r.id.btnstart)
  {
   if(!gpsisopen())
    return;
   
  mlocation = getlocation();
   
   if(mlocation != null)
    textview.settext("维度:" + mlocation.getlatitude() + "\n经度:" + mlocation.getlongitude());
   else
    textview.settext("获取不到数据");
  }
  else if(btn.getid() == r.id.btnstop)
  {
   mlocationmanager.removeupdates(locationlistener);
  }

 }
};
private boolean gpsisopen()
{
 boolean bret = true;

 locationmanager alm = (locationmanager)this.getsystemservice(context.location_service);
 if(!alm.isproviderenabled(locationmanager.gps_provider))
 {
  toast.maketext(this, "未开启gps", toast.length_short).show();
  bret = false;
 }
 else 
 {
  toast.maketext(this, "gps已开启", toast.length_short).show();
 }

 return bret;
}
判断当前是否开启gps
private boolean gpsisopen()
{
 boolean bret = true;
 
 locationmanager alm = (locationmanager)this.getsystemservice(context.location_service);
 if(!alm.isproviderenabled(locationmanager.gps_provider))
 {
  toast.maketext(this, "未开启gps", toast.length_short).show();
  bret = false;
 }
 else 
 {
  toast.maketext(this, "gps已开启", toast.length_short).show();
 }

 return bret;
}
该方法获取当前的经纬度, 第一次获取总是null
后面从locationlistener获取已改变的位置
mlocationmanager.requestlocationupdates()是开启一个locationlistener等待位置变化
private location getlocation()
{
 //获取位置管理服务
 mlocationmanager = (locationmanager)this.getsystemservice(context.location_service);

 //查找服务信息
 criteria criteria = new criteria();
 criteria.setaccuracy(criteria.accuracy_fine); //定位精度: 最高
 criteria.setaltituderequired(false); //海拔信息:不需要
 criteria.setbearingrequired(false); //方位信息: 不需要
 criteria.setcostallowed(true);  //是否允许付费
 criteria.setpowerrequirement(criteria.power_low); //耗电量: 低功耗
 
 string provider = mlocationmanager.getbestprovider(criteria, true); //获取gps信息

 location location = mlocationmanager.getlastknownlocation(provider);
 
 mlocationmanager.requestlocationupdates(provider, 2000, 5, locationlistener);
 
 return location;
}
改方法是等待gps位置改变后得到新的经纬度
private final locationlistener locationlistener = new locationlistener()
{
 public void onlocationchanged(location location)
 {
  // todo auto-generated method stub
  if(location != null)
   textview.settext("维度:" + location.getlatitude() + "\n经度:" 
      + location.getlongitude());
  else
   textview.settext("获取不到数据" + integer.tostring(ncount));
 }

 public void onproviderdisabled(string provider)
 {
  // todo auto-generated method stub
 }

 public void onproviderenabled(string provider)
 {
  // todo auto-generated method stub
 }

 public void onstatuschanged(string provider, int status, bundle extras)
 {
  // todo auto-generated method stub
  
 }
};