1、只许向MapAcitvity传一个Intent值 值为一个坐标 去哪里的坐标即可。
2、自动定位到当前位置。
3、demo下载地址 http://download.csdn.net/detail/u012303938/9173285
代码如下 Mapactivity
package com.example.daohang; import android.app.Activity; import android.content.Context; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.BaseAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; import com.baidu.location.BDLocation; import com.baidu.location.BDLocationListener; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import com.baidu.mapapi.map.BaiduMap; import com.baidu.mapapi.map.BitmapDescriptor; import com.baidu.mapapi.map.BitmapDescriptorFactory; import com.baidu.mapapi.map.MapStatusUpdate; import com.baidu.mapapi.map.MapStatusUpdateFactory; import com.baidu.mapapi.map.MapView; import com.baidu.mapapi.map.MyLocationData; import com.baidu.mapapi.model.LatLng; import com.baidu.mapapi.overlayutil.DrivingRouteOverlay; import com.baidu.mapapi.overlayutil.TransitRouteOverlay; import com.baidu.mapapi.overlayutil.WalkingRouteOverlay; import com.baidu.mapapi.search.core.SearchResult; import com.baidu.mapapi.search.poi.PoiSearch; import com.baidu.mapapi.search.route.DrivingRouteLine; import com.baidu.mapapi.search.route.DrivingRoutePlanOption; import com.baidu.mapapi.search.route.DrivingRouteResult; import com.baidu.mapapi.search.route.OnGetRoutePlanResultListener; import com.baidu.mapapi.search.route.PlanNode; import com.baidu.mapapi.search.route.RoutePlanSearch; import com.baidu.mapapi.search.route.TransitRouteLine; import com.baidu.mapapi.search.route.TransitRoutePlanOption; import com.baidu.mapapi.search.route.TransitRouteResult; import com.baidu.mapapi.search.route.WalkingRouteLine; import com.baidu.mapapi.search.route.WalkingRoutePlanOption; import com.baidu.mapapi.search.route.WalkingRouteResult; import java.util.List; public class MapActivity extends Activity { private LocationClient mLocationClient; public BDLocationListener myListener = new MyLocationListener(); private MapView mMapView = null; private BaiduMap mBaiduMap; private BitmapDescriptor mCurrentMarker; boolean isFirstLoc = true; private PoiSearch mPoiSearch; private RoutePlanSearch mSearch; boolean useDefaultIcon = false; private String addr=""; private String city="",local=""; private LatLng latLngstar,latLngend; private Context context; private LinearLayout ll_bus,ll_driver,ll_walk,ll_bottom; private RelativeLayout rl_map; private Location location; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.map); context=this; location= (Location) getIntent().getSerializableExtra("end"); double lat=Double.valueOf(location.getLat()); double lan=Double.valueOf(location.getLng()); latLngend=new LatLng(lat,lan); //Toast.makeText(context,local,Toast.LENGTH_SHORT).show(); rl_map= (RelativeLayout) findViewById(R.id.rl_map); /** * 使用地图sdk前需先初始化BMapManager. * BMapManager是全局的,可为多个MapView共用,它需要地图模块创建前创建, * 并在地图地图模块销毁后销毁,只要还有地图模块在使用,BMapManager就不应该销毁 */ //获取地图控件引用 mMapView = (MapView) findViewById(R.id.bmapView); mBaiduMap = mMapView.getMap(); // 开启定位图层 mBaiduMap.setMyLocationEnabled(true); DemoApplication app = (DemoApplication) this.getApplication(); mLocationClient = new LocationClient(app); //声明LocationClient类 initLocation(); mLocationClient.registerLocationListener(myListener); //注册监听函数 mLocationClient.start(); mPoiSearch = PoiSearch.newInstance(); mSearch = RoutePlanSearch.newInstance(); OnGetRoutePlanResultListener listener=new OnGetRoutePlanResultListener() { @Override public void onGetWalkingRouteResult(final WalkingRouteResult result) { if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(MapActivity.this, "抱歉,步走未找到结果", Toast.LENGTH_SHORT).show(); } if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) { //起终点或途经点地址有岐义,通过以下接口获取建议查询信息 //result.getSuggestAddrInfo() return; } if (result.error == SearchResult.ERRORNO.NO_ERROR) { final WalkingRouteOverlay overlay=new WalkingRouteOverlay(mBaiduMap); mBaiduMap.setOnMarkerClickListener(overlay); List<WalkingRouteLine> list=result.getRouteLines(); String str=list.get(0).getAllStep().get(0).getInstructions(); //Toast.makeText(MapActivity.this,list.size()+"",Toast.LENGTH_SHORT).show(); ll_bottom= (LinearLayout) findViewById(R.id.ll_bottom); View view=View.inflate(context,R.layout.luxian,null); ListView listView= (ListView) view.findViewById(R.id.listView2); final PopupWindow popupWindow=new PopupWindow(view); popupWindow.setWidth(rl_map.getWidth()); popupWindow.setHeight(rl_map.getHeight()-ll_bottom.getMeasuredHeight()); MYadapter1 mYadapter=new MYadapter1(list); listView.setAdapter(mYadapter); popupWindow.setFocusable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable()); int[] location = new int[2]; ll_bottom.getLocationOnScreen(location); popupWindow.showAtLocation(ll_bottom, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight()); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { popupWindow.dismiss(); mBaiduMap.clear(); overlay.setData(result.getRouteLines().get(position)); overlay.addToMap(); overlay.zoomToSpan(); } }); } } @Override public void onGetTransitRouteResult(final TransitRouteResult result) { if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(MapActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show(); } if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) { //起终点或途经点地址有岐义,通过以下接口获取建议查询信息 //result.getSuggestAddrInfo() return; } if (result.error == SearchResult.ERRORNO.NO_ERROR) { final TransitRouteOverlay overlay = new MyTransitRouteOverlay(mBaiduMap); mBaiduMap.setOnMarkerClickListener(overlay); List<TransitRouteLine> list=result.getRouteLines(); String str=list.get(0).getAllStep().get(0).getInstructions(); //Toast.makeText(MapActivity.this,list.size()+"",Toast.LENGTH_SHORT).show(); ll_bottom= (LinearLayout) findViewById(R.id.ll_bottom); View view=View.inflate(context,R.layout.luxian,null); ListView listView= (ListView) view.findViewById(R.id.listView2); final PopupWindow popupWindow=new PopupWindow(view); popupWindow.setWidth(rl_map.getWidth()); popupWindow.setHeight(rl_map.getHeight()-ll_bottom.getMeasuredHeight()); MYadapter mYadapter=new MYadapter(list); listView.setAdapter(mYadapter); popupWindow.setFocusable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable()); int[] location = new int[2]; ll_bottom.getLocationOnScreen(location); popupWindow.showAtLocation(ll_bottom, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight()); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { popupWindow.dismiss(); mBaiduMap.clear(); overlay.setData(result.getRouteLines().get(position)); overlay.addToMap(); overlay.zoomToSpan(); } }); } } @Override public void onGetDrivingRouteResult(final DrivingRouteResult result) { if (result == null || result.error != SearchResult.ERRORNO.NO_ERROR) { Toast.makeText(MapActivity.this, "抱歉,未找到结果", Toast.LENGTH_SHORT).show(); } if (result.error == SearchResult.ERRORNO.AMBIGUOUS_ROURE_ADDR) { //起终点或途经点地址有岐义,通过以下接口获取建议查询信息 //result.getSuggestAddrInfo() return; } if (result.error == SearchResult.ERRORNO.NO_ERROR) { final DrivingRouteOverlay overlay=new DrivingRouteOverlay(mBaiduMap); mBaiduMap.setOnMarkerClickListener(overlay); List<DrivingRouteLine> list=result.getRouteLines(); String str=list.get(0).getAllStep().get(0).getInstructions(); //Toast.makeText(MapActivity.this,list.size()+"",Toast.LENGTH_SHORT).show(); ll_bottom= (LinearLayout) findViewById(R.id.ll_bottom); View view=View.inflate(context,R.layout.luxian,null); ListView listView= (ListView) view.findViewById(R.id.listView2); final PopupWindow popupWindow=new PopupWindow(view); popupWindow.setWidth(rl_map.getWidth()); popupWindow.setHeight(rl_map.getHeight()-ll_bottom.getMeasuredHeight()); MYadapter2 mYadapter=new MYadapter2(list); listView.setAdapter(mYadapter); popupWindow.setFocusable(true); popupWindow.setBackgroundDrawable(new BitmapDrawable()); int[] location = new int[2]; ll_bottom.getLocationOnScreen(location); popupWindow.showAtLocation(ll_bottom, Gravity.NO_GRAVITY, location[0], location[1]-popupWindow.getHeight()); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { popupWindow.dismiss(); mBaiduMap.clear(); overlay.setData(result.getRouteLines().get(position)); overlay.addToMap(); overlay.zoomToSpan(); } }); } } }; mSearch.setOnGetRoutePlanResultListener(listener); ll_bus= (LinearLayout) findViewById(R.id.ll_bus); ll_bus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PlanNode stNode = PlanNode.withLocation(latLngstar); PlanNode enNode = PlanNode.withLocation(latLngend); //Toast.makeText(context,latLngstar+":"+latLngend+":"+local,Toast.LENGTH_SHORT).show(); mSearch.transitSearch((new TransitRoutePlanOption()) .from(stNode) .city(city) .to(enNode)); } }); ll_driver= (LinearLayout) findViewById(R.id.ll_driver); ll_driver.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PlanNode stNode = PlanNode.withLocation(latLngstar); PlanNode enNode = PlanNode.withLocation(latLngend); mSearch.drivingSearch((new DrivingRoutePlanOption()) .from(stNode).to(enNode)); } }); ll_walk= (LinearLayout) findViewById(R.id.ll_walk); ll_walk.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { PlanNode stNode = PlanNode.withLocation(latLngstar); PlanNode enNode = PlanNode.withLocation(latLngend); mSearch.walkingSearch((new WalkingRoutePlanOption()).from(stNode).to(enNode)); } }); //开启交通图 // mBaiduMap.setTrafficEnabled(true); } //定制RouteOverly private class MyDrivingRouteOverlay extends DrivingRouteOverlay { public MyDrivingRouteOverlay(BaiduMap baiduMap) { super(baiduMap); } @Override public BitmapDescriptor getStartMarker() { if (useDefaultIcon) { return BitmapDescriptorFactory.fromResource(R.drawable.icon_st); } return null; } @Override public BitmapDescriptor getTerminalMarker() { if (useDefaultIcon) { return BitmapDescriptorFactory.fromResource(R.drawable.icon_en); } return null; } } private class MyWalkingRouteOverlay extends WalkingRouteOverlay { public MyWalkingRouteOverlay(BaiduMap baiduMap) { super(baiduMap); } @Override public BitmapDescriptor getStartMarker() { if (useDefaultIcon) { return BitmapDescriptorFactory.fromResource(R.drawable.icon_st); } return null; } @Override public BitmapDescriptor getTerminalMarker() { if (useDefaultIcon) { return BitmapDescriptorFactory.fromResource(R.drawable.icon_en); } return null; } } private class MyTransitRouteOverlay extends TransitRouteOverlay { public MyTransitRouteOverlay(BaiduMap baiduMap) { super(baiduMap); } @Override public BitmapDescriptor getStartMarker() { if (useDefaultIcon) { return BitmapDescriptorFactory.fromResource(R.drawable.icon_st); } return null; } @Override public BitmapDescriptor getTerminalMarker() { if (useDefaultIcon) { return BitmapDescriptorFactory.fromResource(R.drawable.icon_en); } return null; } } @Override protected void onDestroy() { //在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理 mLocationClient.stop(); // 关闭定位图层 mBaiduMap.setMyLocationEnabled(false); mPoiSearch.destroy(); mSearch.destroy(); mMapView.onDestroy(); mMapView = null; super.onDestroy(); } @Override protected void onResume() { super.onResume(); //在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理 mMapView.onResume(); } @Override protected void onPause() { super.onPause(); //在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理 mMapView.onPause(); } public class MyLocationListener implements BDLocationListener { @Override public void onReceiveLocation(BDLocation location) { if (location == null || mMapView == null) return; MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) // 此处设置开发者获取到的方向信息,顺时针0-360 .direction(100).latitude(location.getLatitude()) .longitude(location.getLongitude()).build(); mBaiduMap.setMyLocationData(locData); addr=location.getAddrStr(); city=location.getCity(); latLngstar=new LatLng(location.getLatitude(),location.getLongitude()); // 修改为自定义marker // mCurrentMarker = BitmapDescriptorFactory // .fromResource(R.drawable.icon_marka); // mBaiduMap // .setMyLocationConfigeration(new MyLocationConfiguration( // MyLocationConfiguration.LocationMode.NORMAL, true, mCurrentMarker)); if (isFirstLoc) { isFirstLoc = false; LatLng ll = new LatLng(location.getLatitude(), location.getLongitude()); MapStatusUpdate u = MapStatusUpdateFactory.newLatLng(ll); mBaiduMap.animateMapStatus(u); } // 当不需要定位图层时关闭定位图层 // mBaiduMap.setMyLocationEnabled(false); //Receive Location StringBuffer sb = new StringBuffer(256); sb.append("time : "); sb.append(location.getTime()); sb.append("\nerror code : "); sb.append(location.getLocType()); sb.append("\nlatitude : "); sb.append(location.getLatitude()); sb.append("\nlontitude : "); sb.append(location.getLongitude()); sb.append("\nradius : "); sb.append(location.getRadius()); if (location.getLocType() == BDLocation.TypeGpsLocation) {// GPS定位结果 sb.append("\nspeed : "); sb.append(location.getSpeed());// 单位:公里每小时 sb.append("\nsatellite : "); sb.append(location.getSatelliteNumber()); sb.append("\nheight : "); sb.append(location.getAltitude());// 单位:米 sb.append("\ndirection : "); sb.append(location.getDirection());// 单位度 sb.append("\naddr : "); sb.append(location.getAddrStr()); sb.append("\ndescribe : "); sb.append("gps定位成功"); } else if (location.getLocType() == BDLocation.TypeNetWorkLocation) {// 网络定位结果 sb.append("\naddr : "); sb.append(location.getAddrStr()); //运营商信息 sb.append("\noperationers : "); sb.append(location.getOperators()); sb.append("\ndescribe : "); sb.append("网络定位成功"); } else if (location.getLocType() == BDLocation.TypeOffLineLocation) {// 离线定位结果 sb.append("\ndescribe : "); sb.append("离线定位成功,离线定位结果也是有效的"); } else if (location.getLocType() == BDLocation.TypeServerError) { sb.append("\ndescribe : "); sb.append("服务端网络定位失败,可以反馈IMEI号和大体定位时间到loc-bugs@baidu.com,会有人追查原因"); } else if (location.getLocType() == BDLocation.TypeNetWorkException) { sb.append("\ndescribe : "); sb.append("网络不同导致定位失败,请检查网络是否通畅"); } else if (location.getLocType() == BDLocation.TypeCriteriaException) { sb.append("\ndescribe : "); sb.append("无法获取有效定位依据导致定位失败,一般是由于手机的原因,处于飞行模式下一般会造成这种结果,可以试着重启手机"); } sb.append("\nlocationdescribe : "); // sb.append(location.getLocationDescribe());// 位置语义化信息 // List<Poi> list = location.getPoiList();// POI数据 // if (list != null) { // sb.append("\npoilist size = : "); // sb.append(list.size()); // for (Poi p : list) { // sb.append("\npoi= : "); // sb.append(p.getId() + " " + p.getName() + " " + p.getRank()); // } } // Log.i("BaiduLocationApiDem", sb.toString()); } private void initLocation(){ LocationClientOption option = new LocationClientOption(); option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy );//可选,默认高精度,设置定位模式,高精度,低功耗,仅设备 option.setCoorType("bd09ll");//可选,默认gcj02,设置返回的定位结果坐标系 int span=1000; option.setScanSpan(span);//可选,默认0,即仅定位一次,设置发起定位请求的间隔需要大于等于1000ms才是有效的 option.setIsNeedAddress(true);//可选,设置是否需要地址信息,默认不需要 option.setOpenGps(true);//可选,默认false,设置是否使用gps option.setLocationNotify(true);//可选,默认false,设置是否当gps有效时按照1S1次频率输出GPS结果 //option.setIsNeedLocationDescribe(true);//可选,默认false,设置是否需要位置语义化结果,可以在BDLocation.getLocationDescribe里得到,结果类似于“在北京*附近” //option.setIsNeedLocationPoiList(true);//可选,默认false,设置是否需要POI结果,可以在BDLocation.getPoiList里得到 option.setIgnoreKillProcess(false);//可选,默认false,定位SDK内部是一个SERVICE,并放到了独立进程,设置是否在stop的时候杀死这个进程,默认杀死 option.SetIgnoreCacheException(false);//可选,默认false,设置是否收集CRASH信息,默认收集 // option.setEnableSimulateGps(false);//可选,默认false,设置是否需要过滤gps仿真结果,默认需要 mLocationClient.setLocOption(option); } private class MYadapter extends BaseAdapter{ List<TransitRouteLine> list; public MYadapter( List<TransitRouteLine> list) { this.list=list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null){ convertView=View.inflate(context,R.layout.luxian_item,null); } TextView title= (TextView) convertView.findViewById(R.id.title); TextView time= (TextView) convertView.findViewById(R.id.time); TextView sum= (TextView) convertView.findViewById(R.id.sum); List<TransitRouteLine.TransitStep> routeStep=list.get(position).getAllStep(); String str=""; int du=0,di=0; for(int i=0;i<routeStep.size();i++){ str+=routeStep.get(i).getInstructions(); du+=routeStep.get(i).getDuration(); di+=routeStep.get(i).getDistance(); } // Toast.makeText(context,list.get(position).getAllStep().get(0).getInstructions(),Toast.LENGTH_SHORT).show(); title.setText(str); // String str=""; // int num=0; // for(int i=0;i<routeStep.size();i++){ // str+= routeStep.get(i).getVehicleInfo().getTitle(); // num+=routeStep.get(i).getVehicleInfo().getPassStationNum(); // } // title.setText(str); time.setText((du/60)+"分"); sum.setText(di/1000+"公里左右"); return convertView; } } private class MYadapter1 extends BaseAdapter{ List<WalkingRouteLine> list; public MYadapter1( List<WalkingRouteLine> list) { this.list=list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null){ convertView=View.inflate(context,R.layout.luxian_item,null); } TextView title= (TextView) convertView.findViewById(R.id.title); TextView time= (TextView) convertView.findViewById(R.id.time); TextView sum= (TextView) convertView.findViewById(R.id.sum); List<WalkingRouteLine.WalkingStep> routeStep=list.get(position).getAllStep(); String str=""; int du=0,di=0; for(int i=0;i<routeStep.size();i++){ str+=routeStep.get(i).getInstructions(); du+=routeStep.get(i).getDuration(); di+=routeStep.get(i).getDistance(); } // Toast.makeText(context,list.get(position).getAllStep().get(0).getInstructions(),Toast.LENGTH_SHORT).show(); title.setText(str); // String str=""; // int num=0; // for(int i=0;i<routeStep.size();i++){ // str+= routeStep.get(i).getVehicleInfo().getTitle(); // num+=routeStep.get(i).getVehicleInfo().getPassStationNum(); // } // title.setText(str); time.setText((du/60)+"分"); sum.setText(di/1000+"公里左右"); return convertView; } } private class MYadapter2 extends BaseAdapter{ List<DrivingRouteLine> list; public MYadapter2( List<DrivingRouteLine> list) { this.list=list; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { if(convertView==null){ convertView=View.inflate(context,R.layout.luxian_item,null); } TextView title= (TextView) convertView.findViewById(R.id.title); TextView time= (TextView) convertView.findViewById(R.id.time); TextView sum= (TextView) convertView.findViewById(R.id.sum); List<DrivingRouteLine.DrivingStep> routeStep=list.get(position).getAllStep(); String str=""; int du=0,di=0; for(int i=0;i<routeStep.size();i++){ str+=routeStep.get(i).getInstructions(); du+=routeStep.get(i).getDuration(); di+=routeStep.get(i).getDistance(); } // Toast.makeText(context,list.get(position).getAllStep().get(0).getInstructions(),Toast.LENGTH_SHORT).show(); title.setText(str); // String str=""; // int num=0; // for(int i=0;i<routeStep.size();i++){ // str+= routeStep.get(i).getVehicleInfo().getTitle(); // num+=routeStep.get(i).getVehicleInfo().getPassStationNum(); // } // title.setText(str); time.setText((du/60)+"分"); sum.setText(di/1000+"公里左右"); return convertView; } } }