|
@@ -5,13 +5,14 @@ import android.content.Context;
|
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.NonNull;
|
|
|
|
|
|
|
|
import com.amap.api.maps.AMap;
|
|
import com.amap.api.maps.AMap;
|
|
|
-import com.amap.api.maps.AMapOptions;
|
|
|
|
|
import com.amap.api.maps.CameraUpdate;
|
|
import com.amap.api.maps.CameraUpdate;
|
|
|
import com.amap.api.maps.CameraUpdateFactory;
|
|
import com.amap.api.maps.CameraUpdateFactory;
|
|
|
|
|
+import com.amap.api.maps.model.CameraPosition;
|
|
|
import com.amap.api.maps.model.LatLng;
|
|
import com.amap.api.maps.model.LatLng;
|
|
|
import com.amap.api.maps.model.LatLngBounds;
|
|
import com.amap.api.maps.model.LatLngBounds;
|
|
|
import com.atmob.map_amap_android.bean.MapPadding;
|
|
import com.atmob.map_amap_android.bean.MapPadding;
|
|
|
import com.atmob.map_amap_android.contants.Constants;
|
|
import com.atmob.map_amap_android.contants.Constants;
|
|
|
|
|
+import com.atmob.map_amap_android.event.FlutterCameraChangeEventPlugin;
|
|
|
import com.atmob.map_amap_android.overlays.MyMethodCallHandler;
|
|
import com.atmob.map_amap_android.overlays.MyMethodCallHandler;
|
|
|
import com.atmob.map_amap_android.util.GsonUtil;
|
|
import com.atmob.map_amap_android.util.GsonUtil;
|
|
|
import com.atmob.map_amap_android.util.LogUtil;
|
|
import com.atmob.map_amap_android.util.LogUtil;
|
|
@@ -23,6 +24,7 @@ import com.google.gson.reflect.TypeToken;
|
|
|
import java.util.List;
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
|
|
|
|
|
|
|
|
+import io.flutter.embedding.engine.plugins.FlutterPlugin;
|
|
|
import io.flutter.plugin.common.MethodCall;
|
|
import io.flutter.plugin.common.MethodCall;
|
|
|
import io.flutter.plugin.common.MethodChannel;
|
|
import io.flutter.plugin.common.MethodChannel;
|
|
|
|
|
|
|
@@ -35,11 +37,14 @@ public class MapController implements MyMethodCallHandler {
|
|
|
private final Context context;
|
|
private final Context context;
|
|
|
|
|
|
|
|
private final MethodChannel methodChannel;
|
|
private final MethodChannel methodChannel;
|
|
|
|
|
+ private final FlutterPlugin.FlutterPluginBinding flutterPluginBinding;
|
|
|
|
|
|
|
|
int nowMapTypeIndex = 0;
|
|
int nowMapTypeIndex = 0;
|
|
|
|
|
|
|
|
private final Gson gson;
|
|
private final Gson gson;
|
|
|
|
|
|
|
|
|
|
+ FlutterCameraChangeEventPlugin cameraChangeEventPlugin;
|
|
|
|
|
+
|
|
|
private final int[] mapType = {
|
|
private final int[] mapType = {
|
|
|
AMap.MAP_TYPE_NORMAL,
|
|
AMap.MAP_TYPE_NORMAL,
|
|
|
AMap.MAP_TYPE_SATELLITE,
|
|
AMap.MAP_TYPE_SATELLITE,
|
|
@@ -49,9 +54,10 @@ public class MapController implements MyMethodCallHandler {
|
|
|
AMap.MAP_TYPE_NAVI_NIGHT,
|
|
AMap.MAP_TYPE_NAVI_NIGHT,
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
- public MapController(Context context, MethodChannel methodChannel, AMap map) {
|
|
|
|
|
|
|
+ public MapController(Context context, MethodChannel methodChannel, FlutterPlugin.FlutterPluginBinding flutterPluginBinding, AMap map) {
|
|
|
this.context = context;
|
|
this.context = context;
|
|
|
this.methodChannel = methodChannel;
|
|
this.methodChannel = methodChannel;
|
|
|
|
|
+ this.flutterPluginBinding = flutterPluginBinding;
|
|
|
this.map = map;
|
|
this.map = map;
|
|
|
map.setMapType(mapType[nowMapTypeIndex]);
|
|
map.setMapType(mapType[nowMapTypeIndex]);
|
|
|
gson = GsonUtil.getInstance();
|
|
gson = GsonUtil.getInstance();
|
|
@@ -64,6 +70,9 @@ public class MapController implements MyMethodCallHandler {
|
|
|
case Constants.METHOD_MOVE_CAMERA:
|
|
case Constants.METHOD_MOVE_CAMERA:
|
|
|
moveToCamera(call, result);
|
|
moveToCamera(call, result);
|
|
|
break;
|
|
break;
|
|
|
|
|
+ case Constants.METHOD_MOVE_CAMERA_TO_BOUNDS:
|
|
|
|
|
+ moveCameraToBounds(call, result);
|
|
|
|
|
+ break;
|
|
|
case Constants.METHOD_ANIMATE_CAMERA:
|
|
case Constants.METHOD_ANIMATE_CAMERA:
|
|
|
animateCamera(call, result);
|
|
animateCamera(call, result);
|
|
|
break;
|
|
break;
|
|
@@ -79,8 +88,82 @@ public class MapController implements MyMethodCallHandler {
|
|
|
case Constants.METHOD_MAP_LOGO_VISIBLE:
|
|
case Constants.METHOD_MAP_LOGO_VISIBLE:
|
|
|
mapLogoVisible(call, result);
|
|
mapLogoVisible(call, result);
|
|
|
break;
|
|
break;
|
|
|
|
|
+ case Constants.METHOD_SET_CAMERA_CHANGE_EVENT_CHANNEL:
|
|
|
|
|
+ setCameraChangeEventChannel(call, result);
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private void moveCameraToBounds(MethodCall call, MethodChannel.Result result) {
|
|
|
|
|
+ LogUtil.i(TAG, "moveCameraToBounds===>" + call.arguments());
|
|
|
|
|
+ if (map == null) {
|
|
|
|
|
+ result.error("map is null", null, null);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ try {
|
|
|
|
|
+ Map<String, Object> arguments = call.arguments();
|
|
|
|
|
+ String boundsStr = ParamUtil.getString(arguments, "bounds");
|
|
|
|
|
+ if (boundsStr == null || boundsStr.isEmpty()) {
|
|
|
|
|
+ result.error("bounds is null", null, null);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ LatLngBounds bounds = gson.fromJson(boundsStr, LatLngBounds.class);
|
|
|
|
|
+ if (bounds == null) {
|
|
|
|
|
+ result.error("bounds parse error", null, null);
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ String paddingStr = ParamUtil.getString(arguments, "mapPadding");
|
|
|
|
|
+
|
|
|
|
|
+ MapPadding padding = null;
|
|
|
|
|
+ if (paddingStr != null && !paddingStr.isEmpty()) {
|
|
|
|
|
+ padding = gson.fromJson(paddingStr, MapPadding.class);
|
|
|
|
|
+ }
|
|
|
|
|
+ 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());
|
|
|
|
|
+ }
|
|
|
|
|
+ CameraUpdate cameraUpdate = CameraUpdateFactory.newLatLngBoundsRect(bounds, left, right, top, bottom);
|
|
|
|
|
+ map.moveCamera(cameraUpdate);
|
|
|
|
|
+ result.success(null);
|
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
|
+ LogUtil.d(TAG, "moveCameraToBounds error===>" + e.getMessage());
|
|
|
|
|
+ result.error("JsonSyntaxException", e.getMessage(), null);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
|
|
|
|
|
+ private void setCameraChangeEventChannel(MethodCall call, MethodChannel.Result result) {
|
|
|
|
|
+ if (map == null) {
|
|
|
|
|
+ result.error("map is null", null, null);
|
|
|
|
|
+ return;
|
|
|
}
|
|
}
|
|
|
|
|
+ if (cameraChangeEventPlugin != null) {
|
|
|
|
|
+ return;
|
|
|
|
|
+ }
|
|
|
|
|
+ cameraChangeEventPlugin = new FlutterCameraChangeEventPlugin();
|
|
|
|
|
+ map.setOnCameraChangeListener(new AMap.OnCameraChangeListener() {
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onCameraChange(CameraPosition cameraPosition) {
|
|
|
|
|
+// LogUtil.d(TAG, "onCameraChange===>" + cameraPosition);
|
|
|
|
|
+ // 地图移动过程中持续回调
|
|
|
|
|
+ cameraChangeEventPlugin.senCameraChange("onCameraChange", cameraPosition);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void onCameraChangeFinish(CameraPosition cameraPosition) {
|
|
|
|
|
+// LogUtil.d(TAG, "onCameraChangeFinish===>" + cameraPosition);
|
|
|
|
|
+ // 地图移动结束时回调
|
|
|
|
|
+ cameraChangeEventPlugin.senCameraChange("onCameraChangeFinish", cameraPosition);
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ cameraChangeEventPlugin.onAttachedToEngine(flutterPluginBinding);
|
|
|
|
|
+ LogUtil.d(TAG, "setCameraChangeEventChannel success");
|
|
|
|
|
+ result.success(null);
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
private void mapInteractionEnabled(MethodCall call, MethodChannel.Result result) {
|
|
private void mapInteractionEnabled(MethodCall call, MethodChannel.Result result) {
|
|
@@ -212,4 +295,13 @@ public class MapController implements MyMethodCallHandler {
|
|
|
public String[] getRegisterMethodIdArray() {
|
|
public String[] getRegisterMethodIdArray() {
|
|
|
return Constants.METHOD_ID_LIST_FOR_MAP;
|
|
return Constants.METHOD_ID_LIST_FOR_MAP;
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public void dispose() {
|
|
|
|
|
+ if (cameraChangeEventPlugin != null) {
|
|
|
|
|
+ if (map != null) map.setOnCameraChangeListener(null);
|
|
|
|
|
+ cameraChangeEventPlugin.onDetachedFromEngine(flutterPluginBinding);
|
|
|
|
|
+ cameraChangeEventPlugin = null;
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|