百度地图自动定位

来源:互联网 发布:山东大学网络教育网 编辑:程序博客网 时间:2024/05/01 02:00

1.初始化地图

       private static MapView mMapView = null;
private BaiduMap mBaiduMap = null;
private LocationClient locationClient = null;// 定位相关声明
private double mCurrentLantitude ,mCurrentLongitude ;// 定位经纬度
private boolean isFirstLoc = true;// 是否首次定位


               mMapView = (MapView) findViewById(R.id.vbmapView);
mMapView.showZoomControls(false);
mBaiduMap = mMapView.getMap();
MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(12.0f);
mBaiduMap.setMapStatus(msu);


2.设置定位参数


private void setLocation() {


// 开启定位图层
mBaiduMap.setMyLocationEnabled(true);


locationClient = new LocationClient(
VenueOptionsActivity.this.getApplicationContext()); // 实例化LocationClient类
locationClient.registerLocationListener(myListener); // 注册监听函数


/** 设置参数 */
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); // 打开GPS
option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);// 设置定位模式
option.setCoorType("bd09ll"); // 返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(5000); // 设置发起定位请求的间隔时间为5000ms
option.setIsNeedAddress(true); // 返回的定位结果包含地址信息
option.setNeedDeviceDirect(true); // 返回的定位结果包含手机机头的方向
locationClient.setLocOption(option);


locationClient.start(); // 开始定位
locationClient.requestLocation();
// baiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL); // 设置为一般地图


// baiduMap.setMapType(BaiduMap.MAP_TYPE_SATELLITE); //设置为卫星地图
// baiduMap.setTrafficEnabled(true); //开启交通图


}

3.定位监听

/** 定位监听 */
BDLocationListener myListener = new BDLocationListener() {
@Override
public void onReceiveLocation(BDLocation location) {
// map view 销毁后不在处理新接收的位置
if (location == null || mMapView == null)
return;


MyLocationData locData = new MyLocationData.Builder()
.accuracy(location.getRadius())
// 此处设置开发者获取到的方向信息,顺时针0-300
.direction(100).latitude(location.getLatitude())
.longitude(location.getLongitude()).build();
mBaiduMap.setMyLocationData(locData); // 设置定位数据
mCurrentLantitude = location.getLatitude();
mCurrentLongitude = location.getLongitude();

if (isFirstLoc) {
isFirstLoc = false;


LatLng ll = new LatLng(location.getLatitude(),
location.getLongitude());
MapStatusUpdate u = MapStatusUpdateFactory
.newLatLngZoom(ll, 14); // 设置地图中心点以及缩放级别
mBaiduMap.animateMapStatus(u);
MapUtil.setCurrentLocation(ll);
Log.e("mLantitude, mLongitude", "mLantitude, mLongitude"+mCurrentLantitude+"  "+mCurrentLongitude);
showLocation(mCurrentLantitude, mCurrentLongitude);// 显示获取地址信息
}
}




};


0 0
原创粉丝点击