This commit is contained in:
2026-03-25 17:18:06 +08:00
parent c10c92afdc
commit 67c36f436c

View File

@@ -11,6 +11,7 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.util.TypedValue;
@@ -72,7 +73,9 @@ import org.json.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import androidx.annotation.NonNull;
import io.flutter.plugin.platform.PlatformView;
@@ -121,6 +124,9 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
private String token;
private String plateNumber;
private String mDebugUrl = "https://beta-esg.api.lnh2e.com/appointment/";
private String mReleaseUrl = "";
// 存储货车路线算法接口返回的数据
private TruckRouteData truckRouteData;
@@ -264,7 +270,6 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
}
private void initServices(Context context) {
try {
geocoderSearch = new GeocodeSearch(context);
@@ -451,50 +456,104 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
}
private void startRouteSearch() {
if (mActivity == null || startPoint == null || endPoint == null)
if (mActivity == null || startPoint == null) {
Toast.makeText(mContext, "定位信息不足,请稍后再试", Toast.LENGTH_SHORT).show();
return;
}
//确定目的地坐标和名称
// 优先级truckRouteData (接口返回的精准点) > endPoint (地图点击/搜索的点)
LatLng finalDestinationLatLng = null;
String finalDestinationName = endName;
if (truckRouteData != null && truckRouteData.destinationSite != null) {
String latStr = truckRouteData.destinationSite.latitude;
String lngStr = truckRouteData.destinationSite.longitude;
//防止 parseDouble 崩溃
if (latStr != null && !latStr.isEmpty() && lngStr != null && !lngStr.isEmpty()) {
try {
finalDestinationLatLng = new LatLng(
Double.parseDouble(latStr),
Double.parseDouble(lngStr)
);
if (truckRouteData.destinationSite.name != null && !truckRouteData.destinationSite.name.isEmpty()) {
finalDestinationName = truckRouteData.destinationSite.name;
}
} catch (NumberFormatException e) {
Log.e(TAG, "解析 truckRouteData 坐标失败: " + e.getMessage());
}
}
}
// 如果接口数据不可用,使用之前标记点/输入点作为兜底
if (finalDestinationLatLng == null) {
finalDestinationLatLng = endPoint;
}
// 最终校验
if (finalDestinationLatLng == null) {
Toast.makeText(mContext, "请先选择有效的目的地", Toast.LENGTH_SHORT).show();
return;
}
//构造起点和终点 POI
Poi start = new Poi(startName, startPoint, "");
Poi end = new Poi(endName, endPoint, "");
Poi end = new Poi(finalDestinationName, finalDestinationLatLng, "");
//配置导航参数
AmapNaviParams params = new AmapNaviParams(start, null, end, AmapNaviType.DRIVER, AmapPageType.ROUTE);
//todo 根据接口的返回数据填充
try {
AMapNavi mAMapNavi = AMapNavi.getInstance(mContext);
//添加车辆信息示例代码
AMapCarInfo carInfo = new AMapCarInfo();
carInfo.setCarNumber("沪AGK2267");//设置车牌号
carInfo.setCarType("1"); //设置车辆类型,0:小车; 1:货车. 默认0(小车).
carInfo.setVehicleAxis("6"); //设置货车的轴数mCarType = 1时候生效取值[0-255]默认为2
carInfo.setVehicleHeight("3.56"); //设置货车的高度单位mCarType = 1时候生效取值[0-25.5],默认1.6米
carInfo.setVehicleLength("7.3"); //设置货车的最大长度单位mCarType = 1时候生效取值[0-25]默认6米
carInfo.setVehicleWidth("2.5"); //设置货车的最大宽度单位mCarType = 1时候生效取值[0-25.5]默认2.5米
carInfo.setVehicleSize("4"); //设置货车的大小1-微型货车 2-轻型/小型货车 3-中型货车 4-重型货车默认为2
carInfo.setVehicleLoad("25.99"); //设置货车的总重,即车重+核定载重单位mCarType = 1时候生效取值[0-6553.5]
carInfo.setVehicleWeight("20"); //设置货车的核定载重单位mCarType = 1时候生效取值[0-6553.5]
carInfo.setRestriction(true); //设置是否躲避车辆限行true代表躲避车辆限行false代表不躲避车辆限行,默认为true
carInfo.setVehicleLoadSwitch(true); //设置货车重量是否参与算路true-重量会参与算路false-重量不会参与算路。默认为false
params.setCarInfo(carInfo);
mAMapNavi.setCarInfo(carInfo);
// 设置车辆信息 (只有在数据完整时设置)
if (truckRouteData != null && truckRouteData.truckDto != null) {
AMapCarInfo carInfo = new AMapCarInfo();
carInfo.setCarNumber(plateNumber); // 使用初始化时获取的车牌
carInfo.setCarType(String.valueOf(truckRouteData.truckDto.mcarType));
carInfo.setVehicleAxis(String.valueOf(truckRouteData.truckDto.mvehicleAxis));
carInfo.setVehicleHeight(truckRouteData.truckDto.mvehicleHeight);
carInfo.setVehicleLength(truckRouteData.truckDto.mvehicleLength);
carInfo.setVehicleWidth(truckRouteData.truckDto.mvehicleWidth);
carInfo.setVehicleSize(String.valueOf(truckRouteData.truckDto.mvehicleSize));
carInfo.setVehicleLoad(truckRouteData.truckDto.mvehicleLoad);
carInfo.setVehicleWeight(truckRouteData.truckDto.mvehicleWeight);
carInfo.setRestriction(truckRouteData.truckDto.isRestriction);
carInfo.setVehicleLoadSwitch(truckRouteData.truckDto.mvehicleLoadSwitch);
//添加途经点算路示例代码
// 起点信息
NaviPoi startNaviPoi = new NaviPoi("龙城花园", null, "B000A8UF3J");
// 终点信息
NaviPoi endNaviPoi = new NaviPoi("北京大学", null, "B000A816R6");
// 途经点信息
List<NaviPoi> waysPoiIds = new ArrayList<NaviPoi>();
waysPoiIds.add(new NaviPoi("途经点1", null, "B000A805M7"));
waysPoiIds.add(new NaviPoi("途经点2", null, "B0FFFAADBU"));
waysPoiIds.add(new NaviPoi("途经点3", null, "B0FFF5BER7"));
// POI算路
mAMapNavi.calculateDriveRoute(startNaviPoi, endNaviPoi, waysPoiIds, PathPlanningStrategy.DRIVING_MULTIPLE_ROUTES_DEFAULT);
params.setCarInfo(carInfo);
mAMapNavi.setCarInfo(carInfo);
}
//处理途径点 (如果接口返回了 naviList)
if (truckRouteData != null && truckRouteData.pathDto != null && truckRouteData.pathDto.naviList != null) {
NaviPoi startNaviPoi = new NaviPoi(startName, startPoint, "");
NaviPoi endNaviPoi = new NaviPoi(finalDestinationName, finalDestinationLatLng, "");
List<NaviPoi> waysPoiIds = new ArrayList<>();
for (NaviPoint np : truckRouteData.pathDto.naviList) {
if (np.coordinate != null && !TextUtils.isEmpty(np.coordinate.latitude) && !TextUtils.isEmpty(np.coordinate.longitude)) {
try {
double lat = Double.parseDouble(np.coordinate.latitude);
double lng = Double.parseDouble(np.coordinate.longitude);
waysPoiIds.add(new NaviPoi(np.name, new LatLng(lat, lng), np.poiId));
} catch (Exception e) {
Log.e(TAG, "途径点坐标解析错误: " + np.name);
}
}
}
// 如果有途径点,主动触发算路逻辑
if (!waysPoiIds.isEmpty()) {
mAMapNavi.calculateDriveRoute(startNaviPoi, endNaviPoi, waysPoiIds, PathPlanningStrategy.DRIVING_MULTIPLE_ROUTES_DEFAULT);
}
}
} catch (Exception e) {
e.printStackTrace();
Log.e(TAG, "配置导航参数出错: " + e.getMessage());
}
//启动货车导航组件
//启动高德
AmapNaviPage.getInstance().showRouteActivity(mActivity, params, null);
}
@@ -511,8 +570,12 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
this.plateNumber = plateNumber;
}
// 当前选中的站点ID默认id和选择地图id
private String selectedSiteId = "";
/**
* 定位成功后调用,获取货车路线算法信息
* 定位成功后调用,地图选点后调用
* 获取货车路线算法信息
*/
private void fetchTruckRouteAlgorithm(AMapLocation loc) {
if (plateNumber == null || plateNumber.isEmpty()) {
@@ -524,16 +587,16 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
json.put("longitude", String.valueOf(loc.getLongitude()));
json.put("latitude", String.valueOf(loc.getLatitude()));
json.put("plateNumber", plateNumber);
json.put("hydrogenSiteId", "");
json.put("hydrogenSiteId", selectedSiteId);
Request.Builder requestBuilder = new Request.Builder()
.url("https://beta-esg.api.lnh2e.com/appointment/truck/truckRouteAlgorithm")
.url(mDebugUrl + "truck/truckRouteAlgorithm")
.post(RequestBody.create(json.toString(), MediaType.parse("application/json")));
if (token != null && !token.isEmpty()) {
requestBuilder.addHeader("asoco-token", token);
}
Log.d("566-", "asoco-token:" + token + "requestBuilder:" + json);
httpClient.newCall(requestBuilder.build()).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
@@ -658,6 +721,11 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
}
}
/**
* 获取定位地区的推荐站点
*
* @param loc
*/
private void fetchRecommendStation(AMapLocation loc) {
try {
JSONObject json = new JSONObject();
@@ -666,7 +734,7 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
json.put("longitude", loc.getLongitude());
json.put("latitude", loc.getLatitude());
httpClient.newCall(new Request.Builder().url("https://beta-esg.api.lnh2e.com/appointment/station/getStationInfoByArea").post(RequestBody.create(json.toString(), MediaType.parse("application/json"))).build()).enqueue(new Callback() {
httpClient.newCall(new Request.Builder().url(mDebugUrl + "station/getStationInfoByArea").post(RequestBody.create(json.toString(), MediaType.parse("application/json"))).build()).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
}
@@ -680,10 +748,11 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
JSONObject data = res.getJSONObject("data");
endPoint = new LatLng(data.getDouble("latitude"), data.getDouble("longitude"));
endName = data.getString("name");
selectedSiteId = data.optString("id", "");
String addr = data.optString("address", "");
new Handler(Looper.getMainLooper()).post(() -> {
endInput.setText(addr);
markStation(endPoint, endName, true);
markStation(endPoint, endName, selectedSiteId, true);
});
}
} catch (Exception e) {
@@ -703,7 +772,7 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
json.put("longitude", loc.getLongitude());
json.put("latitude", loc.getLatitude());
httpClient.newCall(new Request.Builder().url("https://beta-esg.api.lnh2e.com/appointment/station/getNearbyHydrogenStationsByLocation").post(RequestBody.create(json.toString(), MediaType.parse("application/json"))).build()).enqueue(new Callback() {
httpClient.newCall(new Request.Builder().url(mDebugUrl + "station/getNearbyHydrogenStationsByLocation").post(RequestBody.create(json.toString(), MediaType.parse("application/json"))).build()).enqueue(new Callback() {
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
}
@@ -718,7 +787,9 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
for (int i = 0; i < array.length(); i++) {
try {
JSONObject item = array.getJSONObject(i);
markStation(new LatLng(item.getDouble("latitude"), item.getDouble("longitude")), item.getString("name"), false);
String id = item.getString("id");
markStation(new LatLng(item.getDouble("latitude"), item.getDouble("longitude")),
item.getString("name"), id, false);
} catch (Exception ignored) {
}
}
@@ -877,20 +948,62 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
return truckRouteData;
}
private void markStation(LatLng latLng, String name, boolean isRecommend) {
Marker m = aMap.addMarker(new MarkerOptions().position(latLng).title(name).icon(BitmapDescriptorFactory.defaultMarker(isRecommend ? BitmapDescriptorFactory.HUE_RED : BitmapDescriptorFactory.HUE_GREEN)));
m.setObject(latLng);
private void markStation(LatLng latLng, String name, String stationId,
boolean isRecommend) {
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng).title(name)
.icon(BitmapDescriptorFactory.defaultMarker(
isRecommend ? BitmapDescriptorFactory.HUE_RED
: BitmapDescriptorFactory.HUE_GREEN));
Marker m = aMap.addMarker(markerOptions);
Map<String, Object> dataMap = new HashMap<>();
dataMap.put("latLng", latLng);
dataMap.put("stationId", stationId);
m.setObject(dataMap);
stationMarkers.add(m);
}
@Override
public boolean onMarkerClick(Marker marker) {
if (marker.getObject() instanceof LatLng) {
endPoint = (LatLng) marker.getObject();
Object obj = marker.getObject();
if (obj instanceof Map) {
Map<String, Object> dataMap = (Map<String, Object>) obj;
// 获取坐标并赋值
LatLng latLng = (LatLng) dataMap.get("latLng");
if (latLng != null) {
this.endPoint = latLng;
}
// 获取站点ID并赋值
String stationId = (String) dataMap.get("stationId");
if (stationId != null) {
this.selectedSiteId = stationId;
}
// 更新 UI 和 业务逻辑
endName = marker.getTitle();
endInput.setText(endName);
// 需要传入当前位置以便接口计算路线
if (mlocationClient != null && mlocationClient.getLastKnownLocation() != null) {
fetchTruckRouteAlgorithm(mlocationClient.getLastKnownLocation());
} else if (currentLatLng != null) {
// 如果没有精准定位,尝试使用上次记录的经纬度
AMapLocation tempLoc = new AMapLocation("");
tempLoc.setLatitude(currentLatLng.latitude);
tempLoc.setLongitude(currentLatLng.longitude);
fetchTruckRouteAlgorithm(tempLoc);
}
// 计算路径
calculateRouteBeforeNavi();
}
return true;
}