|
|
@@ -5,12 +5,21 @@ import android.content.Context;
|
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
import com.amap.api.maps.AMap;
|
|
|
+import com.amap.api.maps.CameraUpdate;
|
|
|
import com.amap.api.maps.CameraUpdateFactory;
|
|
|
+import com.amap.api.maps.model.LatLng;
|
|
|
+import com.amap.api.maps.model.LatLngBounds;
|
|
|
+import com.atmob.map_amap_android.bean.MapPadding;
|
|
|
import com.atmob.map_amap_android.contants.Constants;
|
|
|
import com.atmob.map_amap_android.overlays.MyMethodCallHandler;
|
|
|
+import com.atmob.map_amap_android.util.GsonUtil;
|
|
|
import com.atmob.map_amap_android.util.LogUtil;
|
|
|
import com.atmob.map_amap_android.util.ParamUtil;
|
|
|
+import com.atmob.map_amap_android.util.SizeUtil;
|
|
|
+import com.google.gson.Gson;
|
|
|
+import com.google.gson.reflect.TypeToken;
|
|
|
|
|
|
+import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
import io.flutter.plugin.common.MethodCall;
|
|
|
@@ -26,10 +35,25 @@ public class MapController implements MyMethodCallHandler {
|
|
|
|
|
|
private final MethodChannel methodChannel;
|
|
|
|
|
|
+ int nowMapTypeIndex = 0;
|
|
|
+
|
|
|
+ private final Gson gson;
|
|
|
+
|
|
|
+ private final int[] mapType = {
|
|
|
+ AMap.MAP_TYPE_NORMAL,
|
|
|
+ AMap.MAP_TYPE_SATELLITE,
|
|
|
+ AMap.MAP_TYPE_NIGHT,
|
|
|
+ AMap.MAP_TYPE_NAVI,
|
|
|
+ AMap.MAP_TYPE_BUS,
|
|
|
+ AMap.MAP_TYPE_NAVI_NIGHT,
|
|
|
+ };
|
|
|
+
|
|
|
public MapController(Context context, MethodChannel methodChannel, AMap map) {
|
|
|
this.context = context;
|
|
|
this.methodChannel = methodChannel;
|
|
|
this.map = map;
|
|
|
+ map.setMapType(mapType[nowMapTypeIndex]);
|
|
|
+ gson = GsonUtil.getInstance();
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -42,12 +66,77 @@ public class MapController implements MyMethodCallHandler {
|
|
|
case Constants.METHOD_ANIMATE_CAMERA:
|
|
|
animateCamera(call, result);
|
|
|
break;
|
|
|
- case Constants.METHOD_MAP_CLEAR:
|
|
|
+ case Constants.METHOD_MAP_CLEAR:
|
|
|
clearMap(result);
|
|
|
break;
|
|
|
+ case Constants.METHOD_MAP_MOVE_TO_SUITABLE_LOCATION:
|
|
|
+ animateSuitableLocation(call, result);
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ private void animateSuitableLocation(MethodCall call, MethodChannel.Result result) {
|
|
|
+ try {
|
|
|
+ Map<String, Object> arguments = call.arguments();
|
|
|
+ if (arguments == null) {
|
|
|
+ result.error("arguments is null", null, null);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ boolean isAnimateCamera = ParamUtil.getBoolean(arguments, "isAnimateCamera", true);
|
|
|
+ LogUtil.d(TAG, "isAnimateCamera===>" + isAnimateCamera);
|
|
|
+ String points = (String) arguments.get("points");
|
|
|
+ if (points == null || points.isEmpty()) {
|
|
|
+ result.error("latLngList is null", null, null);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ List<LatLng> latLngList = gson.fromJson(points, new TypeToken<List<LatLng>>() {
|
|
|
+ }.getType());
|
|
|
+ LogUtil.d(TAG, "latLngList===>" + latLngList);
|
|
|
+ if (latLngList == null || latLngList.isEmpty()) {
|
|
|
+ result.error("latLngList is empty", null, null);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ String paddingStr = (String) arguments.get("mapPadding");
|
|
|
+
|
|
|
+ MapPadding padding = null;
|
|
|
+ if (paddingStr != null && !paddingStr.isEmpty()) {
|
|
|
+ padding = gson.fromJson(paddingStr, MapPadding.class);
|
|
|
+ }
|
|
|
+ LogUtil.d(TAG, "padding===>" + padding);
|
|
|
+ moveToSuitableLocation(latLngList, padding);
|
|
|
+ result.success(null);
|
|
|
+ } catch (Exception e) {
|
|
|
+ LogUtil.d(TAG, "addPolyline error===>" + e.getMessage());
|
|
|
+ result.error("JsonSyntaxException", e.getMessage(), null);
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ private void moveToSuitableLocation(List<LatLng> points, MapPadding padding) {
|
|
|
+ if (map == null) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ LatLngBounds.Builder builder = LatLngBounds.builder();
|
|
|
+ for (int i = 0; i < points.size(); i++) {
|
|
|
+ LatLng latLng = points.get(i);
|
|
|
+ builder.include(latLng);
|
|
|
+ }
|
|
|
+ LatLngBounds bounds = builder.build();
|
|
|
+ int left = 0;
|
|
|
+ int top = 0;
|
|
|
+ int right = 0;
|
|
|
+ int bottom = 0;
|
|
|
+ if (padding != null) {
|
|
|
+ left = (int) SizeUtil.dp2px(context, padding.getLeft());
|
|
|
+ top = (int) SizeUtil.dp2px(context, padding.getTop());
|
|
|
+ right = (int) SizeUtil.dp2px(context, padding.getRight());
|
|
|
+ bottom = (int) SizeUtil.dp2px(context, padding.getBottom());
|
|
|
+ }
|
|
|
+ LogUtil.d(TAG, "left===>" + left + " top===>" + top + " right===>" + right + " bottom===>" + bottom);
|
|
|
+ CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBoundsRect(bounds, left, right, top, bottom);
|
|
|
+ map.animateCamera(cameraUpdate);
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
private void clearMap(MethodChannel.Result result) {
|
|
|
LogUtil.i(TAG, "clearMap");
|
|
|
map.clear();
|