zoukankan      html  css  js  c++  java
  • android定位GPS定位 代码实现

    package com.lx.util;
     
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.location.Criteria;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.Bundle;
    /*
     * 要求是单态的 ,只允许存在一个实例.
     * 获取手机的gps信息
     */
    public class GPSInfoService {
        private Context context;
        private LocationManager manager;
        SharedPreferences sp ;
        //1. 私有化构造方法
        private  GPSInfoService(Context context){  
            this.context= context;
            manager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
            sp = context.getSharedPreferences("config", Context.MODE_PRIVATE);
        }
        private static GPSInfoService mGPSService;
         
        public synchronized static GPSInfoService getInstance(Context context){
            if(mGPSService==null)
                mGPSService = new GPSInfoService(context);
            return mGPSService;
        }
         
        public void registerLocationUpdates(){
            //当前你的手机 所支持的定位方式获取出来
            //有多种定位方式 gps network ,基站, passive
            //可以根据定位的条件 ,获取 一个最好的定位方式
            Criteria criteria = new Criteria();
            // 设置定位的精度
            criteria.setAccuracy(Criteria.ACCURACY_COARSE); //获取大体的位置
            criteria.setAltitudeRequired(false); // 海拔信息
            criteria.setCostAllowed(true); //允许产生费用
            criteria.setPowerRequirement(Criteria.POWER_LOW); //低功耗
             
            //获取一个最符合查询条件的位置提供者
            String provider  =manager.getBestProvider(criteria, true);
             
            // 注册 位置改变的监听器
            manager.requestLocationUpdates(provider, 60000, 0, getLinster());
             
        }
         
         
        public void cancleLocationUpdates(){
            manager.removeUpdates(getLinster());
             
        }
        private static MyGPSLinster myGPSLinser;
         
        private MyGPSLinster getLinster(){
            if(myGPSLinser==null)
                myGPSLinser = new MyGPSLinster();
            return myGPSLinser;
        }
         
        /**
         * 获取手机的最后一次位置
         * @return
         */
        public String getLastPosition(){
            return sp.getString("lastlocation", "");
        }
        private class MyGPSLinster implements LocationListener{
     
            // 用户位置改变的时候 的回调方法
            public void onLocationChanged(Location location) {
                // TODO Auto-generated method stub
                //location
                //获取到用户的纬度
                double latitude= location.getLatitude();
                double longitude = location.getLongitude();
                String locationstr = "jing du "+ longitude + " weidu  :"+latitude;
                Editor  editor =  sp.edit();
                editor.putString("lastlocation", locationstr);
                editor.commit();
            }
            // 状态改变
            public void onStatusChanged(String provider, int status, Bundle extras) {
                // TODO Auto-generated method stub
            }
            //gps ,打开
            public void onProviderEnabled(String provider) {
                // TODO Auto-generated method stub
                 
            }
            //关闭
            public void onProviderDisabled(String provider) {
                // TODO Auto-generated method stub
                 
            }
        }
    }
  • 相关阅读:
    仿msn风格的选项卡_网页代码站(www.webdm.cn)
    拍拍网头部导航菜单_网页代码站(www.webdm.cn)
    横向滑动特效的菜单(js+css)_网页代码站(www.webdm.cn)
    QuickMenu 超强悍菜单_网页代码站(www.webdm.cn)
    紫罗兰风格的导航条_网页代码站(www.webdm.cn)
    兼容性超强的六级网站导航主菜单_网页代码站(www.webdm.cn)
    用JavaScript实现横向滑出的多级竖向菜单_网页代码站(www.webdm.cn)
    一款溢出式侧边栏导航菜单_网页代码站(www.webdm.cn)
    css打造的又一款清新的横排下拉菜单_网页代码站(www.webdm.cn)
    [ZZ]byte[]到short、int、long的相互转换
  • 原文地址:https://www.cnblogs.com/china-soft/p/3440665.html
Copyright © 2011-2022 走看看