龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > 移动开发 > Android开发 >

在Android里完美实现基站和WIFI定位(5)

时间:2014-07-26 02:48来源:网络整理 作者:网络 点击:
分享到:
如果还想同时使用GPS定位,其实也很简单,可以和FourSquare提供的BestLocationListener结合起来,将上面那段代码添加到BestLocationListener的register方法里: public v

如果还想同时使用GPS定位,其实也很简单,可以和FourSquare提供的BestLocationListener结合起来,将上面那段代码添加到BestLocationListener的register方法里:

public void register(LocationManager locationManager, boolean gps, Context context) {
    if (DEBUG) Log.d(TAG, "Registering this location listener: " + this.toString());
    long updateMinTime = SLOW_LOCATION_UPDATE_MIN_TIME;
    long updateMinDistance = SLOW_LOCATION_UPDATE_MIN_DISTANCE;
    if (gps) {
      updateMinTime = LOCATION_UPDATE_MIN_TIME;
      updateMinDistance = LOCATION_UPDATE_MIN_DISTANCE;
    }
    List<String> providers = locationManager.getProviders(true);
    int providersCount = providers.size();
    if (!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) && !locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        setChanged();
        notifyObservers(null);
    }
    for (int i = 0; i < providersCount; i++) {
      String providerName = providers.get(i);
      if (locationManager.isProviderEnabled(providerName)) {
        updateLocation(locationManager.getLastKnownLocation(providerName));
      }
      // Only register with GPS if we've explicitly allowed it.
      if (gps || !LocationManager.GPS_PROVIDER.equals(providerName)) {
        locationManager.requestLocationUpdates(providerName, updateMinTime,
            updateMinDistance, this);
      }
    }
    if(cellLocationManager == null) {
        CellInfoManager cellManager = new CellInfoManager(context);
         WifiInfoManager wifiManager = new WifiInfoManager(context);
         cellLocationManager = new CellLocationManager(context, cellManager, wifiManager) {
            @Override
            public void onLocationChanged() {
                if ((latitude() == 0.0D) || (longitude() == 0.0D)) return;
                Location result = new Location("CellLocationManager");
                result.setLatitude(latitude());
                result.setLongitude(longitude());
                result.setAccuracy(accuracy());
                onBestLocationChanged(result);
                this.stop();
            }
         };
    }
    //cellLocationManager.stop();
    cellLocationManager.start();
//    LocationController controller = LocationController.requestLocationUpdates("", updateMinTime,updateMinDistance, this, context);
//    controller.requestCurrentLocation();
  }

精彩图集

赞助商链接