android如何实现定位功能,Android 利用百度地图SDK实现定位功能

本文介绍了如何在Android应用中使用BMap SDK实现地图视图,并结合LocationClient进行实时定位。开发者学习了如何初始化地图、设置地图缩放、监听定位事件以及处理定位图层。

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

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical" >

android:id="@+id/map_view"

android:layout_width="match_parent"

android:layout_height="200dp"

android:clickable="true" />

Map.java:

public class HouseKeeperSearch extends Activity {

private MapView mMapView;

private MapController mMapController = null;

// 定位相关

private LocationClient mLocClient;

private LocationData locData = null;

public MyLocationListener myListener = new MyLocationListener();

boolean isFirstLoc = true;// 是否首次定位

private MyLocationOverlay myLocationOverlay = null; // 定点标志

private BMapManager mBMapMan = null;

private ProgressDialog pd;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mBMapMan = new BMapManager(getApplication());

mBMapMan.init("Ut5QiVKCvqiXol4Ph6uW3N3h", null);

pd = new ProgressDialog(this);

pd.setIndeterminate(true);

pd.setMessage(getString(R.string.loading_location));

pd.setCancelable(true);

pd.show();

setContentView(R.layout.activity_housekeeper_search);

initMap();

}

private void initMap() {

// 地图初始化

mMapView = (MapView) findViewById(R.id.map_view);

mMapController = mMapView.getController();

mMapView.getController().setZoom(16);

mMapView.getController().enableClick(true);

mMapView.setBuiltInZoomControls(true);

// 定位初始化

mLocClient = new LocationClient(this);

locData = new LocationData();

mLocClient.registerLocationListener(myListener);

LocationClientOption option = new LocationClientOption();

option.setOpenGps(true);// 打开gps

option.setCoorType("bd09ll"); // 设置坐标类型

option.setScanSpan(1000);

mLocClient.setLocOption(option);

mLocClient.start(); //启动

// 定位图层初始化

myLocationOverlay = new MyLocationOverlay(mMapView);

// 设置定位数据

myLocationOverlay.setMarker(getResources().getDrawable(

R.drawable.location));

// 添加定位图层

mMapView.getOverlays().add(myLocationOverlay);

myLocationOverlay.enableCompass();

// 修改定位数据后刷新图层生效

mMapView.refresh(); //加载

}

/**

* 定位SDK监听函数

*/

public class MyLocationListener implements BDLocationListener {

@Override

public void onReceiveLocation(BDLocation location) {

if (location == null)

return;

locData.latitude = location.getLatitude();

locData.longitude = location.getLongitude();

// 如果不显示定位精度圈,将accuracy赋值为0即可

locData.accuracy = location.getRadius();

// 此处可以设置 locData的方向信息, 如果定位 SDK 未返回方向信息,用户可以自己实现罗盘功能添加方向信息。

locData.direction = location.getDerect();

myLocationOverlay.setData(locData);

// 更新图层数据执行刷新后生效

mMapView.refresh();

// 是手动触发请求或首次定位时,移动到定位点

if (isFirstLoc) {

// 移动地图到定位点

Log.d("LocationOverlay", "receive location, animate to it");

mMapController.animateTo(new GeoPoint(

(int) (locData.latitude * 1e6),

(int) (locData.longitude * 1e6)));

if (pd.isShowing())

pd.dismiss();

}

// 首次定位完成

isFirstLoc = false;

}

public void onReceivePoi(BDLocation poiLocation) {

if (poiLocation == null) {

return;

}

}

}

@Override

protected void onPause() {

mMapView.onPause();

if (mBMapMan != null) {

mBMapMan.stop();

}

super.onPause();

}

@Override

protected void onResume() {

mMapView.onResume();

if (mBMapMan != null) {

mBMapMan.start();

}

super.onResume();

}

@Override

protected void onDestroy() {

// 退出时销毁定位

if (mLocClient != null)

mLocClient.stop();

mMapView.destroy();

if (mBMapMan != null) {

mBMapMan.destroy();

mBMapMan = null;

}

super.onDestroy();

}

@Override

protected void onSaveInstanceState(Bundle outState) {

super.onSaveInstanceState(outState);

mMapView.onSaveInstanceState(outState);

}

@Override

protected void onRestoreInstanceState(Bundle savedInstanceState) {

super.onRestoreInstanceState(savedInstanceState);

mMapView.onRestoreInstanceState(savedInstanceState);

}

}

效果图:

bVcJ1r

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值