调整地图选点

This commit is contained in:
2026-03-26 18:01:05 +08:00
parent b275c66383
commit 3aac4e7330
3 changed files with 88 additions and 26 deletions

View File

@@ -4,7 +4,9 @@ import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.os.Build;
@@ -39,6 +41,7 @@ import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.LocationSource;
import com.amap.api.maps.MapView;
import com.amap.api.maps.MapsInitializer;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
@@ -92,8 +95,7 @@ import okhttp3.Response;
/**
* 高德地图导航
*/
public class NativeMapView implements PlatformView, LocationSource, AMapLocationListener,
GeocodeSearch.OnGeocodeSearchListener, RouteSearch.OnRouteSearchListener, AMap.OnMarkerClickListener, Inputtips.InputtipsListener {
public class NativeMapView implements PlatformView, LocationSource, AMapLocationListener, GeocodeSearch.OnGeocodeSearchListener, RouteSearch.OnRouteSearchListener, AMap.OnMarkerClickListener, Inputtips.InputtipsListener {
private static final String TAG = "NativeMapView";
private final FrameLayout container;
@@ -316,8 +318,7 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
tvStationName.setTextSize(18);
tvStationName.setTextColor(Color.BLACK);
tvStationName.setTypeface(Typeface.DEFAULT_BOLD);
LinearLayout.LayoutParams nameParams = new LinearLayout.LayoutParams(
0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
LinearLayout.LayoutParams nameParams = new LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT, 1.0f);
titleLayout.addView(tvStationName, nameParams);
// 关闭按钮 (X)
@@ -398,9 +399,7 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
// 开始规划前隐藏输入框面板
searchArea.setVisibility(View.GONE);
RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(
new LatLonPoint(startPoint.latitude, startPoint.longitude),
new LatLonPoint(endPoint.latitude, endPoint.longitude));
RouteSearch.FromAndTo fromAndTo = new RouteSearch.FromAndTo(new LatLonPoint(startPoint.latitude, startPoint.longitude), new LatLonPoint(endPoint.latitude, endPoint.longitude));
RouteSearch.DriveRouteQuery query = new RouteSearch.DriveRouteQuery(fromAndTo, RouteSearch.DRIVING_SINGLE_DEFAULT, null, null, "");
routeSearch.calculateDriveRouteAsyn(query);
}
@@ -466,10 +465,7 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
//防止 parseDouble 崩溃
if (latStr != null && !latStr.isEmpty() && lngStr != null && !lngStr.isEmpty()) {
try {
finalDestinationLatLng = new LatLng(
Double.parseDouble(latStr),
Double.parseDouble(lngStr)
);
finalDestinationLatLng = new LatLng(Double.parseDouble(latStr), Double.parseDouble(lngStr));
if (truckRouteData.destinationSite.name != null && !truckRouteData.destinationSite.name.isEmpty()) {
finalDestinationName = truckRouteData.destinationSite.name;
}
@@ -582,9 +578,7 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
json.put("plateNumber", plateNumber);
json.put("hydrogenSiteId", selectedSiteId);
Request.Builder requestBuilder = new Request.Builder()
.url(mDebugUrl + "truck/truckRouteAlgorithm")
.post(RequestBody.create(json.toString(), MediaType.parse("application/json")));
Request.Builder requestBuilder = new Request.Builder().url(mDebugUrl + "truck/truckRouteAlgorithm").post(RequestBody.create(json.toString(), MediaType.parse("application/json")));
if (token != null && !token.isEmpty()) {
requestBuilder.addHeader("asoco-token", token);
@@ -780,8 +774,11 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
try {
JSONObject item = array.getJSONObject(i);
String id = item.getString("id");
markStation(new LatLng(item.getDouble("latitude"), item.getDouble("longitude")),
item.getString("name"), item.getString("address"), id, false);
//推荐站点跳出
if (selectedSiteId.equals(id)) {
continue;
}
markStation(new LatLng(item.getDouble("latitude"), item.getDouble("longitude")), item.getString("name"), item.getString("address"), id, false);
} catch (Exception ignored) {
}
}
@@ -798,13 +795,17 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
}
private void markStation(LatLng latLng, String name, String address, String stationId,
boolean isRecommend) {
MarkerOptions markerOptions = new MarkerOptions()
.position(latLng).title(name)
.icon(BitmapDescriptorFactory.defaultMarker(
isRecommend ? BitmapDescriptorFactory.HUE_RED
: BitmapDescriptorFactory.HUE_GREEN));
private void markStation(LatLng latLng, String name, String address, String stationId, boolean isRecommend) {
String displayName = name;
if (displayName != null && displayName.length() > 7) {
displayName = displayName.substring(0, 7) + "...";
}
BitmapDescriptor icon = getMarkerIconWithText(mContext, displayName, isRecommend);
MarkerOptions markerOptions = new MarkerOptions().position(latLng).icon(icon).anchor(0.5f, 0.8f); // 重点:调整锚点,确保图标尖端对准经纬度
Marker m = aMap.addMarker(markerOptions);
Map<String, Object> dataMap = new HashMap<>();
@@ -818,8 +819,14 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
@Override
public boolean onMarkerClick(Marker marker) {
Object obj = marker.getObject();
//地图选点
for (Marker m : stationMarkers) {
m.setIcon(BitmapDescriptorFactory.fromResource(m.equals(marker) ? R.drawable.ic_marker : R.drawable.ic_un_marker));
}
Object obj = marker.getObject();
if (obj instanceof Map) {
Map<String, Object> dataMap = (Map<String, Object>) obj;
@@ -909,6 +916,61 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
mapView.onDestroy();
}
public BitmapDescriptor getMarkerIconWithText(Context context, String text, boolean isRecommend) {
// 创建主容器
LinearLayout container = new LinearLayout(context);
container.setOrientation(LinearLayout.VERTICAL);
container.setGravity(Gravity.CENTER_HORIZONTAL);
// 必须要给容器本身也设置一个基础参数,否则测量可能失败
container.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
// 文字部分:必须显式设置 LayoutParams
TextView textView = new TextView(context);
textView.setText(text);
textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
textView.setTextColor(Color.BLACK);
// 设置文字的参数为 WRAP_CONTENT
LinearLayout.LayoutParams textLp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
textView.setLayoutParams(textLp);
container.addView(textView);
// 图标部分:设置固定大小
ImageView imageView = new ImageView(context);
imageView.setImageResource(isRecommend ? R.drawable.ic_marker : R.drawable.ic_un_marker);
int size = dp2px(30);
LinearLayout.LayoutParams imgLp = new LinearLayout.LayoutParams(size, size);
imgLp.topMargin = dp2px(2);
imageView.setLayoutParams(imgLp);
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
container.addView(imageView);
// --- 精准测量 ---
int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
container.measure(spec, spec);
// 获取测量后的实际宽高
int measuredWidth = container.getMeasuredWidth();
int measuredHeight = container.getMeasuredHeight();
// 如果测出来是 0说明有问题
if (measuredWidth <= 0 || measuredHeight <= 0) {
// 兜底逻辑:如果测量失败,给一个默认大小
measuredWidth = dp2px(100);
measuredHeight = dp2px(60);
}
container.layout(0, 0, measuredWidth, measuredHeight);
// 绘制到 Bitmap
Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
// 绘制前可以选清空背景(透明)
canvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
container.draw(canvas);
return BitmapDescriptorFactory.fromBitmap(bitmap);
}
/**
* 辅助方法:创建带图标的文本项
@@ -970,7 +1032,7 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
}
// 1. 成本计算模式 (高亮绿色头部)
TextView item1 = createMenuItem(context, "成本计算模式", true);
TextView item1 = createMenuItem(context, "成本计算模式", false);
item1.setOnClickListener(v -> switchMode("成本计算"));
// 2. 送货规划模式
@@ -978,7 +1040,7 @@ public class NativeMapView implements PlatformView, LocationSource, AMapLocation
item2.setOnClickListener(v -> switchMode("送货规划"));
// 3. 加氢规划模式
TextView item3 = createMenuItem(context, "加氢规划模式", false);
TextView item3 = createMenuItem(context, "加氢规划模式", true);
item3.setOnClickListener(v -> switchMode("加氢规划"));
menu.addView(item1);

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.6 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB