From b3f765c6d473d6c90b32f84ae3fc0c40bedcc388 Mon Sep 17 00:00:00 2001 From: huangjiahong Date: Thu, 9 Sep 2021 14:43:54 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=BE=E7=BD=AE=E4=B8=AD?= =?UTF-8?q?=E5=BF=83=E7=82=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../flutter_minemap/unil/Constants.java | 1 + .../views/mapHandler/MMapHandlerFactory.java | 1 + .../views/mapHandler/MapStateHandler.java | 23 +++++- example/lib/view/base_map_page.dart | 71 +++++++++++++++++-- lib/mimemap_controler.dart | 17 +++++ 5 files changed, 104 insertions(+), 9 deletions(-) diff --git a/android/src/main/java/com/imfunc/flutter_minemap/unil/Constants.java b/android/src/main/java/com/imfunc/flutter_minemap/unil/Constants.java index c0329b4..bd1d0bc 100755 --- a/android/src/main/java/com/imfunc/flutter_minemap/unil/Constants.java +++ b/android/src/main/java/com/imfunc/flutter_minemap/unil/Constants.java @@ -15,6 +15,7 @@ public static class ViewType { */ public static class FMMMapStateMethodId { public static final String sMapSetStyleMethod = "flutter_minemap/map/setStyleJson"; + public static final String sMapSetCenterMethod = "flutter_minemap/map/setCenter"; } /** diff --git a/android/src/main/java/com/imfunc/flutter_minemap/views/mapHandler/MMapHandlerFactory.java b/android/src/main/java/com/imfunc/flutter_minemap/views/mapHandler/MMapHandlerFactory.java index 095e6ad..8c39822 100755 --- a/android/src/main/java/com/imfunc/flutter_minemap/views/mapHandler/MMapHandlerFactory.java +++ b/android/src/main/java/com/imfunc/flutter_minemap/views/mapHandler/MMapHandlerFactory.java @@ -45,6 +45,7 @@ public void dispatchMethodHandler(Context context, MethodCall call, /// 对不同类型的操作进行分治管理 switch (methodId) { case Constants.FMMMapStateMethodId.sMapSetStyleMethod: + case Constants.FMMMapStateMethodId.sMapSetCenterMethod: mMapHandler = mMapHandlerHashMap.get(Constants.MMapHandlerType.MAP_STATE); break; case Constants.FMMClusterLayerMethodId.sMapAddClusterLayerMethod: diff --git a/android/src/main/java/com/imfunc/flutter_minemap/views/mapHandler/MapStateHandler.java b/android/src/main/java/com/imfunc/flutter_minemap/views/mapHandler/MapStateHandler.java index 14ecb17..9d4380b 100755 --- a/android/src/main/java/com/imfunc/flutter_minemap/views/mapHandler/MapStateHandler.java +++ b/android/src/main/java/com/imfunc/flutter_minemap/views/mapHandler/MapStateHandler.java @@ -5,16 +5,19 @@ import com.imfunc.flutter_minemap.FMMMapController; import com.imfunc.flutter_minemap.unil.Constants; +import com.imfunc.flutter_minemap.unil.conveter.FMMMapConveter; +import com.minedata.minemap.geometry.LatLng; import com.minedata.minemap.map.MineMap; +import java.util.Map; + import io.flutter.plugin.common.MethodCall; import io.flutter.plugin.common.MethodChannel; public class MapStateHandler extends MMapHandler { private static final String TAG = MapStateHandler.class.getSimpleName(); private MineMap mineMap; - - + public MapStateHandler(FMMMapController mMapController) { super(mMapController); @@ -35,13 +38,27 @@ public void handlerMethodCallResult(Context context, MethodCall call, MethodChan case Constants.FMMMapStateMethodId.sMapSetStyleMethod: setMapType(call, result); break; + case Constants.FMMMapStateMethodId.sMapSetCenterMethod: + setCenter(call, result); + break; default: break; } } + /** + * @param call + * @param result + */ + private void setCenter(MethodCall call, MethodChannel.Result result) { + LatLng center = FMMMapConveter.mapToLatlng((Map) call.arguments); + mMapController.setCenter(center); + result.success(true); + } + /** * 设置地图样式 + * * @param call * @param result */ @@ -50,7 +67,7 @@ private void setMapType(MethodCall call, Integer mapType = call.arguments(); mMapController.setMapType(mapType); - + result.success(true); } } diff --git a/example/lib/view/base_map_page.dart b/example/lib/view/base_map_page.dart index e0d68ae..9d2947c 100755 --- a/example/lib/view/base_map_page.dart +++ b/example/lib/view/base_map_page.dart @@ -5,20 +5,35 @@ import 'package:flutter_minemap/models/fmm_map_models.dart'; import 'package:flutter_minemap/models/fmm_types.dart'; import 'package:flutter_minemap_example/config/config.dart'; -class BaseMapPage extends StatelessWidget { +class BaseMapPage extends StatefulWidget { const BaseMapPage({Key key}) : super(key: key); @override - Widget build(BuildContext context) { - MineMapController _mineMapController; + _BaseMapPageState createState() => _BaseMapPageState(); +} + +class _BaseMapPageState extends State { + MineMapController _mineMapController; + @override + Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('地图'), ), - body: Container( - height: double.infinity, - width: double.infinity, + body: Column( + children: [ + _mapWidget(), + _makeWidget(context), + ], + ), + ); + } + + Widget _mapWidget() { + return Expanded( + child: Container( + width: MediaQuery.of(context).size.width, alignment: Alignment.center, child: MineMapView( mapOptions: FMMMapOptions( @@ -36,4 +51,48 @@ class BaseMapPage extends StatelessWidget { ), ); } + + Widget _makeWidget(BuildContext context) { + final List _btnL = [ + CustomButtonModel( + '北京', + () => _mineMapController + .setMapCenter(FMMCoordinate(39.914687, 116.403613))), + CustomButtonModel( + '上海', + () => _mineMapController + .setMapCenter(FMMCoordinate(31.233706, 121.477091))), + CustomButtonModel( + '济南', + () => _mineMapController + .setMapCenter(FMMCoordinate(36.656786, 117.122375))), + CustomButtonModel( + '珠海', + () => _mineMapController + .setMapCenter(FMMCoordinate(22.26827, 113.573358))), + ]; + + List _l = []; + + for (CustomButtonModel e in _btnL) { + _l.add(TextButton( + onPressed: e.func, + child: Text(e.name), + )); + } + + return Container( + width: MediaQuery.of(context).size.width, + child: Wrap( + children: _l, + ), + ); + } +} + +class CustomButtonModel { + final String name; + final Function func; + + CustomButtonModel(this.name, this.func); } diff --git a/lib/mimemap_controler.dart b/lib/mimemap_controler.dart index ed303e6..a0dc730 100755 --- a/lib/mimemap_controler.dart +++ b/lib/mimemap_controler.dart @@ -4,6 +4,8 @@ import 'package:flutter_minemap/models/overlays/cluster_layer_model.dart'; import 'package:flutter_minemap/private/fmm_method_channel_handler.dart'; import 'package:flutter_minemap/private/fmm_method_id.dart'; +import 'models/fmm_map_models.dart'; + class MineMapController { MethodChannel _mapChannel; @@ -36,6 +38,21 @@ class MineMapController { return result; } + /// 设置中心点 + Future setMapCenter(FMMCoordinate coordinate) async { + if (_mapChannel == null) { + return false; + } + bool result = false; + try { + result = (await _mapChannel.invokeMethod( + FMMMapStateMethodId.kMapSetCenterMethod, coordinate.toMap())) as bool; + } on PlatformException catch (e) { + print(e.toString()); + } + return result; + } + /// 添加聚合点图层 Future addClusterLayer(ClusterLayerModel params) async { print(params);