Android笔记之高德地图定位(通过开启服务发送广播用handle消息机制更新位置信息)

步骤 1.导入高德地图jar包 2.新建服务类(定位成功发送广播之后关闭服务)3.接收广播之后更新位置信息

GPSService类(记得在配置文件中配置service)



public class GPSService extends Service implements AMapLocationListener {

	private static final String TAG = "GPSService";
	private LocationManagerProxy mAMapLocManager = null;
	private AMapLocation aMapLocation;// 用于判断定位超时
	private Handler handler = new Handler();
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}

	@Override
	public void onCreate() {
		mAMapLocManager = LocationManagerProxy.getInstance(this);
		/*
		 * mAMapLocManager.setGpsEnable(false);
		 * 1.0.2版本新增方法,设置true表示混合定位中包含gps定位,false表示纯网络定位,默认是true Location
		 * API定位采用GPS和网络混合定位方式
		 * 第一个参数是定位provider,第二个参数时间最短是5000毫秒,第三个参数距离间隔单位是米,第四个参数是定位监听者
		 */
		mAMapLocManager.requestLocationUpdates(LocationProviderProxy.AMapNetwork, 5000, 10, this);
		handler.postDelayed(stop, 12000);// 设置超过12秒还没有定位到就停止定位
	}

	private Runnable stop = new Runnable() {
		@Override
		public void run() {
			if (aMapLocation == null) {
				stopLocation();// 销毁掉定位
			}
		}
	};

	/** 销毁定位 */
	private void stopLocation() {
		if (mAMapLocManager != null) {
			mAMapLocManager.removeUpdates(this);
			mAMapLocManager.destory();
		}
		mAMapLocManager = null;
	}

	@Override
	public void onLocationChanged(AMapLocation location) {
		if (location != null) {
			this.aMapLocation = location;// 判断超时机制
			double geoLat = location.getLatitude();
			double geoLng = location.getLongitude();
			String cityCode = "";
			String desc = "";
			Bundle locBundle = location.getExtras();
			if (locBundle != null) {
				cityCode = locBundle.getString("citycode");
				desc = locBundle.getString("desc");
			}
			// String str = ("定位成功:(" + geoLng + "," + geoLat + ")"
			// + "\n精    度    :" + location.getAccuracy() + "米"
			// + "\n定位方式:" + location.getProvider() + "\n定位时间:"
			// + new Date(location.getTime()).toLocaleString() + "\n城市编码:"
			// + cityCode + "\n位置描述:" + desc + "\n省:"
			// + location.getProvince() + "\n市:" + location.getCity()
			// + "\n区(县):" + location.getDistrict() + "\n区域编码:" + location
			// .getAdCode());
			// 发送广播
			Intent intent = new Intent();
			intent.setAction(Common.LOCATION_ACTION);
			// 经纬度是double类型的
			intent.putExtra("geoLat", geoLat);
			intent.putExtra("geoLng", geoLng);
			intent.putExtra("cityCode", location.getAdCode());
			intent.putExtra("city", location.getCity());
			sendBroadcast(intent);
			// 停止服务
			stopSelf();
		}
	}

	@Override
	@Deprecated
	public void onStart(Intent intent, int startId) {

	}

	@Override
	public boolean stopService(Intent name) {
		stopLocation();// 停止定位
		return super.stopService(name);
	}

	@Override
	public void onLocationChanged(Location location) {

	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {

	}

	@Override
	public void onProviderEnabled(String provider) {

	}

	@Override
	public void onProviderDisabled(String provider) {

	}

}

MainActivity




public class MainActivity extends Activity {

	private LocationBroadcastReceiver receiver;
	private String cityCode;//城市ID
	private String city;//城市地址
	private double geoLat;//经度
	private double geoLng;//纬度

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_find_store);
		// 启动服务  
        Intent intent = new Intent();  
        intent.setClass(this, GPSService.class);  
        startService(intent);
        //注册广播
		IntentFilter filter = new IntentFilter();
		filter.addAction(Common.LOCATION_ACTION);
		receiver = new LocationBroadcastReceiver();
		this.registerReceiver(new LocationBroadcastReceiver(), filter);
	}

	/** 广播接收着 */
	private class LocationBroadcastReceiver extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (!intent.getAction().equals(Common.LOCATION_ACTION))
				return;
			cityCode = intent.getStringExtra("cityCode");
			city = intent.getStringExtra("city");
			geoLat = intent.getDoubleExtra("geoLat", -1);
			geoLng = intent.getDoubleExtra("geoLng", -1);
			Message message = new Message();
			message.what = 0;
			handler.sendMessage(message);
		}
	}

	Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			if (msg.what == 0) {
				//在这里将获取到的定位信息展示到UI界面
			}
		};
	};

}

Android简单定位实例参考博文  
Android定位实例
高德地图jar包可以去官网下载


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值