百度地图API的官方教程太乱了,不容易上手。就一个定位的小功能,摸索了好久才搞定了。
第一步,核心类
public LocationClient mLocationClient; //定位的核心类
public MyLocationListener mMyLocationListener;//定位数据的返回接口(public class MyLocationListener implements BDLocationListener)
public class MyLocationListener implements BDLocationListener
{
@Override
public void onReceiveLocation(BDLocation location)
{
// Receive Location
//locaiton是定位返回的数据
}
public void onReceivePoi(BDLocation poiLocation)
{
}
}
第二步,初始化定位参数
mLocationClient = new LocationClient(mContext);
mMyLocationListener = new MyLocationListener();
mLocationClient.registerLocationListener(mMyLocationListener);
LocationClientOption option = new LocationClientOption();
option.setLocationMode(LocationMode.Hight_Accuracy);//设置定位模式
option.setCoorType("bd09ll");//返回的定位结果是百度经纬度,默认值gcj02
option.setScanSpan(5000);//设置发起定位请求的间隔时间为5000ms
option.setIsNeedAddress(true);//返回的定位结果包含地址信息
option.setNeedDeviceDirect(true);//返回的定位结果包含手机机头的方向
mLocationClient.setLocOption(option);
mLocationClient.start();
第三步,实现接口。主要是实现其中的onReceiveLocation函数,并将定位数据展现在地图上。
public class MyLocationListener implements BDLocationListener
{
@Override
public void onReceiveLocation(BDLocation location)
{
// Receive Location
mLocation=location;
if (location == null)
{
return;
}
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());
if (location.getLocType() == BDLocation.TypeGpsLocation)
{
sb.append("\nspeed : ");
sb.append(location.getSpeed());
sb.append("\nsatellite : ");
sb.append(location.getSatelliteNumber());
sb.append("\ndirection : ");
sb.append("\naddr : ");
sb.append(location.getAddrStr());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation)
{
sb.append("\naddr : ");
sb.append(location.getAddrStr());
// ��Ӫ����Ϣ
sb.append("\noperationers : ");
}
toast(sb.toString());
MyLocationData locationData=new MyLocationData.Builder().accuracy(mLocation.getRadius())
.direction(100).latitude(mLocation.getLatitude()).longitude(mLocation.getLongitude()).build();
mLatLng=new LatLng(mLocation.getLatitude(), mLocation.getLongitude());//中心点,位置
mMapStatus=new MapStatus.Builder().target(mLatLng).zoom(18).build();
mMapStatusUpdate=MapStatusUpdateFactory.newMapStatus(mMapStatus);
mBaiduMap.setMyLocationData(locationData);//设置定位数据
// 设置定位图层的配置(定位模式,是否允许方向信息,用户自定义定位图标)
mCurrentMarker = BitmapDescriptorFactory.fromResource(R.drawable.icon_geo);
mMapStatusUpdate=MapStatusUpdateFactory.newMapStatus(mMapStatus);
mCurrentMode = com.baidu.mapapi.map.MyLocationConfiguration.LocationMode.NORMAL;
MyLocationConfiguration configuration=new MyLocationConfiguration(mCurrentMode, true, mCurrentMarker);
mBaiduMap.setMyLocationConfigeration(configuration);//设置我的职位标注
mBaiduMap.setMapStatus(mMapStatusUpdate);//把地图中心拉倒我所在的位置
}
public void onReceivePoi(BDLocation poiLocation)
{
mLocation=poiLocation;
// TODO Auto-generated method stub
if (poiLocation==null)
{
return;
}
StringBuffer sbBuffer=new StringBuffer(256);
sbBuffer.append("poi time:");
sbBuffer.append(poiLocation.getTime());
sbBuffer.append("\nerror code:");
sbBuffer.append(poiLocation.getLocType());
sbBuffer.append("\nlatitude:");
sbBuffer.append(poiLocation.getLatitude());
sbBuffer.append("\nlontitude:");
sbBuffer.append(poiLocation.getLongitude());
sbBuffer.append("\nradius:");
sbBuffer.append(poiLocation.getRadius());
if(poiLocation.getLocType()==BDLocation.TypeNetWorkLocation)
{
sbBuffer.append("\naddr:");
sbBuffer.append(poiLocation.getAddrStr());
}
else {
sbBuffer.append("no POi information! ");
}
toast(sbBuffer.toString());
}
}
就这么简单!