bugfix
This commit is contained in:
@@ -11,6 +11,7 @@ import android.os.Bundle;
|
|||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.text.Editable;
|
import android.text.Editable;
|
||||||
|
import android.text.TextUtils;
|
||||||
import android.text.TextWatcher;
|
import android.text.TextWatcher;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
import android.util.TypedValue;
|
import android.util.TypedValue;
|
||||||
@@ -72,7 +73,9 @@ import org.json.JSONObject;
|
|||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import io.flutter.plugin.platform.PlatformView;
|
import io.flutter.plugin.platform.PlatformView;
|
||||||
@@ -121,6 +124,9 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
|
|||||||
private String token;
|
private String token;
|
||||||
private String plateNumber;
|
private String plateNumber;
|
||||||
|
|
||||||
|
private String mDebugUrl = "https://beta-esg.api.lnh2e.com/appointment/";
|
||||||
|
private String mReleaseUrl = "";
|
||||||
|
|
||||||
// 存储货车路线算法接口返回的数据
|
// 存储货车路线算法接口返回的数据
|
||||||
private TruckRouteData truckRouteData;
|
private TruckRouteData truckRouteData;
|
||||||
|
|
||||||
@@ -264,7 +270,6 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
private void initServices(Context context) {
|
private void initServices(Context context) {
|
||||||
try {
|
try {
|
||||||
geocoderSearch = new GeocodeSearch(context);
|
geocoderSearch = new GeocodeSearch(context);
|
||||||
@@ -451,50 +456,104 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void startRouteSearch() {
|
private void startRouteSearch() {
|
||||||
if (mActivity == null || startPoint == null || endPoint == null)
|
if (mActivity == null || startPoint == null) {
|
||||||
|
Toast.makeText(mContext, "定位信息不足,请稍后再试", Toast.LENGTH_SHORT).show();
|
||||||
return;
|
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 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);
|
AmapNaviParams params = new AmapNaviParams(start, null, end, AmapNaviType.DRIVER, AmapPageType.ROUTE);
|
||||||
|
|
||||||
//todo 根据接口的返回数据填充
|
|
||||||
try {
|
try {
|
||||||
AMapNavi mAMapNavi = AMapNavi.getInstance(mContext);
|
AMapNavi mAMapNavi = AMapNavi.getInstance(mContext);
|
||||||
|
|
||||||
//添加车辆信息示例代码
|
// 设置车辆信息 (只有在数据完整时设置)
|
||||||
AMapCarInfo carInfo = new AMapCarInfo();
|
if (truckRouteData != null && truckRouteData.truckDto != null) {
|
||||||
carInfo.setCarNumber("沪AGK2267");//设置车牌号
|
AMapCarInfo carInfo = new AMapCarInfo();
|
||||||
carInfo.setCarType("1"); //设置车辆类型,0:小车; 1:货车. 默认0(小车).
|
carInfo.setCarNumber(plateNumber); // 使用初始化时获取的车牌
|
||||||
carInfo.setVehicleAxis("6"); //设置货车的轴数,mCarType = 1时候生效,取值[0-255],默认为2
|
carInfo.setCarType(String.valueOf(truckRouteData.truckDto.mcarType));
|
||||||
carInfo.setVehicleHeight("3.56"); //设置货车的高度,单位:米,mCarType = 1时候生效,取值[0-25.5],默认1.6米
|
carInfo.setVehicleAxis(String.valueOf(truckRouteData.truckDto.mvehicleAxis));
|
||||||
carInfo.setVehicleLength("7.3"); //设置货车的最大长度,单位:米,mCarType = 1时候生效,取值[0-25],默认6米
|
carInfo.setVehicleHeight(truckRouteData.truckDto.mvehicleHeight);
|
||||||
carInfo.setVehicleWidth("2.5"); //设置货车的最大宽度,单位:米,mCarType = 1时候生效,取值[0-25.5],默认2.5米
|
carInfo.setVehicleLength(truckRouteData.truckDto.mvehicleLength);
|
||||||
carInfo.setVehicleSize("4"); //设置货车的大小,1-微型货车 2-轻型/小型货车 3-中型货车 4-重型货车,默认为2
|
carInfo.setVehicleWidth(truckRouteData.truckDto.mvehicleWidth);
|
||||||
carInfo.setVehicleLoad("25.99"); //设置货车的总重,即车重+核定载重,单位:吨,mCarType = 1时候生效,取值[0-6553.5]
|
carInfo.setVehicleSize(String.valueOf(truckRouteData.truckDto.mvehicleSize));
|
||||||
carInfo.setVehicleWeight("20"); //设置货车的核定载重,单位:吨,mCarType = 1时候生效,取值[0-6553.5]
|
carInfo.setVehicleLoad(truckRouteData.truckDto.mvehicleLoad);
|
||||||
carInfo.setRestriction(true); //设置是否躲避车辆限行,true代表躲避车辆限行,false代表不躲避车辆限行,默认为true
|
carInfo.setVehicleWeight(truckRouteData.truckDto.mvehicleWeight);
|
||||||
carInfo.setVehicleLoadSwitch(true); //设置货车重量是否参与算路,true-重量会参与算路;false-重量不会参与算路。默认为false
|
carInfo.setRestriction(truckRouteData.truckDto.isRestriction);
|
||||||
params.setCarInfo(carInfo);
|
carInfo.setVehicleLoadSwitch(truckRouteData.truckDto.mvehicleLoadSwitch);
|
||||||
mAMapNavi.setCarInfo(carInfo);
|
|
||||||
|
|
||||||
//添加途经点算路示例代码
|
params.setCarInfo(carInfo);
|
||||||
// 起点信息
|
mAMapNavi.setCarInfo(carInfo);
|
||||||
NaviPoi startNaviPoi = new NaviPoi("龙城花园", null, "B000A8UF3J");
|
}
|
||||||
// 终点信息
|
|
||||||
NaviPoi endNaviPoi = new NaviPoi("北京大学", null, "B000A816R6");
|
//处理途径点 (如果接口返回了 naviList)
|
||||||
// 途经点信息
|
if (truckRouteData != null && truckRouteData.pathDto != null && truckRouteData.pathDto.naviList != null) {
|
||||||
List<NaviPoi> waysPoiIds = new ArrayList<NaviPoi>();
|
NaviPoi startNaviPoi = new NaviPoi(startName, startPoint, "");
|
||||||
waysPoiIds.add(new NaviPoi("途经点1", null, "B000A805M7"));
|
NaviPoi endNaviPoi = new NaviPoi(finalDestinationName, finalDestinationLatLng, "");
|
||||||
waysPoiIds.add(new NaviPoi("途经点2", null, "B0FFFAADBU"));
|
|
||||||
waysPoiIds.add(new NaviPoi("途经点3", null, "B0FFF5BER7"));
|
List<NaviPoi> waysPoiIds = new ArrayList<>();
|
||||||
// POI算路
|
for (NaviPoint np : truckRouteData.pathDto.naviList) {
|
||||||
mAMapNavi.calculateDriveRoute(startNaviPoi, endNaviPoi, waysPoiIds, PathPlanningStrategy.DRIVING_MULTIPLE_ROUTES_DEFAULT);
|
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) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
Log.e(TAG, "配置导航参数出错: " + e.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
//启动货车导航组件
|
//启动高德
|
||||||
AmapNaviPage.getInstance().showRouteActivity(mActivity, params, null);
|
AmapNaviPage.getInstance().showRouteActivity(mActivity, params, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -511,8 +570,12 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
|
|||||||
this.plateNumber = plateNumber;
|
this.plateNumber = plateNumber;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 当前选中的站点ID,默认id和选择地图id
|
||||||
|
private String selectedSiteId = "";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 定位成功后调用,获取货车路线算法信息
|
* 定位成功后调用,地图选点后调用
|
||||||
|
* 获取货车路线算法信息
|
||||||
*/
|
*/
|
||||||
private void fetchTruckRouteAlgorithm(AMapLocation loc) {
|
private void fetchTruckRouteAlgorithm(AMapLocation loc) {
|
||||||
if (plateNumber == null || plateNumber.isEmpty()) {
|
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("longitude", String.valueOf(loc.getLongitude()));
|
||||||
json.put("latitude", String.valueOf(loc.getLatitude()));
|
json.put("latitude", String.valueOf(loc.getLatitude()));
|
||||||
json.put("plateNumber", plateNumber);
|
json.put("plateNumber", plateNumber);
|
||||||
json.put("hydrogenSiteId", "");
|
json.put("hydrogenSiteId", selectedSiteId);
|
||||||
|
|
||||||
Request.Builder requestBuilder = new Request.Builder()
|
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")));
|
.post(RequestBody.create(json.toString(), MediaType.parse("application/json")));
|
||||||
|
|
||||||
if (token != null && !token.isEmpty()) {
|
if (token != null && !token.isEmpty()) {
|
||||||
requestBuilder.addHeader("asoco-token", token);
|
requestBuilder.addHeader("asoco-token", token);
|
||||||
}
|
}
|
||||||
|
Log.d("566-", "asoco-token:" + token + "requestBuilder:" + json);
|
||||||
httpClient.newCall(requestBuilder.build()).enqueue(new Callback() {
|
httpClient.newCall(requestBuilder.build()).enqueue(new Callback() {
|
||||||
@Override
|
@Override
|
||||||
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
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) {
|
private void fetchRecommendStation(AMapLocation loc) {
|
||||||
try {
|
try {
|
||||||
JSONObject json = new JSONObject();
|
JSONObject json = new JSONObject();
|
||||||
@@ -666,7 +734,7 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
|
|||||||
json.put("longitude", loc.getLongitude());
|
json.put("longitude", loc.getLongitude());
|
||||||
json.put("latitude", loc.getLatitude());
|
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
|
@Override
|
||||||
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
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");
|
JSONObject data = res.getJSONObject("data");
|
||||||
endPoint = new LatLng(data.getDouble("latitude"), data.getDouble("longitude"));
|
endPoint = new LatLng(data.getDouble("latitude"), data.getDouble("longitude"));
|
||||||
endName = data.getString("name");
|
endName = data.getString("name");
|
||||||
|
selectedSiteId = data.optString("id", "");
|
||||||
String addr = data.optString("address", "");
|
String addr = data.optString("address", "");
|
||||||
new Handler(Looper.getMainLooper()).post(() -> {
|
new Handler(Looper.getMainLooper()).post(() -> {
|
||||||
endInput.setText(addr);
|
endInput.setText(addr);
|
||||||
markStation(endPoint, endName, true);
|
markStation(endPoint, endName, selectedSiteId, true);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
@@ -703,7 +772,7 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
|
|||||||
json.put("longitude", loc.getLongitude());
|
json.put("longitude", loc.getLongitude());
|
||||||
json.put("latitude", loc.getLatitude());
|
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
|
@Override
|
||||||
public void onFailure(@NonNull Call call, @NonNull IOException e) {
|
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++) {
|
for (int i = 0; i < array.length(); i++) {
|
||||||
try {
|
try {
|
||||||
JSONObject item = array.getJSONObject(i);
|
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) {
|
} catch (Exception ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -877,20 +948,62 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
|
|||||||
return truckRouteData;
|
return truckRouteData;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void markStation(LatLng latLng, String name, boolean isRecommend) {
|
private void markStation(LatLng latLng, String name, String stationId,
|
||||||
Marker m = aMap.addMarker(new MarkerOptions().position(latLng).title(name).icon(BitmapDescriptorFactory.defaultMarker(isRecommend ? BitmapDescriptorFactory.HUE_RED : BitmapDescriptorFactory.HUE_GREEN)));
|
boolean isRecommend) {
|
||||||
m.setObject(latLng);
|
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);
|
stationMarkers.add(m);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean onMarkerClick(Marker marker) {
|
public boolean onMarkerClick(Marker marker) {
|
||||||
if (marker.getObject() instanceof LatLng) {
|
Object obj = marker.getObject();
|
||||||
endPoint = (LatLng) 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();
|
endName = marker.getTitle();
|
||||||
endInput.setText(endName);
|
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();
|
calculateRouteBeforeNavi();
|
||||||
}
|
}
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user