zoukankan      html  css  js  c++  java
  • 定位LocationManager

    定位类型:
    1 gps 卫星定位 1米之内 agps 5米之内 手机上面不能遮盖物。
    2 wifi 定位 ip号 网段
    3 基站定位 500 -1000米
    network :wifi 基站
    gps :gps agps
    passvie: 无源定位 (综合所有的定位方式,得出最准确的结果)


    LocationManager
    要得到最好的定位方式,需要构建一组查询条件

     1         // 查询条件
     2         Criteria criteria = new Criteria();
     3         criteria.setAccuracy(Criteria.ACCURACY_FINE); // 定位的精准度
     4         criteria.setAltitudeRequired(false);          // 海拔信息是否关注
     5         criteria.setBearingRequired(false); // 对周围的事物是否关心
     6         criteria.setCostAllowed(true);  // 是否支持收费查询
     7         criteria.setPowerRequirement(Criteria.POWER_LOW); // 是否耗电
     8         criteria.setSpeedRequired(false); // 对速度是否关注
     9         
    10         // 获取最好的定位方式
    11         String provider = locationManager.getBestProvider(criteria, true); // true 代表从打开的设备中查找

    获取手机的经度纬度位置demo:

      1 package com.android.hzy.mobilesafe.engine;
      2 
      3 import android.content.Context;
      4 import android.content.SharedPreferences;
      5 import android.content.SharedPreferences.Editor;
      6 import android.location.Criteria;
      7 import android.location.Location;
      8 import android.location.LocationListener;
      9 import android.location.LocationManager;
     10 import android.os.Bundle;
     11 
     12 /**
     13  * 该类只能有一个实例  单例 
     14  * 如果打开多个定位耗电  而且同时只能有一个在使用
     15  * @author Administrator
     16  *
     17  */
     18 public class GPSInfoService {
     19 
     20     private static GPSInfoService mInstance;
     21     private LocationManager locationManager;// 定位服务
     22     private MyLocationListener listener;
     23     private SharedPreferences sp;
     24     
     25     private GPSInfoService(Context context) {
     26         // TODO Auto-generated constructor stub
     27         locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
     28         sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
     29     }
     30     
     31     public static GPSInfoService getInstance(Context context){
     32         if(mInstance == null){
     33             mInstance = new GPSInfoService(context);
     34         }
     35         return mInstance;
     36     }
     37     
     38     /************注册定位监听**************************/
     39     public void registenerLocationChangeListener(){
     40         // 得到所有的定位
     41 //        List<String> providers = locationManager.getAllProviders();
     42 //        for (String provider : providers) {
     43 //            Log.i("i", provider);
     44 //        }
     45         
     46         // 查询条件
     47         Criteria criteria = new Criteria();
     48         criteria.setAccuracy(Criteria.ACCURACY_FINE); // 定位的精准度
     49         criteria.setAltitudeRequired(false);          // 海拔信息是否关注
     50         criteria.setBearingRequired(false); // 对周围的事物是否关心
     51         criteria.setCostAllowed(true);  // 是否支持收费查询
     52         criteria.setPowerRequirement(Criteria.POWER_LOW); // 是否耗电
     53         criteria.setSpeedRequired(false); // 对速度是否关注
     54         
     55         // 获取最好的定位方式
     56         String provider = locationManager.getBestProvider(criteria, true); // true 代表从打开的设备中查找
     57         
     58         // 注册监听
     59         /**
     60          * provider:定位方式
     61          * minTime:定位时间   最少不能小于60000ms  (定位需要时间)
     62          * minDistance:最小距离位置更新  0代表不更新   按定位时间更新
     63          * 
     64          */
     65         locationManager.requestLocationUpdates(provider, 0, 0, getListener());
     66     }
     67     
     68 
     69     
     70     /*****通过单例来得到 定位的监听器*****************/
     71     private MyLocationListener getListener(){
     72         if(listener == null){
     73             listener = new MyLocationListener();
     74         }
     75         return listener;
     76     }
     77     
     78     /******从SharedPreferences 返回上一个获取地理位置的值************/
     79     public String getLastLocation(){
     80         
     81         return sp.getString("last_location", "");
     82     }
     83     
     84     private final class MyLocationListener implements LocationListener{
     85 
     86         @Override
     87         /*************当手机发生位置改变***************/
     88         public void onLocationChanged(Location location) {
     89             // TODO Auto-generated method stub
     90             double latitude = location.getLatitude(); // 纬度
     91             double longitude = location.getLongitude(); // 经度
     92             
     93             // 将获取的值保存到首选项中
     94             Editor editor = sp.edit();
     95             editor.putString("last_location", "jingdu: " + longitude + "   weidu: " + latitude);
     96             editor.commit();
     97         }
     98 
     99         @Override
    100         /**********当手机的某个位置提供者的状态发生改变的时候调用***************/
    101         public void onStatusChanged(String provider, int status, Bundle extras) {
    102 
    103         }
    104 
    105         @Override
    106         /*******某个位置的提供者可用了************/
    107         public void onProviderEnabled(String provider) {
    108             
    109         }
    110 
    111         @Override
    112         /*******某个位置的提供者被禁用掉******************/
    113         public void onProviderDisabled(String provider) {
    114             
    115         }
    116     }
    117     
    118     /**********取消监听********************/
    119     public void unRegisterLocationChangeListener(){
    120         locationManager.removeUpdates(getListener());
    121     }
    122 }
  • 相关阅读:
    【Services】【Web】【tomcat】配置tomcat支持https传输
    【Services】【Web】【apr】安装apr
    【Services】【Web】【Nginx】静态下载页面的安装与配置
    【Linux】【Problems】在fedora 9上解决依赖问题
    【Java】【设计模式】单例设计模式
    【Linux】【Shell】【text】awk
    【Linux】【Shell】【Basic】字符串操作
    【Linux】【Shell】【Basic】数组
    Linux上常用插件的一些命令(十)
    常见HTTP状态码
  • 原文地址:https://www.cnblogs.com/androidez/p/2914261.html
Copyright © 2011-2022 走看看