为android封装的百度定位组件

package com.android.location;

import java.math.BigDecimal;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

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

/**
 * 利用百度地图定位
 * 
 * @author Esa
 */
public class LocationService {

	private String TAG = "Location";
	public static final int DATA = 6;
	private LocationClient client;
	private LocationClientOption option;
	private Handler handler;

	/**
	 * 构造函数
	 */
	public LocationService(Context context) {
		client = new LocationClient(context);// 实例化定位类
		client.registerLocationListener(new BDLocationListener() {

			@Override
			public void onReceiveLocation(BDLocation location) {
				if (location != null) {
					int locType = location.getLocType();// 获取定位类型,161网络定位 61GPS定位
					Log.w(TAG, "Location type = " + locType);
					if (locType == BDLocation.TypeNetWorkLocation || locType == BDLocation.TypeGpsLocation) {
						double latitude = location.getLatitude();// 返回维度
						double longitude = location.getLongitude();// 返回经度
						String address = location.getAddrStr();// 返回地理信息
						float radius = -1;
						if (location.hasRadius()) {
							radius = location.getRadius();// 获取定位精度半径,单位是米
							BigDecimal b = new BigDecimal(radius);
							radius = b.setScale(2, BigDecimal.ROUND_HALF_UP).floatValue();
						}
						if (handler != null) {
							StringBuffer json = new StringBuffer();
							json.append("{\"locType\":\"").append(locType == BDLocation.TypeNetWorkLocation ? "网络定位" : "GPS定位").append("\",");
							json.append("\"latitude\":\"").append(latitude).append("\",");
							json.append("\"longitude\":\"").append(longitude).append("\",");
							json.append("\"radius\":\"").append(radius).append("\",");
							json.append("\"address\":\"").append(address).append("\"}");
							Message msg = handler.obtainMessage();
							msg.what = DATA;
							msg.obj = json.toString();
							Log.d(TAG, "定位结果 => " + json.toString());
							handler.sendMessage(msg);
						}
					}
				}
			}

			@Override
			public void onReceivePoi(BDLocation location) {
			}

		});// 设置监听

		option = new LocationClientOption();// 实例化定位参数
		option.setAddrType("all");// 设置返回地理信息
		option.setOpenGps(true);
		// option.setPriority(LocationClientOption.GpsFirst);
		option.setCoorType("bd09ll");// 设置为百度坐标系
		option.disableCache(true);// 不使用缓存
		option.setScanSpan(1000 * 3);// 定位间隔
		client.setLocOption(option);// 设置参数
	}

	/**
	 * 通过经纬度获取地理信息
	 * 
	 * @param latitude
	 * @param longitude
	 * @return
	 */
	public synchronized String getAddress(String latitude, String longitude) {
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(String.format("http://api.map.baidu.com/geocoder/v2/?ak=%s&location=%s,%s&output=%s&pois=%s", "C2ab471c7883b11890e509c2abb27b56", latitude, longitude, "json", "0"));
		try {
			HttpResponse response = client.execute(get);
			if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
				return null;
			}
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				String json = EntityUtils.toString(entity, "UTF-8");
				JSONObject object = new JSONObject(json);
				int status = object.getInt("status");
				if (status == 0) {
					String address = object.getJSONObject("result").getString("formatted_address");
					if (address != null && !"".equals(address.trim())) {
						return address;
					}
				}
			}
		} catch (Exception e) {
			Log.e(TAG, "", e);
		}
		return null;
	}

	/**
	 * 启动定位
	 * 
	 * @param handler
	 *            接收的handler
	 */
	public void start(Handler handler) {
		this.handler = handler;
		this.start();
	}

	/**
	 * 开启定位
	 */
	private void start() {
		if (client.isStarted()) {
			Log.w(TAG, "定位服务 正在运行");
		} else {
			Log.d(TAG, "开启定位");
			client.start();
		}
	}

	/**
	 * 是否正在定位
	 * 
	 * @return
	 */
	public boolean isStarted() {
		return client.isStarted();
	}

	/**
	 * 停止定位
	 */
	public void stop() {
		Log.d(TAG, "定位服务 关闭");
		client.stop();
		handler = null;
	}
}


LocationService service = new LocationService(Context);

service.start(Handler);

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值