百度地图API安卓版的GPS定位

本文详细介绍了如何在官方程序中实现定位服务,并通过DemoApp提供了实例代码,包括创建定位监听器、添加MyLocationOverlay、暂停和恢复地图服务等功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在官方程序中略为提及,但显然不够清楚,幸好官方有DemoApp,特将此代码提出。

 

1 添加LocationListerner变量

LocationListener mLocationListener = null; // create时注册此listener,Destroy时需要Remove


2 在OnCreate中注册定位监听器事件

mLocationListener = new LocationListener() {

public void onLocationChanged(Location location) {
	if (location != null) {
					String strLog = String.format("您当前的位置:\r\n" + "纬度:%f\r\n"
							+ "经度:%f", location.getLongitude(),
							location.getLatitude());

					TextView mainText = (TextView) findViewById(R.id.textview);
					mainText.setText(strLog);
				}
				if (location != null) {
					GeoPoint pt = new GeoPoint(
							(int) (location.getLatitude() * 1e6),
							(int) (location.getLongitude() * 1e6));
					mMapView.getController().animateTo(pt);
				}
			}
		};


3 添加MyLocationOverlay变量

MyLocationOverlay mLocationOverlay = null;

4 在OnCreate中添加该层

 

mLocationOverlay = new MyLocationOverlay(this, mMapView);
		mMapView.getOverlays().add(mLocationOverlay);


5 最后在Activity重载两个函数

@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		HZBikeApp app = (HZBikeApp) this.getApplication();
		app.mBMapMan.getLocationManager().removeUpdates(mLocationListener);
		mLocationOverlay.disableMyLocation();
		mLocationOverlay.disableCompass(); // 关闭指南针
		app.mBMapMan.stop();	// 停止地图服务
		super.onPause();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		HZBikeApp app = (HZBikeApp) this.getApplication();
		app.mBMapMan.getLocationManager().requestLocationUpdates(
				mLocationListener);
		mLocationOverlay.enableMyLocation();
		mLocationOverlay.enableCompass(); // 打开指南针
		app.mBMapMan.start();	// 恢复地图服务
		super.onResume();
	}


 

使用GPS 定位,首先,需要在清单文件(AndroidManifest.xml)中注册获取定位的权限: **1.获取位置管理器对象LocationManager** ``` import android.location.LocationManager; LocationManager lm; // lm =(LocationManager) this.getSystemService(Context`.LOCATION_SERVICE); // ``` **2.一般使用LocationManager的getLastKnownLocation(LocationManager.GPS_PROVIDER);方法获取Location对象** ``` String provider = LocationManager.GPS_PROVIDER;// 指定LocationManager的定位方法 Location location = locationManager.getLastKnownLocation(provider);// 调用getLastKnownLocation()方法获取当前的位置信息 ``` 不过不建议用这种方法,有几点原因: 一,在很多提供定位服务的应用程序中,不仅需要获取当前的位置信息,还需要监视位置的变化,在位置改变时调用特定的处理方法 ,其中LocationManager提供了一种便捷、高效的位置监视方法requestLocationUpdates(),可以根据位置的距离变化和时间间隔设定,产生位置改变事件的条件,这样可以避免因微小的距离变化而产生大量的位置改变事件 。 二,当你开启GPS,provider的值为GPS。这时的定位方式为GPS,由于GPS定位慢,所以它不可能立即返回你一个Location对象,所以就返回null了。 **3.推荐locationManager.requestLocationUpdates();方法** LocationManager中设定监听位置变化的代码如下: ``` lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 2000, 10,new MyLocationListener()); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值