Android Open Source - Locations Location Module






From Project

Back to project page Locations.

License

The source code is released under:

Apache License

If you think the Android project Locations listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.ixming.android.location.baidu;
/*from  w  w w. ja va 2 s . c  om*/
import android.content.Context;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.location.LocationClientOption.LocationMode;

public class LocationModule {
  
  private static final String TAG = LocationModule.class.getSimpleName();
  
  private final long TIMEOUT = 10 * 1000;
  private final Handler mHandler;
  private final TimeoutRunnable mTimeoutRunnable = new TimeoutRunnable();
  private final LocationClient mLocationClient;
  private final LocationClientOption mLocationClientOption;
  private final MyLocationListener mBDLocationListener;
  
  /**
   * ???????
   */
  public LocationModule(Context context) { 
    mHandler = new Handler();
    
    mLocationClientOption = new LocationClientOption();
    // ????GPS??????????????????
    mLocationClientOption.setLocationMode(LocationMode.Battery_Saving);
//    mLocationClientOption.setLocationMode(LocationMode.Hight_Accuracy);
    mLocationClientOption.setCoorType("gcj02");
    mLocationClientOption.setNeedDeviceDirect(true);
    mLocationClientOption.setIsNeedAddress(true);
//    mLocationClientOption.setOpenGps(false);
    
    mLocationClient = new LocationClient(context);
    mLocationClient.setLocOption(mLocationClientOption);
    
    mBDLocationListener = new MyLocationListener();
    mLocationClient.registerLocationListener(mBDLocationListener);
  }
  
  public void startContinuousLocation(final OnLocationLoadListener listener) {
    mLocationClientOption.setScanSpan(5000);
    
    mBDLocationListener.setOnLocationLoadListener(listener);
    if (!mLocationClient.isStarted()) {
      mLocationClient.start();
    }
    mLocationClient.requestLocation();
  }
  
  public void requestLocation(final OnLocationLoadListener listener) {
    mHandler.postDelayed(mTimeoutRunnable
        .setOnLocationLoadListener(listener), TIMEOUT);
    
    mLocationClientOption.setScanSpan(0);
    
    mBDLocationListener.setOnLocationLoadListener(listener);
    if (!mLocationClient.isStarted()) {
      mLocationClient.start();
    }
    mLocationClient.requestLocation();
  }
  
  public void stopLocation() {
    stopLocationClient(mLocationClient);
    
    mBDLocationListener.setOnLocationLoadListener(null);
  }
  
  protected void preventRTimeoutRunnable() {
    mHandler.removeCallbacks(mTimeoutRunnable.reset());
  }
  
  private void stopLocationClient(LocationClient client) {
    if (null != client) {
      if (client.isStarted()) {
        client.stop();
      }
    }
  }
  
  private class MyLocationListener implements BDLocationListener {
    private OnLocationLoadListener mOnLocationLoadListener;
    public MyLocationListener setOnLocationLoadListener(OnLocationLoadListener listener) {
      synchronized (mBDLocationListener) {
        mOnLocationLoadListener = listener;
      }
      return this;
    }
    
    @Override
    public void onReceivePoi(BDLocation location) { }
    
    @Override
    public void onReceiveLocation(BDLocation location) {
      if (null == mOnLocationLoadListener) {
        return;
      }
      synchronized (mBDLocationListener) {
        boolean isHandlered = false;
        try {
          if (null == location) {
            mOnLocationLoadListener.onLocationFailed(mErrorMessages.get(ERROR_COMMON));
            isHandlered = true;
          } else {
            String city = location.getCity();
            if (TextUtils.isEmpty(city)) {
              mOnLocationLoadListener.onLocationFailed(mErrorMessages.get(location.getLocType()));
            } else {
              mOnLocationLoadListener.onLocationLoad(new LocationInfo(location));
            }
            isHandlered = true;
          }
        } catch (Exception e) {
          Log.e(TAG, "onReceiveLocation Exception: " +   e.getMessage());
        } finally {
          preventRTimeoutRunnable();
          if (!isHandlered) {
            mOnLocationLoadListener.onLocationFailed(mErrorMessages.get(ERROR_COMMON));
          }
          // if ScanSpan < 1000, one time
          if (mLocationClientOption.getScanSpan() < LocationClientOption.MIN_SCAN_SPAN) {
            stopLocation();
          }
        }
      }
    }
  }

  private class TimeoutRunnable implements Runnable {
    private OnLocationLoadListener mListener;
    public TimeoutRunnable setOnLocationLoadListener(OnLocationLoadListener listener) {
      mListener = listener;
      return this;
    }
    
    public TimeoutRunnable reset() {
      mListener = null;
      return this;
    }
    
    @Override
    public void run() {
      if (null != mListener) {
        mListener.onLocationFailed(mErrorMessages.get(ERROR_TIMEOUT));
      }
      stopLocationClient(mLocationClient);
    }
  }
  
  private final int ERROR_COMMON = Integer.MIN_VALUE;
  private final int ERROR_TIMEOUT = Integer.MIN_VALUE + 1;
  private final SparseArray<String> mErrorMessages;
  {
    mErrorMessages = new SparseArray<String>(){
      @Override
      public String get(int key) {
        int index = indexOfKey(key);
        if (index < 0) {
          return "???????????";
        }
        return super.valueAt(index);
      }
    };
    mErrorMessages.put(ERROR_COMMON, "???????????");
    mErrorMessages.put(ERROR_TIMEOUT, "???????????");
    mErrorMessages.put(62, "???????????");
//       61 ? GPS?????
//       62 ? ?????????????????????????????
//       63 ? ??????????????????????????????????
//       65 ? ?????????
//       161? ?????????
//       162~167? ?????????? 
    mErrorMessages.put(162, "?????????");
    mErrorMessages.put(163, "?????????");
    mErrorMessages.put(164, "?????????");
    mErrorMessages.put(165, "?????????");
    mErrorMessages.put(166, "?????????");
    mErrorMessages.put(167, "?????????");
  }
}




Java Source Code List

org.ixming.android.location.baidu.LocationInfo.java
org.ixming.android.location.baidu.LocationModule.java
org.ixming.android.location.baidu.OnLocationLoadListener.java