andriod开发的过程中可能会涉及到再andriod使用百度地图的问题,这个问题的解决分为了三个步骤
第一步:打开百度开放平台,然后下载百度地图的sdk,之后再导入到这个项目中,就可以申请到自己的appkey。
第二步:创建一个layout,这个方面你可以自由发挥
[java] view plaincopyprint?
1. <SPAN style="FONT-SIZE: 18px"><com.baidu.mapapi.map.MapView
2. android:id="@+id/bmapView"
3. android:layout_width="fill_parent"
4. android:layout_height="0.0px"
5. android:layout_weight="2.07"
6. android:clickable="true" /></SPAN>
<com.baidu.mapapi.map.MapView
android:id="@+id/bmapView"
android:layout_width="fill_parent"
android:layout_height="0.0px"
android:layout_weight="2.07"
android:clickable="true" />
第三部:建立一个activity。
private static LocationData locationData = new LocationData();
static MapView mMapView;
public LocationClient mLocationClient = null;
public BDLocationListener myListener = new MyLocationListener();
private MapController mMapController;
private MKSearch mkSearch;
private GeoPoint point2;
private MKMapViewListener mMapListener = null;
private LocationClientOption option;
必须在oncreate中初始化百度地图的key,而且是在setContentView的前面初始化。
MapController 是对地图的控制,比如缩放,zoom级别,地图的中心点等等
mMapController.setCenter(point);// 设置地图中心点
mMapController.setZoom(15);// 设置地图zoom级别
mMapController.enableClick(true);
mMapView.setOnTouchListener(null);
定位的核心代码:BDLocationListener
private class MyLocationListener implements BDLocationListener {
// 大概一秒钟定位一次
@Override
public void onReceiveLocation(BDLocation location) {
if (location == null)
return;
if (flag_new == true) {
myPoint = new GeoPoint((int) (location.getLatitude() * 1e6),
(int) (location.getLongitude() * 1e6));
locationData.latitude = location.getLatitude();
locationData.longitude = location.getLongitude();
locationData.direction = 2.0f;
locationData.accuracy = location.getRadius();// 获取服务
locationData.direction = location.getDerect();
// strAdd = location.getAddrStr().toString();
mMapView.refresh();// 此处刷新必须有
// 定位本地位置,此句没有,则无法定位
mMapController.animateTo(new GeoPoint(
(int) (locationData.latitude * 1e6),
(int) (locationData.longitude * 1e6)));
MyLocationOverlay myLocationOverlay = new MyLocationOverlay(
mMapView);
// 手动将位置源置为天安门,在实际应用中,请使用百度定位SDK获取位置信息,要在SDK中显示一个位置,需要
// 使用百度经纬度坐标(bd09ll)
locationData.direction = 2.0f;
myLocationOverlay.setData(locationData);
mMapView.getOverlays().add(myLocationOverlay);
mMapView.refresh();
mMapView.getController().animateTo(
new GeoPoint((int) (lat), (int) (lon)));
flag_new = false;
}
}
要进行路线的查询的就要实现MKSearch()中的MKSearchListener()
其实百度地图给出了很详细的api资料了,可以对着来一步一步实现
本文来自麦子学院:http://www.maiziedu.com/