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() {@Overridepublic 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);}}}}@Overridepublic 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;}}


LocationServiceservice=newLocationService(Context);

service.start(Handler);

更多相关文章

  1. Android SQLite数据库实例
  2. Android ProgressDialog简单实例
  3. [置顶] 我的Android进阶之旅------>Android MediaPlayer播放mp3
  4. WCF 实例 —— Android 短信助手 (WCF + Android) (2)
  5. Android 开发手记一NDK编程实例
  6. Android中的AsyncTask和Handler应用实例二
  7. Android vector矢量图应用实例

随机推荐

  1. android 设置Button或者ImageButton的背
  2. Android(安卓)FactoryTest 流程
  3. 目标身高Android版
  4. android listview的一些设置
  5. android sqlite java.lang.IllegalArgume
  6. tabcontent中scaleType的使用 图片Drawab
  7. Android(安卓)Material Design 控件常用
  8. Android(安卓)中文 API (18) ―― AbsSeekB
  9. 【移动开发】Android中Fragment+ViewPage
  10. Android自学笔记之Android常见命令操作及