Skip to content

Commit b3f765c

Browse files
author
huangjiahong
committed
添加设置中心点
1 parent bb9a703 commit b3f765c

File tree

5 files changed

+104
-9
lines changed

5 files changed

+104
-9
lines changed

android/src/main/java/com/imfunc/flutter_minemap/unil/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ public static class ViewType {
1515
*/
1616
public static class FMMMapStateMethodId {
1717
public static final String sMapSetStyleMethod = "flutter_minemap/map/setStyleJson";
18+
public static final String sMapSetCenterMethod = "flutter_minemap/map/setCenter";
1819
}
1920

2021
/**

android/src/main/java/com/imfunc/flutter_minemap/views/mapHandler/MMapHandlerFactory.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public void dispatchMethodHandler(Context context, MethodCall call,
4545
/// 对不同类型的操作进行分治管理
4646
switch (methodId) {
4747
case Constants.FMMMapStateMethodId.sMapSetStyleMethod:
48+
case Constants.FMMMapStateMethodId.sMapSetCenterMethod:
4849
mMapHandler = mMapHandlerHashMap.get(Constants.MMapHandlerType.MAP_STATE);
4950
break;
5051
case Constants.FMMClusterLayerMethodId.sMapAddClusterLayerMethod:

android/src/main/java/com/imfunc/flutter_minemap/views/mapHandler/MapStateHandler.java

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55

66
import com.imfunc.flutter_minemap.FMMMapController;
77
import com.imfunc.flutter_minemap.unil.Constants;
8+
import com.imfunc.flutter_minemap.unil.conveter.FMMMapConveter;
9+
import com.minedata.minemap.geometry.LatLng;
810
import com.minedata.minemap.map.MineMap;
911

12+
import java.util.Map;
13+
1014
import io.flutter.plugin.common.MethodCall;
1115
import io.flutter.plugin.common.MethodChannel;
1216

1317
public class MapStateHandler extends MMapHandler {
1418
private static final String TAG = MapStateHandler.class.getSimpleName();
1519
private MineMap mineMap;
16-
17-
20+
1821

1922
public MapStateHandler(FMMMapController mMapController) {
2023
super(mMapController);
@@ -35,13 +38,27 @@ public void handlerMethodCallResult(Context context, MethodCall call, MethodChan
3538
case Constants.FMMMapStateMethodId.sMapSetStyleMethod:
3639
setMapType(call, result);
3740
break;
41+
case Constants.FMMMapStateMethodId.sMapSetCenterMethod:
42+
setCenter(call, result);
43+
break;
3844
default:
3945
break;
4046
}
4147
}
4248

49+
/**
50+
* @param call
51+
* @param result
52+
*/
53+
private void setCenter(MethodCall call, MethodChannel.Result result) {
54+
LatLng center = FMMMapConveter.mapToLatlng((Map<String, Object>) call.arguments);
55+
mMapController.setCenter(center);
56+
result.success(true);
57+
}
58+
4359
/**
4460
* 设置地图样式
61+
*
4562
* @param call
4663
* @param result
4764
*/
@@ -50,7 +67,7 @@ private void setMapType(MethodCall call,
5067
Integer mapType = call.arguments();
5168

5269
mMapController.setMapType(mapType);
53-
70+
5471
result.success(true);
5572
}
5673
}

example/lib/view/base_map_page.dart

Lines changed: 65 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,35 @@ import 'package:flutter_minemap/models/fmm_map_models.dart';
55
import 'package:flutter_minemap/models/fmm_types.dart';
66
import 'package:flutter_minemap_example/config/config.dart';
77

8-
class BaseMapPage extends StatelessWidget {
8+
class BaseMapPage extends StatefulWidget {
99
const BaseMapPage({Key key}) : super(key: key);
1010

1111
@override
12-
Widget build(BuildContext context) {
13-
MineMapController _mineMapController;
12+
_BaseMapPageState createState() => _BaseMapPageState();
13+
}
14+
15+
class _BaseMapPageState extends State<BaseMapPage> {
16+
MineMapController _mineMapController;
1417

18+
@override
19+
Widget build(BuildContext context) {
1520
return Scaffold(
1621
appBar: AppBar(
1722
title: Text('地图'),
1823
),
19-
body: Container(
20-
height: double.infinity,
21-
width: double.infinity,
24+
body: Column(
25+
children: [
26+
_mapWidget(),
27+
_makeWidget(context),
28+
],
29+
),
30+
);
31+
}
32+
33+
Widget _mapWidget() {
34+
return Expanded(
35+
child: Container(
36+
width: MediaQuery.of(context).size.width,
2237
alignment: Alignment.center,
2338
child: MineMapView(
2439
mapOptions: FMMMapOptions(
@@ -36,4 +51,48 @@ class BaseMapPage extends StatelessWidget {
3651
),
3752
);
3853
}
54+
55+
Widget _makeWidget(BuildContext context) {
56+
final List<CustomButtonModel> _btnL = [
57+
CustomButtonModel(
58+
'北京',
59+
() => _mineMapController
60+
.setMapCenter(FMMCoordinate(39.914687, 116.403613))),
61+
CustomButtonModel(
62+
'上海',
63+
() => _mineMapController
64+
.setMapCenter(FMMCoordinate(31.233706, 121.477091))),
65+
CustomButtonModel(
66+
'济南',
67+
() => _mineMapController
68+
.setMapCenter(FMMCoordinate(36.656786, 117.122375))),
69+
CustomButtonModel(
70+
'珠海',
71+
() => _mineMapController
72+
.setMapCenter(FMMCoordinate(22.26827, 113.573358))),
73+
];
74+
75+
List<Widget> _l = [];
76+
77+
for (CustomButtonModel e in _btnL) {
78+
_l.add(TextButton(
79+
onPressed: e.func,
80+
child: Text(e.name),
81+
));
82+
}
83+
84+
return Container(
85+
width: MediaQuery.of(context).size.width,
86+
child: Wrap(
87+
children: _l,
88+
),
89+
);
90+
}
91+
}
92+
93+
class CustomButtonModel {
94+
final String name;
95+
final Function func;
96+
97+
CustomButtonModel(this.name, this.func);
3998
}

lib/mimemap_controler.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import 'package:flutter_minemap/models/overlays/cluster_layer_model.dart';
44
import 'package:flutter_minemap/private/fmm_method_channel_handler.dart';
55
import 'package:flutter_minemap/private/fmm_method_id.dart';
66

7+
import 'models/fmm_map_models.dart';
8+
79
class MineMapController {
810
MethodChannel _mapChannel;
911

@@ -36,6 +38,21 @@ class MineMapController {
3638
return result;
3739
}
3840

41+
/// 设置中心点
42+
Future<bool> setMapCenter(FMMCoordinate coordinate) async {
43+
if (_mapChannel == null) {
44+
return false;
45+
}
46+
bool result = false;
47+
try {
48+
result = (await _mapChannel.invokeMethod(
49+
FMMMapStateMethodId.kMapSetCenterMethod, coordinate.toMap())) as bool;
50+
} on PlatformException catch (e) {
51+
print(e.toString());
52+
}
53+
return result;
54+
}
55+
3956
/// 添加聚合点图层
4057
Future<bool> addClusterLayer(ClusterLayerModel params) async {
4158
print(params);

0 commit comments

Comments
 (0)