首页 > 安全资讯 >

Android——通过GPS获取用户地理位置并监听位置变化

17-04-11

Android——通过GPS获取用户地理位置并监听位置变化,这个功能挺简单的,最近用到了就总结下。

Android——通过GPS获取用户地理位置并监听位置变化,这个功能挺简单的,最近用到了就总结下:

1、开启地理位置监听:

  /*
     * 开启地理位置监听
     */
    public void registerGpsListener(Context context) {
        Log.i(TAG, "registerGpsListener() start");

        mLocationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
        //第一次获得设备的位置
        mLastLocation = DeviceInfoUtils.getLocation(context);
        boolean tmp = false;

        //根据网络定位
        if (mLocationManager.getAllProviders().contains(LocationManager.NETWORK_PROVIDER)) {
            if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
                tmp = true;
                // 30秒更新一次,或最小位移变化超过100米更新一次;
                mLocationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 30 * 1000,
                        mMinDistance, mLocationListener);
            } else {
                Log.d(TAG, "provider LocationManager.NETWORK_PROVIDER is not enabled");
            }
        }
        //GPRS定位
        if (!tmp) {
            Log.d(TAG, "not provider LocationManager.NETWORK_PROVIDER.");
            if (mLocationManager.getAllProviders().contains(LocationManager.GPS_PROVIDER)) {
                if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
                    // 30秒更新一次,或最小位移变化超过100米更新一次;
                    mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 30 * 1000,
                            mMinDistance, mLocationListener);
                } else {
                    Log.d(TAG, "provider LocationManager.GPS_PROVIDER is not enabled");
                }
            } else {
                Log.d(TAG, "not provider LocationManager.GPS_PROVIDER.");
            }
        }

        Log.i(TAG, "registerGpsListener() end");
    }

2、编写地理位置变化监听函数:

 /**
     * 地理位置变化监听函数
     */
    private LocationListener mLocationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            Log.d(TAG, "Provider onLocationChanged == > latitude :" + location.getLatitude() + " " +
                    ";longitude : " + location.getLongitude() + " ;time : " + StringUtil
                    .getCurrentTime(location.getTime()));
            if (mLastLocation != null) {
                if (DeviceInfoUtils.convertFormat(mLastLocation.getLatitude()).equals(
                        DeviceInfoUtils.convertFormat(location.getLatitude()))
                        && DeviceInfoUtils.convertFormat(mLastLocation.getLongitude())
                        .equals(DeviceInfoUtils.convertFormat(location
                                .getLongitude()))) {
                    Log.d(TAG, "onLocationChanged location is not changed");
                    return;
                }
            }
            mLastLocation = location;

            Map args = new HashMap();
            args.put("init", "false");
            args.put("reportType", "GPRS");
            args.put("operType", "Ukonwn");
            reportDeviceInfo("", args);
        }

        public void onProviderDisabled(String provider) {
            Log.d(TAG, "Provider now is disabled..");
        }

        public void onProviderEnabled(String provider) {
            Log.d(TAG, "Provider now is enabled..");
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {
            Log.d(TAG, "Provider onStatusChanged");
        }
    };

3、获取上次地理位置:

 LocationManager locManger = (LocationManager) context.getSystemService(Context
                    .LOCATION_SERVICE);
            Location loc = locManger.getLastKnownLocation("gps");
            if (loc == null) {
                loc = locManger.getLastKnownLocation("network");
            }

注意添加权限。

相关文章
最新文章
热点推荐