application 初始化
public class MyApplication extends Application{
private static MyApplication mInstance = null;
public boolean m_bKeyRight = true;
@Override
public void onCreate() {
mInstance = this;
SDKInitializer.initialize(this);
}
public static MyApplication getInstance() {
return mInstance;
}
}
定位实现
private void initLocation() {
//开启定位图层
mBaiduMap.setMyLocationEnabled(true);
//定位初始化
mLocClient = new LocationClient(this);
mLocClient.registerLocationListener(new MyLocationListenner(mMapView, mBaiduMap));
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true); //打开gps
option.setCoorType("bd09ll"); //设置坐标类型
option.setNeedDeviceDirect(true);
option.setScanSpan(1000);
option.setIsNeedAddress(true);
mLocClient.setLocOption(option);
//给位置客户端注册位置监听器
mLocClient.registerLocationListener(this);
mLocClient.start();
mBaiduMap.setMyLocationConfigeration(new MyLocationConfiguration(mCurrentMode, true, null));
}
路线规划
case R.id.drive:
mSearch.drivingSearch((new DrivingRoutePlanOption())
.from(stNode).to(enNode));
break;
继承 OnGetRoutePlanResultListener 实现以下方法
@Override
public void onGetWalkingRouteResult(WalkingRouteResult result) {
}
@Override
public void onGetTransitRouteResult(TransitRouteResult result) {
}
@Override
public void onGetDrivingRouteResult(DrivingRouteResult result) {
}
@Override
public void onGetBikingRouteResult(BikingRouteResult bikingRouteResult) {
}
导航
case R.id.btn_nav:
if (BaiduNaviManager.isNaviInited()) {
routeplanToNavi(CoordinateType.BD09LL);
}
private void routeplanToNavi(CoordinateType coType) {
BNRoutePlanNode sNode = null;
BNRoutePlanNode eNode = null;
switch (coType) {
case BD09LL: {
if(longtSt!=null&&longtEn!=null){
sNode = new BNRoutePlanNode( longtSt,latSt, editSt.getText().toString(), null, coType);
eNode = new BNRoutePlanNode( longtEn,latEn, editEn.getText().toString(), null, coType);
}else{
Toast.makeText(this, "输入起始点", 0).show();
}
break;
}
}
if (sNode != null && eNode != null) {
List<BNRoutePlanNode> list = new ArrayList<BNRoutePlanNode>();
list.add(sNode);
list.add(eNode);
BaiduNaviManager.getInstance().launchNavigator(this, list, 1, true, new DemoRoutePlanListener(sNode));
}
}
导航主要根据路线规划后的起始点进行导航就是经纬度,原先用OnGetGeoCoderResultListener这个接口去转换经纬度但是只能算一个后来写个工具类来把地址转换经纬度。
public static double latitude;
public static double Longitude;
public static ArrayList<Double> getAddress(String adressStr,Context context) {
Geocoder geocoder = new Geocoder(context);
ArrayList<Double> list=new ArrayList<Double>();
//根据地址获取地理位置信息
List<Address> addresses;
try {
addresses = geocoder.getFromLocationName(adressStr, 1);
if (addresses.size() > 0) {
Address address = addresses.get(0);
latitude = address.getLatitude();
Longitude=address.getLongitude();
list.add(latitude);
list.add(Longitude);
Log.e("经纬度:", latitude+""+Longitude);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
简单的实现定位规划导航bug有点多。
http://download.csdn.net/detail/wtq_domian/9571081