1、初始化
SDKInitializer.initialize(getApplicationContext());//我测试在Application的onCreate()不行,必须在activity的onCreate()中2、配置map参数
[html] view plain copy private void initMap() { // 地图初始化 mMapView = (MapView) findViewById(R.id.id_bmapView); mBaiduMap = mMapView.getMap(); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); // 定位初始化 mlocationClient = new LocationClient(this); mlocationClient.registerLocationListener(myListener); LocationClientOption option = new LocationClientOption(); option.setOpenGps(true); // 打开gps option.setCoorType("bd09ll"); // 设置坐标类型 option.setScanSpan(5000);//设置onReceiveLocation()获取位置的频率 option.setIsNeedAddress(true);//如想获得具体位置就需要设置为true mlocationClient.setLocOption(option); mlocationClient.start(); mCurrentMode = MyLocationConfiguration.LocationMode.FOLLOWING; mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration( mCurrentMode, true, null)); myOrientationListener = new MyOrientationListener(this); //通过接口回调来实现实时方向的改变 myOrientationListener.setOnOrientationListener(new MyOrientationListener.OnOrientationListener() { @Override public void onOrientationChanged(float x) { mCurrentX = x; } }); myOrientationListener.start(); mSearch = RoutePlanSearch.newInstance(); mSearch.setOnGetRoutePlanResultListener(this); initMarkerClickEvent(); }3、获取当前地址
[html] view plain copy public class MyLocationListenner implements BDLocationListener {@Override
public void onReceiveLocation(BDLocation bdLocation) { // map view 销毁后不在处理新接收的位置 if (bdLocation == null || mMapView == null) { return; } MyLocationData locData = new MyLocationData.Builder() .accuracy(bdLocation.getRadius()) .direction(mCurrentX)//设定图标方向 // 此处设置开发者获取到的方向信息,顺时针0-360 .latitude(bdLocation.getLatitude()) .longitude(bdLocation.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); currentLatitude = bdLocation.getLatitude(); currentLongitude = bdLocation.getLongitude(); current_addr.setText(bdLocation.getAddrStr()); currentLL = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude()); startNodeStr = PlanNode.withLocation(currentLL); //option.setScanSpan(5000),每隔5000ms这个方法就会调用一次,而有些我们只想调用一次,所以要判断一下isFirstLoc if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(bdLocation.getLatitude(), bdLocation.getLongitude()); MapStatus.Builder builder = new MapStatus.Builder(); //地图缩放比设置为18 builder.target(ll).zoom(18.0f); mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build())); changeLatitude = bdLocation.getLatitude(); changeLongitude = bdLocation.getLongitude(); if (!isServiceLive) { addOverLayout(currentLatitude, currentLongitude); } } }