您的位置:首页 > 移动开发 > Android开发

百度地图在android的使用

2014-05-10 10:04 537 查看
步骤一:在百度开放平台下载到百度地图的sdk,然后导入到项目中,申请到自己的appkey。
步骤二:创建layout,这个可以自由发挥
<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资料了,可以对着来一步一步实现。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: