百度map android sdk3.5实现定位 并跳转的指定坐标,添加标记

来源:互联网 发布:淘宝店铺主页图片 编辑:程序博客网 时间:2024/04/30 12:51

前几天又下载了新的百度地图sdk,3.5版本,发现百度地图api有了较大变化


定位和3.0版本差不多

但是设置地图中心和添加maker标记有较大变化

设置地图中心点

// 定义地图状态zoom表示缩放级别3-18
MapStatus mMapStatus = new MapStatus.Builder().target(cenpt)
.zoom(14).build();
// 定义MapStatusUpdate对象,以便描述地图状态将要发生的变化
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory
.newMapStatus(mMapStatus);
// 改变地图状态
// 开启定位图层
mMapView.getMap().setMapStatus(mMapStatusUpdate);

添加maker标记

// 定义Maker坐标点
// 构建Marker图标
BitmapDescriptor bitmap = BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher);
// 构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions().position(cenpt).icon(
bitmap);
// 在地图上添加Marker,并显示
mMapView.getMap().clear();
mMapView.getMap().addOverlay(option);
mLocationClient.stop();








完整代码如下

public class MapActivity extends BaseActivity {
MapView mMapView;


public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener();


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_mapview);
mMapView = (MapView) findViewById(R.id.bmapView);


mLocationClient = new LocationClient(getApplicationContext()); // 声明LocationClient类
mLocationClient.registerLocationListener(myListener); // 注册监听函数
mLocationClient.start();
}


@Override
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
mMapView.onDestroy();
mLocationClient.stop();
}


@Override
protected void onResume() {
super.onResume();
// 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
mMapView.onResume();
}


@Override
protected void onPause() {
super.onPause();
// 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
mMapView.onPause();
}


public class MyLocationListener implements BDLocationListener {
@Override
public void onReceiveLocation(BDLocation 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());
} else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {
sb.append("\naddr : ");
sb.append(location.getAddrStr());
}
System.out.println("" + sb.toString());
LatLng cenpt = new LatLng(location.getLatitude(),
location.getLongitude());
// 定义地图状态zoom表示缩放级别3-18
MapStatus mMapStatus = new MapStatus.Builder().target(cenpt)
.zoom(14).build();
// 定义MapStatusUpdate对象,以便描述地图状态将要发生的变化
MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory
.newMapStatus(mMapStatus);
// 改变地图状态
// 开启定位图层
mMapView.getMap().setMapStatus(mMapStatusUpdate);




// 定义Maker坐标点
// 构建Marker图标
BitmapDescriptor bitmap = BitmapDescriptorFactory
.fromResource(R.drawable.ic_launcher);
// 构建MarkerOption,用于在地图上添加Marker
OverlayOptions option = new MarkerOptions().position(cenpt).icon(
bitmap);
// 在地图上添加Marker,并显示
mMapView.getMap().clear();
mMapView.getMap().addOverlay(option);
mLocationClient.stop();
}
}


}

1 0