用户定位(gps,network)

来源:互联网 发布:如何在excel筛选数据 编辑:程序博客网 时间:2024/05/22 16:05
       public void test(View v){LocationManager lm = (LocationManager)TitleListActivity.this.getSystemService(Context.LOCATION_SERVICE);//Criteria对象可以设置一系列查询条件,用于查询当前设备符合条件的LocationProviderCriteria criteria = new Criteria();criteria.setAccuracy(Criteria.ACCURACY_FINE);//高精度条件criteria.setPowerRequirement(Criteria.POWER_LOW);//电量消耗低条件criteria.setAltitudeRequired(false);//不需要海拔条件criteria.setSpeedRequired(false);//不需要速度条件criteria.setCostAllowed(false);//不予许产生费用条件String provider = lm.getBestProvider(criteria, true);Log.d("tab", provider);//lm.requestLocationUpdates(定位类型, 多少时间更新一次, 多少米更新, 所绑定的监听)TestLocationListener tll = new TestLocationListener();lm.requestLocationUpdates(provider, 5000, 500, tll);}public class TestLocationListener implements LocationListener{@Override//时间,地址满足其一执行该方法public void onLocationChanged(Location location) {// TODO Auto-generated method stubLog.d("tab", location.getLongitude()+"经度");Log.d("tab", location.getLatitude()+"纬度");Geocoder geocoder = new Geocoder(TitleListActivity.this,Locale.CHINA);try {List<Address> addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);Iterator<Address> it= addresses.iterator();while(it.hasNext()){Address address = (Address)it.next();//获取定位所在城市String city = address.getLocality();Log.d("tab", address+"");Log.d("tab", city+"");}} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}@Overridepublic void onStatusChanged(String provider, int status, Bundle extras) {// TODO Auto-generated method stub}@Overridepublic void onProviderEnabled(String provider) {// TODO Auto-generated method stub}@Overridepublic void onProviderDisabled(String provider) {// TODO Auto-generated method stub}}



定位方式的分类

1、GPS定位:

使用GPS卫星进行定位,需要加权限android.permission.ACCESS_FINE_LOCATION

2、NETWORK定位

使用信号接收塔和WIFI介入点进行定位,需要加权限

android.permission.ACCESS_FINE_LOCATION(精确定位)或

android.permission.ACCESS_COARSE_LOCATION(粗糙定位)


0 0
原创粉丝点击