package com.wise.wawc; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.wise.pubclas.Constant; import com.wise.pubclas.GetSystem; import com.wise.pubclas.Variable; import android.app.Service; import android.content.Intent; import android.os.IBinder; import android.util.Log; /** * 定位服务 * @author honesty */ public class LocationService extends Service { private static final String TAG = "LocationService"; public LocationClient mLocationClient = null; public BDLocationListener myListener = new MyLocationListener(); @Override public IBinder onBind(Intent intent) { return null; } @Override public void onCreate() { super.onCreate(); Log.e(TAG, "onCreate"); mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类 mLocationClient.registerLocationListener(myListener); // 注册监听函数 mLocationClient.setAK(WawcApplication.strKey); SetOption(); Log.d(TAG, "发起定位请求"); mLocationClient.start(); } @Override public int onStartCommand(Intent intent, int flags, int startId) { Log.e(TAG, "onStartCommand"); return super.onStartCommand(intent, flags, startId); } @Override public void onDestroy() { Log.e(TAG, "onDestroy"); mLocationClient.stop(); mLocationClient.unRegisterLocationListener(myListener); super.onDestroy(); } private void SetOption() { LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); option.setAddrType("all");// 返回的定位结果包含地址信息 option.setCoorType("bd09ll");// 返回的定位结果是百度经纬度,默认值gcj02 option.setScanSpan(60000);// 设置发起定位请求的间隔时间为5000ms option.disableCache(true);// 禁止启用缓存定位 option.setPoiNumber(5); // 最多返回POI个数 option.setPoiDistance(1000); // poi查询距离 option.setPoiExtraInfo(true); // 是否需要POI的电话和地址等详细信息 mLocationClient.setLocOption(option); } class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location == null) return; Log.d(TAG, "定位成功"); StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("\nerror code : "); sb.append(location.getLocType()); sb.append("\nlatitude : "); sb.append(location.getLatitude()); sb.append("\nlontitude : "); sb.append(location.getLongitude()); sb.append("\nradius : "); sb.append(location.getRadius()); Variable.Lat = location.getLatitude(); Variable.Lon = location.getLongitude(); if (location.getLocType() == BDLocation.TypeGpsLocation) { sb.append("\nspeed : "); sb.append(location.getSpeed()); sb.append("\nsatellite : "); sb.append(location.getSatelliteNumber()); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) { sb.append("\naddr : "); sb.append(location.getAddrStr()); Variable.Adress = location.getAddrStr(); Variable.City = location.getCity(); Variable.gpsTime = GetSystem.GetNowTime(); //发送定位城市广播,选择城市用到 Intent intent = new Intent(Constant.A_City); intent.putExtra("City", location.getCity()); intent.putExtra("AddrStr", location.getAddrStr()); intent.putExtra("Lat", String.valueOf(location.getLatitude())); intent.putExtra("Lon", String.valueOf(location.getLongitude())); sendBroadcast(intent); } } @Override public void onReceivePoi(BDLocation poiLocation) { if (poiLocation == null) { return; } StringBuffer sb = new StringBuffer(256); sb.append("Poi time : "); sb.append(poiLocation.getTime()); sb.append("\nerror code : "); sb.append(poiLocation.getLocType()); sb.append("\nlatitude : "); sb.append(poiLocation.getLatitude()); sb.append("\nlontitude : "); sb.append(poiLocation.getLongitude()); sb.append("\nradius : "); sb.append(poiLocation.getRadius()); if (poiLocation.getLocType() == BDLocation.TypeNetWorkLocation) { sb.append("\naddr : "); sb.append(poiLocation.getAddrStr()); } if (poiLocation.hasPoi()) { sb.append("\nPoi:"); sb.append(poiLocation.getPoi()); } else { sb.append("noPoi information"); } Log.d(TAG, "poi:" + sb.toString()); } } }