百度地图在android中的使用方法,百度地图在Android中的使用

有关百度地图的使用,(http://lbsyun.baidu.com/index.php?title=androidsdk/sdkandev-download)这个链接说明的很详细了,包括环境的搭建,接下来我主要说明我的demo的几个功能。最后给出源码。

1.引入百度地图

// 在使用SDK各组件之前初始化context信息,传入ApplicationContext

// 注意该方法要再setContentView方法之前实现

SDKInitializer.initialize(getApplicationContext());

setContentView(R.layout.main);

mMapView = (MapView) findViewById(R.id.bmapView);

// 设置放大比例

mBaiduMap = mMapView.getMap();

MapStatusUpdate msu = MapStatusUpdateFactory.zoomTo(15.0f);

mBaiduMap.setMapStatus(msu);

这个官方文档上有详细说明

2.定位以及方向定位,模式的切换

locate函数实现定位和方向定位的(关键代码被我抠出来了)

// 注册监听器

mLocationListener = new MyLocationListener();

mLocationClient = new LocationClient(this);

mLocationClient.registerLocationListener(mLocationListener);

// 配置定位SDK参数

LocationClientOption option = new LocationClientOption();

// 可选,默认高精度,设置定位模式,高精度,低功耗,仅设备

option.setLocationMode(LocationMode.Hight_Accuracy);

option.setCoorType("bd09ll");// 可选,默认gcj02,设置返回的定位结果坐标系

// 可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的

option.setScanSpan(1000);

option.setIsNeedAddress(true);// 可选,设置是否需要地址信息,默认不需要

option.setOpenGps(true);// 可选,默认false,设置是否使用gps

mLocationClient.setLocOption(option);

// 方向传感器,用来控制定位图标所指的方向

myOrientationListener = new MyOrientationListener(

getApplicationContext());

myOrientationListener

.setOnOrientationListenr(new OnOrientationListenr() {

@Override

public void onOrientationChanged(float x) {

mCurrentX = x;

}

});

其中,MyOrientationListener.java文件中定义了一个MyOrientationListener用来通知主界面

public MyOrientationListener(Context context) {

super();

this.context = context;

}

public interface OnOrientationListenr {

void onOrientationChanged(float x);

}

MyOrientationListener.java就是引入方向传感器来定位方向的。

但是Sensor.TYPE_ORIENTATION已过期

所以参考了http://blog.csdn.net/android_qhdxuan/article/details/7454313

模式切换比较简单

就是点击菜单键多出来的三个XX模式(普通,跟随,罗盘)

menu/main.xml

android:id="@+id/normal"

android:showAsAction="never"

android:title="@string/normal"/>

android:id="@+id/follow"

android:showAsAction="never"

android:title="@string/follow"/>

android:id="@+id/compass"

android:showAsAction="never"

android:title="@string/compass"/>

然后在onOptionsItemSelected中

case R.id.normal:

// 普通态: 更新定位数据时不对地图做任何操作

mode = MyLocationConfiguration.LocationMode.NORMAL;

break;

case R.id.follow:

// 跟随态,保持定位图标在地图中心

mode = MyLocationConfiguration.LocationMode.FOLLOWING;

break;

case R.id.compass:

// 罗盘态,显示定位方向圈,保持定位图标在地图中心

mode = MyLocationConfiguration.COMPASS

3.添加覆盖物

初始化图层

private void addOverLay(List infos) {

mBaiduMap.clear();

LatLng latLng = null;

Marker marker = null;

OverlayOptions options;

for (Info info : infos) {

latLng = new LatLng(info.getLatitude(), info.getLongtitude());

options = new MarkerOptions().position(latLng).icon(mMaker)

.zIndex(5);

marker = (Marker) mBaiduMap.addOverlay(options);

Bundle bundle = new Bundle();

bundle.putSerializable("info", info);

marker.setExtraInfo(bundle);

}

MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(latLng);

mBaiduMap.setMapStatus(msu);

}

然后为地图上的Marker添加点击事件:

mBaiduMap.setOnMarkerClickListener(new OnMarkerClickListener() {

@Override

public boolean onMarkerClick(Marker marker) {

Bundle bundle = marker.getExtraInfo();

Info info = (Info) bundle.getSerializable("info");

// 设置显示的参数

((ImageView) marker_layout.findViewById(R.id.info_img))

.setImageResource(info.getImgId());

((TextView) marker_layout.findViewById(R.id.info_distance))

.setText(info.getDistance());

((TextView) marker_layout.findViewById(R.id.info_name))

.setText(info.getName());

((TextView) marker_layout.findViewById(R.id.info_zan))

.setText(info.getZan() + "");

// 点击覆盖物显示文本信息

BitmapDescriptor bd = BitmapDescriptorFactory

.fromResource(R.drawable.locate);

final LatLng latLng = marker.getPosition();

Point point = mBaiduMap.getProjection()

.toScreenLocation(latLng);

LatLng ll = mBaiduMap.getProjection().fromScreenLocation(point);

InfoWindow infoWindow = new InfoWindow(bd, ll, 0,

new InfoWindow.OnInfoWindowClickListener() {

@Override

public void onInfoWindowClick() {

mBaiduMap.hideInfoWindow();

}

});

mBaiduMap.showInfoWindow(infoWindow);

marker_layout.setVisibility(View.VISIBLE);

MapStatusUpdate msu = MapStatusUpdateFactory.newLatLng(ll);

mBaiduMap.animateMapStatus(msu);

return true;

}

});

最后添加地图的单击事件,隐藏出现的详细信息布局和InfoWindow

mBaiduMap.setOnMapClickListener(new OnMapClickListener() {

@Override

public boolean onMapPoiClick(MapPoi arg0) {

return false;

}

@Override

public void onMapClick(LatLng arg0) {

marker_layout.setVisibility(View.INVISIBLE);

mBaiduMap.hideInfoWindow();

}

});

源码地址:http://download.csdn.net/detail/lxj1137800599/9508084