Android 第三方SDK 之百度定位(省-市-区)

简单了解下实现定位的方式:

GPS、基站、Wifi方式


由于众所周知的原因,Google的部分原生服务被阉割的很凄惨……由于业务要求仅仅是获取“省-市-区”,选择百度定位SDK。

并且没有做深入研究,仅参考。

百度地图开发者中心


关键源代码(Demo下载请直接跳往底部):

public interface ILocationAdapter {

	public static final String LOCATION_TIME = "LOCATION_TIME";
	public static final String LOCATION_ERROR_CODE = "LOCATION_ERROR_CODE";
	public static final String LOCATION_LATITUDE = "LOCATION_LATITUDE";
	public static final String LOCATION_LONGITUDE = "LOCATION_LONGITUDE";
	public static final String LOCATION_RADIUS = "LOCATION_RADIUS";

	public void init();

	public void start();

	public void setOnLocationStateChangedListener(
			OnLocationStateChanged onChanged);

	public void requestLocation();

	public int requestOfflineLocation();

	public void requestPoi();

	public boolean requestLocationState();

	public void stop();

	public void recycle();

}

public interface IBaiduLocationAdapter extends ILocationAdapter {

	public static final String LOCATION_PROVINCE = "LOCATION_PROVINCE";
	public static final String LOCATION_CITY = "LOCATION_CITY";
	public static final String LOCATION_DISTRICT = "LOCATION_DISTRICT";
	public static final String LOCATION_ADDRSTR = "LOCATION_ADDRSTR";
	public static final String LOCATION_SPEED = "LOCATION_SPEED";
	public static final String LOCATION_SATELLITE = "LOCATION_SATELLITE";
	public static final String LOCATION_POI = "LOCATION_POI";

}

public interface OnLocationStateChanged {

	public void onLocationStateChanged(HashMap<String, ?> locationMap);
}


// 被适配对象
public class BdNetworkLocationAdaptee {

	// private GeofenceClient mGeofenceClient;
	private LocationClient mLocationClient;
	private OnLocationStateChanged mOnLocationStateChanged = null;
	private BdLocationListenner mBdLocationListenner = new BdLocationListenner();
	private Context mContext;

	public BdNetworkLocationAdaptee(Context context) {
		this.mContext = context;
		mLocationClient = new LocationClient(mContext);
		mLocationClient.setAK("697f50541f8d4779124896681cb6584d");
		mLocationClient.registerLocationListener(mBdLocationListenner);
	}

	public void init() {
		setLocationOption();
	}

	public void start() {
		mLocationClient.start();
	}

	public void requestLocation() {
		mLocationClient.requestLocation();
	}

	public boolean requestLocationState() {
		return mLocationClient.isStarted();
	}

	public int requestOfflineLocation() {
		return mLocationClient.requestOfflineLocation();
	}

	public void requestPoi() {
		mLocationClient.requestOfflineLocation();
	}

	public void stop() {
		mLocationClient.stop();
	}

	public void recycle() {
		if (mLocationClient.isStarted()) {
			mLocationClient.stop();
		}
		mLocationClient = null;
	}

	public void setOnLocationStateChangedListener(
			OnLocationStateChanged onChanged) {
		this.mOnLocationStateChanged = onChanged;
	}

	/**
	 * 监听函数,有更新位置的时候,格式化成字符串,输出到屏幕中
	 */
	public class BdLocationListenner implements BDLocationListener {
		@Override
		public void onReceiveLocation(BDLocation location) {

			if (location == null)
				return;
			HashMap<String, Object> locationMap = new HashMap<String, Object>();
			locationMap.put(IBaiduLocationAdapter.LOCATION_TIME,
					location.getTime());
			locationMap.put(IBaiduLocationAdapter.LOCATION_ERROR_CODE,
					location.getLocType());
			locationMap.put(IBaiduLocationAdapter.LOCATION_LATITUDE,
					location.getLatitude());
			locationMap.put(IBaiduLocationAdapter.LOCATION_LONGITUDE,
					location.getLongitude());
			locationMap.put(IBaiduLocationAdapter.LOCATION_RADIUS,
					location.getRadius());

			if (location.getLocType() == BDLocation.TypeGpsLocation) {
				locationMap.put(IBaiduLocationAdapter.LOCATION_SPEED,
						location.getSpeed());
				locationMap.put(IBaiduLocationAdapter.LOCATION_SATELLITE,
						location.getSatelliteNumber());
			} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
				/**
				 * 格式化显示地址信息
				 */
				locationMap.put(IBaiduLocationAdapter.LOCATION_PROVINCE,
						location.getProvince());
				locationMap.put(IBaiduLocationAdapter.LOCATION_CITY,
						location.getCity());
				locationMap.put(IBaiduLocationAdapter.LOCATION_DISTRICT,
						location.getDistrict());
				locationMap.put(IBaiduLocationAdapter.LOCATION_ADDRSTR,
						location.getAddrStr());
			}
			// sb.append(mLocationClient.getVersion());
			// sb.append(location.isCellChangeFlag());

			if (null != mOnLocationStateChanged) {
				mOnLocationStateChanged.onLocationStateChanged(locationMap);
			}
		}

