iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >Android编程实现GPS位置获取的方法
  • 944
分享到

Android编程实现GPS位置获取的方法

androidgpsroi 2023-05-30 23:05:22 944人浏览 安东尼
摘要

本文实例讲述了Android编程实现GPS位置获取的方法。分享给大家供大家参考,具体如下:public class GPSInfoService { private static GPSInfoService mInstance; pr

本文实例讲述了Android编程实现GPS位置获取的方法。分享给大家供大家参考,具体如下:

public class GPSInfoService {  private static GPSInfoService mInstance;  private LocationManager locationManager;//定位服务  private GPSInfoService(Context context) {    // TODO Auto-generated constructor stub    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);  }  public static GPSInfoService getInstance(Context context){    if(mInstance == null){      mInstance = new GPSInfoService(context);    }    return mInstance;  }  //注册定位监听  public void reGIStenerLocationChangeListener(){    //得到所有的定位服务//   List<String> providers = locationManager.getAllProviders();//   for(String provider:providers){//     Log.i("i", provider);//   }    //查询条件    Criteria criteria = new Criteria();    //定位的精准度    criteria.setAccuracy(Criteria.ACCURACY_FINE);    //海拔信息是否关注    criteria.setAltitudeRequired(false);    //对周围的事情是否进行关心    criteria.setBearingRequired(false);    //是否支持收费的查询    criteria.setCostAllowed(true);    //是否耗电    criteria.setPowerRequirement(Criteria.POWER_LOW);    //对速度是否关注    criteria.setSpeedRequired(false);    //得到最好的定位方式    String provider = locationManager.getBestProvider(criteria, true);    //注册监听    locationManager.requestLocationUpdates(provider, 60000, 0, getListener());  }  //取消监听  public void unRegisterLocationChangeListener(){    locationManager.removeUpdates(getListener());  }  private MyLocationListener listener;  //得到定位的监听器  private MyLocationListener getListener(){    if(listener == null){      listener = new MyLocationListener();    }    return listener;  }  //得到上个地理位置  public String getLastLocation(){    return sp.getString("last_location", "");  }  private final class MyLocationListener implements LocationListener{    //位置的改变    public void onLocationChanged(Location location) {      // TODO Auto-generated method stub      double latitude = location.getLatitude();//维度      double longitude = location.getLongitude();//经度      String last_location = "jingdu: " + longitude + ",weidu:" + latitude;      Editor editor = sp.edit();      editor.putString("last_location", last_location);      editor.commit();    }    //gps卫星有一个没有找到    public void onStatusChanged(String provider, int status, Bundle extras) {      // TODO Auto-generated method stub    }    //某个设置被打开    public void onProviderEnabled(String provider) {      // TODO Auto-generated method stub    }    //某个设置被关闭    public void onProviderDisabled(String provider) {      // TODO Auto-generated method stub    }  }}

--结束END--

本文标题: Android编程实现GPS位置获取的方法

本文链接: https://www.lsjlt.com/news/222218.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作