规划路线
private void guihualuxian() {
RouteSearch routeSearch;
routeSearch = new RouteSearch(this);
routeSearch.setRouteSearchListener(new RouteSearch.OnRouteSearchListener() {
@Override
public void onBusRouteSearched(BusRouteResult busRouteResult, int i) {
}
@Override
public void onDriveRouteSearched(DriveRouteResult result, int errorCode) {
aMap.clear();// 清理地图上的所有覆盖物
if (errorCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getPaths() != null) {
if (result.getPaths().size() > 0) {
final DrivePath drivePath = result.getPaths()
.get(0);
if (drivePath == null) {
return;
}
DrivingRouteOverlay drivingRouteOverlay = new DrivingRouteOverlay(
mContext, aMap, drivePath,
result.getStartPos(),
result.getTargetPos(), null);
drivingRouteOverlay.setNodeIconVisibility(false);//设置节点marker是否显示
drivingRouteOverlay.setIsColorfulline(true);//是否用颜色展示交通拥堵情况,默认true
drivingRouteOverlay.removeFromMap();
drivingRouteOverlay.addToMap();
drivingRouteOverlay.zoomToSpan();
// mBottomLayout.setVisibility(View.VISIBLE);
// int dis = (int) drivePath.getDistance();
// int dur = (int) drivePath.getDuration();
// String des = AMapUtil.getFriendlyTime(dur)+"("+AMapUtil.getFriendlyLength(dis)+")";
// mRotueTimeDes.setText(des);
// mRouteDetailDes.setVisibility(View.VISIBLE);
// int taxiCost = (int) mDriveRouteResult.getTaxiCost();
// mRouteDetailDes.setText("打车约"+taxiCost+"元");
// mBottomLayout.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// Intent intent = new Intent(mContext,
// DriveRouteDetailActivity.class);
// intent.putExtra("drive_path", drivePath);
// intent.putExtra("drive_result",
// mDriveRouteResult);
// startActivity(intent);
// }
// });
} else if (result != null && result.getPaths() == null) {
ToastUtil.show(mContext, R.string.no_result);
}
} else {
ToastUtil.show(mContext, R.string.no_result);
}
} else {
ToastUtil.showerror(mContext, errorCode);
}
}
@Override
public void onWalkRouteSearched(WalkRouteResult walkRouteResult, int i) {
}
@Override
public void onRideRouteSearched(RideRouteResult rideRouteResult, int i) {
}
});
RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(new LatLonPoint(jing, wei), latLonPoint);
RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, RouteSearch.BUS_DEFAULT, null, null, "");
routeSearch.calculateDriveRouteAsyn(query);
}
//坐标转换
private void zuobiaohuhuan() {
GeocodeSearch geocoderSearch;
//地理编码搜索GeocodeSearch
geocoderSearch = new GeocodeSearch(this);
geocoderSearch.setOnGeocodeSearchListener(new GeocodeSearch.OnGeocodeSearchListener() {
@Override
public void onRegeocodeSearched(RegeocodeResult regeocodeResult, int i) {
}
@Override//获取终点位置
public void onGeocodeSearched(GeocodeResult geocodeResult, int i) {
latLonPoint = geocodeResult.getGeocodeAddressList().get(0).getLatLonPoint();
guihualuxian();
}
});
// name表示地址,第二个参数表示查询城市,中文或者中文全拼,citycode、adcode
GeocodeQuery query = new GeocodeQuery(name, "");
geocoderSearch.getFromLocationNameAsyn(query);
}
定位
// 高德定位
public void dingWei() { //声明mlocationClient对象 AMapLocationClient mlocationClient; //声明mLocationOption对象 AMapLocationClientOption mLocationOption = null; mlocationClient = new AMapLocationClient(this); //初始化定位参数 mLocationOption = new AMapLocationClientOption(); //设置定位监听 mlocationClient.setLocationListener(new AMapLocationListener() { @Override public void onLocationChanged(AMapLocation amapLocation) { if (amapLocation != null) { if (amapLocation.getErrorCode() == 0) { //定位成功回调信息,设置相关消息 amapLocation.getLocationType();//获取当前定位结果来源,如网络定位结果,详见定位类型表 SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Date date = new Date(amapLocation.getTime()); String format = df.format(date);//定位时间 poiName=amapLocation.getPoiName() + "," + amapLocation.getAoiName()+"定位时间:"+format; start.setText(poiName); latitude = amapLocation.getLatitude();//获取纬度 longitude = amapLocation.getLongitude();//获取经度 float accuracy = amapLocation.getAccuracy();//获取精度信息 } else { //显示错误信息ErrCode是错误码,errInfo是错误信息,详见错误码表。 Log.e("AmapError", "location Error, ErrCode:" + amapLocation.getErrorCode() + ", errInfo:" + amapLocation.getErrorInfo()); } } } }); //设置定位模式为高精度模式,Battery_Saving为低功耗模式,Device_Sensors是仅设备模式 mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy); //设置定位间隔,单位毫秒,默认为2000ms mLocationOption.setInterval(2000); //设置定位参数 mlocationClient.setLocationOption(mLocationOption); // 此方法为每隔固定时间会发起一次定位请求,为了减少电量消耗或网络流量消耗, // 注意设置合适的定位时间的间隔(最小间隔支持为1000ms),并且在合适时间调用stopLocation()方法来取消定位请求 // 在定位结束后,在合适的生命周期调用onDestroy()方法 // 在单次定位情况下,定位无论成功与否,都无需调用stopLocation()方法移除请求,定位sdk内部会移除 //启动定位 mlocationClient.startLocation(); }
绘制蓝点
mMapView.onCreate(savedInstanceState); //初始化地图控制器对象 if (aMap == null) { aMap = mMapView.getMap(); } Intent intent = getIntent(); wei = intent.getDoubleExtra("wei", 0); jing = intent.getDoubleExtra("jing", 0); name = intent.getStringExtra("name"); lanDian();
//获取蓝点 private void lanDian() { myLocationStyle = new MyLocationStyle();//初始化定位蓝点样式类myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_LOCATION_ROTATE);//连续定位、且将视角移动到地图中心点,定位点依照设备方向旋转,并且会跟随设备移动。(1秒1次定位)如果不设置myLocationType,默认也会执行此种模式。 myLocationStyle.myLocationType(MyLocationStyle.LOCATION_TYPE_FOLLOW);//连续定位、且将视角移动到地图中心点,定位蓝点跟随设备移动。(1秒1次定位) // myLocationStyle.interval(2000); //设置连续定位模式下的定位间隔,只在连续定位模式下生效,单次定位模式下不会生效。单位为毫秒。 aMap.setMyLocationStyle(myLocationStyle);//设置定位蓝点的Style myLocationStyle.showMyLocation(true);//设置是否显示定位小蓝点,用于满足只想使用定位,不想使用定位小蓝点的场景,设置false以后图面上不再有定位蓝点的概念,但是会持续回调位置信息。 aMap.getUiSettings().setMyLocationButtonEnabled(true);//设置默认定位按钮是否显示,非必需设置。 aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false //设置SDK 自带定位消息监听 aMap.setOnMyLocationChangeListener(new AMap.OnMyLocationChangeListener() { @Override//获取我的位置 public void onMyLocationChange(Location location) { //维度 latitude = location.getLatitude(); //经度 longitude = location.getLongitude(); } }); }