feat: 增加路径规划公共方法
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package com.navinfo.outdoor.util;
|
package com.navinfo.outdoor.util;
|
||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
|
import android.util.Log;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.RadioButton;
|
import android.widget.RadioButton;
|
||||||
import android.widget.RadioGroup;
|
import android.widget.RadioGroup;
|
||||||
@@ -14,16 +15,33 @@ import com.navinfo.outdoor.R;
|
|||||||
import com.navinfo.outdoor.api.Constant;
|
import com.navinfo.outdoor.api.Constant;
|
||||||
import com.tencent.map.navi.CalcRouteCallback;
|
import com.tencent.map.navi.CalcRouteCallback;
|
||||||
import com.tencent.map.navi.TencentNaviManager;
|
import com.tencent.map.navi.TencentNaviManager;
|
||||||
|
import com.tencent.map.navi.TencentRouteSearchCallback;
|
||||||
import com.tencent.map.navi.car.CarRouteSearchOptions;
|
import com.tencent.map.navi.car.CarRouteSearchOptions;
|
||||||
import com.tencent.map.navi.car.TencentCarNaviManager;
|
import com.tencent.map.navi.car.TencentCarNaviManager;
|
||||||
import com.tencent.map.navi.data.CalcRouteResult;
|
import com.tencent.map.navi.data.CalcRouteResult;
|
||||||
import com.tencent.map.navi.data.NaviPoi;
|
import com.tencent.map.navi.data.NaviPoi;
|
||||||
|
import com.tencent.map.navi.data.RouteColors;
|
||||||
|
import com.tencent.map.navi.data.RouteData;
|
||||||
|
import com.tencent.map.navi.data.TrafficItem;
|
||||||
|
import com.tencent.map.navi.ride.RideRouteSearchOptions;
|
||||||
import com.tencent.map.navi.ride.TencentRideNaviManager;
|
import com.tencent.map.navi.ride.TencentRideNaviManager;
|
||||||
|
import com.tencent.map.navi.walk.TencentWalkNaviManager;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.CameraUpdateFactory;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.TencentMap;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.model.BitmapDescriptorFactory;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.model.LatLng;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.model.LatLngBounds;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.model.MarkerOptions;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.model.Polyline;
|
||||||
|
import com.tencent.tencentmap.mapsdk.maps.model.PolylineOptions;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class NaviUtils {
|
public class NaviUtils {
|
||||||
private static NaviUtils instance;
|
private static NaviUtils instance;
|
||||||
|
private Context mContext;
|
||||||
|
private TencentMap tencentMap;
|
||||||
public static NaviUtils getInstance() {
|
public static NaviUtils getInstance() {
|
||||||
if (instance == null) {
|
if (instance == null) {
|
||||||
instance = new NaviUtils();
|
instance = new NaviUtils();
|
||||||
@@ -31,6 +49,11 @@ public class NaviUtils {
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void init(Context mContext, TencentMap tencentMap) {
|
||||||
|
this.mContext = mContext;
|
||||||
|
this.tencentMap = tencentMap;
|
||||||
|
}
|
||||||
|
|
||||||
public void selectNaviType(AppCompatActivity mContext, SelectNaviTypeListener selectNaviTypeListener) {
|
public void selectNaviType(AppCompatActivity mContext, SelectNaviTypeListener selectNaviTypeListener) {
|
||||||
CustomDialog selectNaviTypeDialog = CustomDialog.build(mContext, R.layout.dialog_select_navi_type, new CustomDialog.OnBindView() {
|
CustomDialog selectNaviTypeDialog = CustomDialog.build(mContext, R.layout.dialog_select_navi_type, new CustomDialog.OnBindView() {
|
||||||
@Override
|
@Override
|
||||||
@@ -100,24 +123,177 @@ public class NaviUtils {
|
|||||||
* */
|
* */
|
||||||
public void searchRoute(Constant.NAV_TYPE nav_type, TencentNaviManager naviManager,
|
public void searchRoute(Constant.NAV_TYPE nav_type, TencentNaviManager naviManager,
|
||||||
NaviPoi start, NaviPoi end) throws Exception {
|
NaviPoi start, NaviPoi end) throws Exception {
|
||||||
if (nav_type == Constant.NAV_TYPE.CAR) {
|
if (nav_type == Constant.NAV_TYPE.CAR) { // 驾车模式
|
||||||
TencentCarNaviManager carNaviManager = (TencentCarNaviManager)naviManager;
|
TencentCarNaviManager carNaviManager = (TencentCarNaviManager)naviManager;
|
||||||
|
|
||||||
CarRouteSearchOptions options = CarRouteSearchOptions.create();
|
CarRouteSearchOptions options = CarRouteSearchOptions.create();
|
||||||
carNaviManager.searchRoute(start, end, new ArrayList<>(), options, new CalcRouteCallback() {
|
carNaviManager.searchRoute(start, end, new ArrayList<>(), options, routeSearchCallback);
|
||||||
@Override
|
} else if (nav_type == Constant.NAV_TYPE.RIDE) { // 骑行模式
|
||||||
public void onCalcRouteSuccess(CalcRouteResult calcRouteResult) {
|
TencentRideNaviManager rideNaviManager = (TencentRideNaviManager)naviManager;
|
||||||
// 路径规划成功
|
RideRouteSearchOptions options = RideRouteSearchOptions.create();
|
||||||
}
|
rideNaviManager.searchRoute(start, end, options, routeSearchCallback);
|
||||||
|
} else { // 步行模式
|
||||||
@Override
|
TencentWalkNaviManager walkNaviManager = (TencentWalkNaviManager) naviManager;
|
||||||
public void onCalcRouteFailure(CalcRouteResult calcRouteResult) {
|
walkNaviManager.searchRoute(start, end, routeSearchCallback);
|
||||||
// 路径规划失败
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 算路回调
|
||||||
|
*/
|
||||||
|
private TencentRouteSearchCallback routeSearchCallback = new TencentRouteSearchCallback() {
|
||||||
|
@Override
|
||||||
|
public void onRouteSearchFailure(int i, String s) {
|
||||||
|
//i错误码提示:1001为网络错误,1002为无网络,2001为返回数据无效(或空),2002为起终点参数错误,2003为途经点参数错误,2004为吸附失败,2005为算路失败,2999为服务器内部错误
|
||||||
|
Log.e("route","error:"+i);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onRouteSearchSuccess(ArrayList<RouteData> arrayList) {
|
||||||
|
// 添加道路Route到地图,默认选取第一条
|
||||||
|
addRoutes(arrayList);
|
||||||
|
zoomToRoute(arrayList.get(0));
|
||||||
|
// addMarkerStart(start.getLatitude(),start.getLongitude());
|
||||||
|
// addMarkerDestination(dest.getLatitude(),dest.getLongitude());
|
||||||
|
// addMarkerPass(wayPoints);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCalcRouteSuccess(CalcRouteResult calcRouteResult) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCalcRouteFailure(CalcRouteResult calcRouteResult) {
|
||||||
|
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
private void addMarkerStart(double latitude, double longitude) {
|
||||||
|
LatLng startPoint = new LatLng(latitude, longitude);
|
||||||
|
tencentMap.addMarker(new MarkerOptions(startPoint)
|
||||||
|
.icon(BitmapDescriptorFactory.fromAsset("navi_marker_start.png"))
|
||||||
|
.anchor(0.5f, 1));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addMarkerDestination(double latitude, double longitude) {
|
||||||
|
|
||||||
|
LatLng destinationPoint = new LatLng(latitude, longitude);
|
||||||
|
tencentMap.addMarker(new MarkerOptions(destinationPoint)
|
||||||
|
.icon(BitmapDescriptorFactory.fromAsset("navi_marker_end.png"))
|
||||||
|
.anchor(0.5f, 1));
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addMarkerPass(ArrayList<NaviPoi> wayPoints) {
|
||||||
|
|
||||||
|
for (NaviPoi wayPoint : wayPoints) {
|
||||||
|
LatLng destinationPoint = new LatLng(wayPoint.getLatitude(), wayPoint.getLongitude());
|
||||||
|
tencentMap.addMarker(new MarkerOptions(destinationPoint)
|
||||||
|
.icon(BitmapDescriptorFactory.fromAsset("navi_marker_pass.png"))
|
||||||
|
.anchor(0.5f, 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addRoutes(ArrayList<RouteData> mRouteDatas) {
|
||||||
|
RouteData routeData = mRouteDatas.get(0);
|
||||||
|
ArrayList<TrafficItem> traffics = getTrafficItemsFromList(routeData.getTrafficIndexList());
|
||||||
|
List<LatLng> mRoutePoints = routeData.getRoutePoints();
|
||||||
|
// 点的个数
|
||||||
|
int pointSize = mRoutePoints.size();
|
||||||
|
// 路段总数 三个index是一个路况单元,分别为:路况级别,起点,终点
|
||||||
|
int trafficSize = traffics.size();
|
||||||
|
// 路段index所对应的颜色值数组
|
||||||
|
int[] trafficColors = new int[pointSize];
|
||||||
|
// 路段index数组
|
||||||
|
int[] trafficColorsIndex = new int[pointSize];
|
||||||
|
int pointStart = 0;
|
||||||
|
int pointEnd = 0;
|
||||||
|
int trafficColor = 0;
|
||||||
|
int index = 0;
|
||||||
|
for (int j = 0; j < trafficSize; j++) {
|
||||||
|
pointStart = traffics.get(j).getFromIndex();
|
||||||
|
// 规避错乱数据
|
||||||
|
if (pointStart < pointEnd) {
|
||||||
|
pointStart = pointEnd;
|
||||||
|
}
|
||||||
|
pointEnd = traffics.get(j).getToIndex();
|
||||||
|
trafficColor = getTrafficColorByCode(traffics.get(j).getTraffic());
|
||||||
|
|
||||||
|
for (int k = pointStart; k < pointEnd || k == pointSize - 1; k++) {
|
||||||
|
trafficColors[index] = trafficColor;
|
||||||
|
trafficColorsIndex[index] = index;
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
PolylineOptions options = new PolylineOptions()
|
||||||
|
.addAll(routeData.getRoutePoints())
|
||||||
|
.width(15)
|
||||||
|
.arrow(true)
|
||||||
|
.colors(trafficColors, trafficColorsIndex)
|
||||||
|
.zIndex(10);
|
||||||
|
|
||||||
|
Polyline polyline = tencentMap.addPolyline(options);
|
||||||
|
|
||||||
|
}
|
||||||
|
private void zoomToRoute(RouteData routeData) {
|
||||||
|
int marginLeft = mContext.getResources().getDimensionPixelSize(R.dimen.navigation_line_margin_left);
|
||||||
|
int marginTop = mContext.getResources().getDimensionPixelSize(R.dimen.navigation_line_margin_top);
|
||||||
|
int marginRight = mContext.getResources().getDimensionPixelSize(R.dimen.navigation_line_margin_right);
|
||||||
|
int marginBottom = mContext.getResources().getDimensionPixelSize(R.dimen.navigation_line_margin_bottom);
|
||||||
|
LatLngBounds.Builder builder = new LatLngBounds.Builder();
|
||||||
|
|
||||||
|
builder.include(routeData.getRoutePoints());
|
||||||
|
LatLngBounds bounds = builder.build();
|
||||||
|
tencentMap.moveCamera(CameraUpdateFactory.newLatLngBoundsRect(bounds, marginLeft, marginRight, marginTop, marginBottom));
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private int getTrafficColorByCode(int type) {
|
||||||
|
int color = 0xFFFFFFFF;
|
||||||
|
switch (type) {
|
||||||
|
case 0:
|
||||||
|
// 路况标签-畅通
|
||||||
|
// 绿色
|
||||||
|
color = RouteColors.COLOR_MAIN_DAY_SMOOTH;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
// 路况标签-缓慢
|
||||||
|
// 黄色
|
||||||
|
color = RouteColors.COLOR_MAIN_DAY_SLOW;
|
||||||
|
break;
|
||||||
|
case 2:
|
||||||
|
// 路况标签-拥堵
|
||||||
|
// 红色
|
||||||
|
color = RouteColors.COLOR_MAIN_DAY_VERY_SLOW;
|
||||||
|
break;
|
||||||
|
case 3:
|
||||||
|
// 路况标签-无路况
|
||||||
|
color = RouteColors.COLOR_MAIN_DAY_UNKNOWN;
|
||||||
|
break;
|
||||||
|
case 4:
|
||||||
|
// 路况标签-特别拥堵(猪肝红)
|
||||||
|
color = RouteColors.COLOR_MAIN_DAY_JAM;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
return color;
|
||||||
|
}
|
||||||
|
|
||||||
|
private ArrayList<TrafficItem> getTrafficItemsFromList(ArrayList<Integer> indexList) {
|
||||||
|
ArrayList<TrafficItem> trafficItems = new ArrayList<>();
|
||||||
|
for (int i = 0; i < indexList.size(); i = i + 3) {
|
||||||
|
TrafficItem item = new TrafficItem();
|
||||||
|
item.setTraffic(indexList.get(i));
|
||||||
|
item.setFromIndex(indexList.get(i + 1));
|
||||||
|
item.setToIndex(indexList.get(i + 2));
|
||||||
|
trafficItems.add(item);
|
||||||
|
}
|
||||||
|
return trafficItems;
|
||||||
|
}
|
||||||
public interface SelectNaviTypeListener {
|
public interface SelectNaviTypeListener {
|
||||||
void selectNaviType(Constant.NAV_TYPE nav_type);
|
void selectNaviType(Constant.NAV_TYPE nav_type);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,4 +3,10 @@
|
|||||||
<dimen name="top_pind_sp">20dp</dimen>
|
<dimen name="top_pind_sp">20dp</dimen>
|
||||||
<dimen name="fab_margin">16dp</dimen>
|
<dimen name="fab_margin">16dp</dimen>
|
||||||
<dimen name="default_widget_padding">6dp</dimen>
|
<dimen name="default_widget_padding">6dp</dimen>
|
||||||
|
|
||||||
|
<dimen name="navigation_line_width">10dp</dimen>
|
||||||
|
<dimen name="navigation_line_margin_left">30dp</dimen>
|
||||||
|
<dimen name="navigation_line_margin_top">50dp</dimen>
|
||||||
|
<dimen name="navigation_line_margin_right">80dp</dimen>
|
||||||
|
<dimen name="navigation_line_margin_bottom">30dp</dimen>
|
||||||
</resources>
|
</resources>
|
||||||
Reference in New Issue
Block a user