		@Override
		public void onReceivePoi(BDLocation poiLocation) {
			if (poiLocation == null) {
				return;
			}
			HashMap<String, Object> locationMap = new HashMap<String, Object>();
			locationMap.put(IBaiduLocationAdapter.LOCATION_TIME,
					poiLocation.getTime());
			locationMap.put(IBaiduLocationAdapter.LOCATION_ERROR_CODE,
					poiLocation.getLocType());
			locationMap.put(IBaiduLocationAdapter.LOCATION_LATITUDE,
					poiLocation.getLatitude());
			locationMap.put(IBaiduLocationAdapter.LOCATION_LONGITUDE,
					poiLocation.getLongitude());
			locationMap.put(IBaiduLocationAdapter.LOCATION_RADIUS,
					poiLocation.getRadius());
			if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation) {
				locationMap.put(IBaiduLocationAdapter.LOCATION_ADDRSTR,
						poiLocation.getAddrStr());
			}
			if (poiLocation.hasPoi()) {
				locationMap.put(IBaiduLocationAdapter.LOCATION_POI,
						poiLocation.getPoi());
			}

			if (null != mOnLocationStateChanged) {
				mOnLocationStateChanged.onLocationStateChanged(locationMap);
			}
		}
	}

	private void setLocationOption() {
		LocationClientOption option = new LocationClientOption();
		option.setOpenGps(getGpsState());
		// 设置坐标类型
		option.setCoorType("bd09ll");
		option.setServiceName("com.baidu.location.service_v2.9");
		option.setPoiExtraInfo(false);
		option.setAddrType("all");
		// 设置定位模式,小于1秒则一次定位;大于等于1秒则定时定位
		option.setScanSpan(1000);
		option.setPriority(LocationClientOption.NetWorkFirst); // 设置网络优先
		// option.setPriority(LocationClientOption.GpsFirst); // 不设置,默认是gps优先

		option.setPoiNumber(10);
		option.disableCache(true);
		mLocationClient.setLocOption(option);
	}

	private boolean getGpsState() {

		LocationManager mLocationManager = (LocationManager) mContext
				.getSystemService(Context.LOCATION_SERVICE);
		return mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
	}
}

//	适配对象
public class BdNetworkLocationTarget implements IBaiduLocationAdapter {

	// 具体实现类,如果需要适配多种情况可以抽象此类
	private BdNetworkLocationAdaptee mAdaptee;

	public BdNetworkLocationTarget(BdNetworkLocationAdaptee adaptee) {
		this.mAdaptee = adaptee;
	}

	@Override
	public void init() {
		mAdaptee.init();
	}

	@Override
	public void start() {
		mAdaptee.start();

	}

	@Override
	public void requestLocation() {
		mAdaptee.requestLocation();
	}

	@Override
	public boolean requestLocationState() {
		return mAdaptee.requestLocationState();
	}

	@Override
	public int requestOfflineLocation() {
		return mAdaptee.requestOfflineLocation();
	}

	@Override
	public void requestPoi() {
		mAdaptee.requestPoi();
	}

	@Override
	public void stop() {
		mAdaptee.stop();
	}

	@Override
	public void recycle() {
		mAdaptee.recycle();
	}

	@Override
	public void setOnLocationStateChangedListener(
			OnLocationStateChanged onChanged) {
		mAdaptee.setOnLocationStateChangedListener(onChanged);
	}

}

主入口程序

public class mainActivity extends Activity {
	private TextView mTv = null;
	private Button mStartBtn;
	private Button mLocBtn;
	private Button mPoiBtn;
	private Button mOfflineBtn;
	private boolean mIsStart;

	public static String TAG = "LocTestDemo";

	BdNetworkLocationAdaptee locationAdaptee;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);
		mTv = (TextView) findViewById(R.id.textview);
		mStartBtn = (Button) findViewById(R.id.StartBtn);
		mLocBtn = (Button) findViewById(R.id.locBtn);
		mPoiBtn = (Button) findViewById(R.id.PoiReq);
		mOfflineBtn = (Button) findViewById(R.id.offLineBtn);
		mIsStart = false;

		locationAdaptee = new BdNetworkLocationAdaptee(getApplicationContext());
		locationAdaptee.init();
		locationAdaptee
				.setOnLocationStateChangedListener(new OnLocationStateChanged() {

					@Override
					public void onLocationStateChanged(
							HashMap<String, ?> locationMap) {

						if (null != locationMap)
							mTv.setText((String) locationMap
									.get(IBaiduLocationAdapter.LOCATION_PROVINCE)
									+ "-"
									+ (String) locationMap
											.get(IBaiduLocationAdapter.LOCATION_CITY)
									+ "-"
									+ (String) locationMap
											.get(IBaiduLocationAdapter.LOCATION_DISTRICT));

					}
				});

		mStartBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (!mIsStart) {
					locationAdaptee.start();
					mStartBtn.setText("stop");
					mIsStart = true;

				} else {
					locationAdaptee.stop();
					mIsStart = false;
					mStartBtn.setText("start");
				}
			}
		});

		// 定位按钮
		mLocBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (locationAdaptee != null
						&& locationAdaptee.requestLocationState()) {
					locationAdaptee.requestLocation();
				} else
					Log.d(TAG, "locClient is null or not started");
			}
		});

		mPoiBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				locationAdaptee.requestPoi();
			}
		});

		// 离线基站定位按钮
		mOfflineBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int req = locationAdaptee.requestOfflineLocation();
			}
		});
	}

	@Override
	public void onDestroy() {
		locationAdaptee.stop();
		super.onDestroy();
	}

}

点击下载mDemo-baiduLocation.zip 资源

你可能感兴趣的:(Android 第三方SDK 之百度定位(省-市-区))