结束行程,可以查看行驶轨迹
百度地图开放平台注册不需要公司营业执照什么的,个人就能注册,地址:http://lbsyun.baidu.com。
首先建议大家吧百度地图API的demo下载下来研究一下,它包含我们用到的所有知识点,你再把资源整合一下就行了。SDK的Demo下载地址:http://lbsyun.baidu.com/sdk/download?selected=mapsdk_basicmap,mapsdk_searchfunction,mapsdk_lbscloudsearch,mapsdk_calculationtool,mapsdk_radar
<span style="font-size:14px;">public class RouteDetailActivity extends BaseActivity {
private MapView route_detail_mapview;
BaiduMap routeBaiduMap;
private BitmapDescriptor startBmp, endBmp;
private MylocationListener mlistener;
LocationClient mlocationClient;
TextView total_time, total_distance, total_price;
public ArrayList<RoutePoint> routePoints;
public static boolean completeRoute = false;
String time, distance, price, routePointsStr;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_route_detail);
setStatusBar();
route_detail_mapview = (MapView) findViewById(R.id.route_detail_mapview);
total_time = (TextView) findViewById(R.id.total_time);
total_distance = (TextView) findViewById(R.id.total_distance);
total_price = (TextView) findViewById(R.id.total_pricce);
routeBaiduMap = route_detail_mapview.getMap();
route_detail_mapview.showZoomControls(false);
startBmp = BitmapDescriptorFactory.fromResource(R.mipmap.route_start);
endBmp = BitmapDescriptorFactory.fromResource(R.mipmap.route_end);
initMap();
Intent intent = getIntent();
String time = intent.getStringExtra("totalTime");
String distance = intent.getStringExtra("totalDistance");
String price = intent.getStringExtra("totalPrice");
routePointsStr = intent.getStringExtra("routePoints");
routePoints = new Gson().fromJson(routePointsStr, new TypeToken<List<RoutePoint>>() {
}.getType());
List<LatLng> points = new ArrayList<LatLng>();
for (int i = 0; i < routePoints.size(); i++) {
RoutePoint point = routePoints.get(i);
LatLng latLng = new LatLng(point.getRouteLat(), point.getRouteLng());
Log.d("gaolei", "point.getRouteLat()----show-----" + point.getRouteLat());
Log.d("gaolei", "point.getRouteLng()----show-----" + point.getRouteLng());
points.add(latLng);
}
if (points.size() > 2) {
OverlayOptions ooPolyline = new PolylineOptions().width(10)
.color(0xFF36D19D).points(points);
routeBaiduMap.addOverlay(ooPolyline);
RoutePoint startPoint = routePoints.get(0);
LatLng startPosition = new LatLng(startPoint.getRouteLat(), startPoint.getRouteLng());
MapStatus.Builder builder = new MapStatus.Builder();
builder.target(startPosition).zoom(18.0f);
routeBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
RoutePoint endPoint = routePoints.get(routePoints.size() - 1);
LatLng endPosition = new LatLng(endPoint.getRouteLat(), endPoint.getRouteLng());
addOverLayout(startPosition, endPosition);
}
total_time.setText("骑行时长:" + time + "分钟");
total_distance.setText("骑行距离:" + distance + "米");
total_price.setText("余额支付:" + price + "元");
}
private void initMap() {
mlocationClient = new LocationClient(this);
// mlistener = new MylocationListener();
// mlocationClient.registerLocationListener(mlistener);
LocationClientOption mOption = new LocationClientOption();
//设置坐标类型
mOption.setCoorType("bd09ll");
//设置是否需要地址信息,默认为无地址
mOption.setIsNeedAddress(true);
//设置是否打开gps进行定位
mOption.setOpenGps(true);
//设置扫描间隔,单位是毫秒 当<1000(1s)时,定时定位无效
int span = 10000;
mOption.setScanSpan(span);
//设置 LocationClientOption
mlocationClient.setLocOption(mOption);
if (!mlocationClient.isStarted()) {
mlocationClient.start();
}
UiSettings settings=routeBaiduMap.getUiSettings();
settings.setScrollGesturesEnabled(true);
}
public class MylocationListener implements BDLocationListener {
//定位请求回调接口
private boolean isFirstIn = true;
//定位请求回调函数,这里面会得到定位信息
@Override
public void onReceiveLocation(BDLocation bdLocation) {
//判断是否为第一次定位,是的话需要定位到用户当前位置
if (isFirstIn) {
Log.d("gaolei", "onReceiveLocation----------RouteDetail-----" + bdLocation.getAddrStr());
// LatLng currentLL = new LatLng(bdLocation.getLatitude(),
// bdLocation.getLongitude());
//// startNodeStr = PlanNode.withLocation(currentLL);
// MapStatus.Builder builder = new MapStatus.Builder();
// builder.target(currentLL).zoom(18.0f);
// routeBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
isFirstIn = false;
}
}
}
private void addOverLayout(LatLng startPosition, LatLng endPosition) {
//先清除图层
// mBaiduMap.clear();
// 定义Maker坐标点
// 构建MarkerOption,用于在地图上添加Marker
MarkerOptions options = new MarkerOptions().position(startPosition)
.icon(startBmp);
// 在地图上添加Marker,并显示
routeBaiduMap.addOverlay(options);
MarkerOptions options2 = new MarkerOptions().position(endPosition)
.icon(endBmp);
// 在地图上添加Marker,并显示
routeBaiduMap.addOverlay(options2);
}
public void onDestroy() {
super.onDestroy();
routeBaiduMap.setMyLocationEnabled(false);
mlocationClient.stop();
completeRoute = false;
}
public void finishActivity(View view) {
completeRoute = true;
finish();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN) {
completeRoute = true;
finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}</span>