main.dart 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import 'package:flutter/material.dart';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/services.dart';
  4. import 'dart:math';
  5. void main() {
  6. runApp(const MyApp());
  7. }
  8. class MyApp extends StatelessWidget {
  9. const MyApp({super.key});
  10. // This widget is the root of your application.
  11. @override
  12. Widget build(BuildContext context) {
  13. return MaterialApp(
  14. title: 'Flutter Demo',
  15. theme: ThemeData(
  16. colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
  17. ),
  18. home: const MyHomePage(title: 'Flutter Demo Home Page'),
  19. );
  20. }
  21. }
  22. class MyHomePage extends StatefulWidget {
  23. const MyHomePage({super.key, required this.title});
  24. final String title;
  25. @override
  26. State<MyHomePage> createState() => _MyHomePageState();
  27. }
  28. class _MyHomePageState extends State<MyHomePage> {
  29. MethodChannel? _methodChannel;
  30. void onViewCreated(int viewId) {
  31. _methodChannel = MethodChannel("atmob_map_method_channel");
  32. _methodChannel?.setMethodCallHandler((call) {
  33. if (call.method == "marker#onTap") {
  34. debugPrint("Marker tapped: ${call.arguments}");
  35. }
  36. return Future.value(null);
  37. });
  38. }
  39. @override
  40. Widget build(BuildContext context) {
  41. return CupertinoPageScaffold(
  42. child: Stack(
  43. children: [
  44. UiKitView(
  45. viewType: "com.atmob.flutter.map",
  46. onPlatformViewCreated: onViewCreated
  47. ),
  48. Positioned(
  49. bottom: 0,
  50. left: 0,
  51. right: 0,
  52. child: Column(
  53. children: [
  54. CupertinoButton(
  55. child: Text("Random Position"),
  56. onPressed: () {
  57. _methodChannel?.invokeMethod("map#moveCamera", {
  58. "latitude": Random().nextDouble() * (40.0 - 22.0) + 22.0,
  59. "longitude": Random().nextDouble() * (120.0 - 110.0) + 110.0,
  60. "zoom": 0.05
  61. });
  62. }),
  63. CupertinoButton(
  64. child: Text("Add Marker"),
  65. onPressed: () {
  66. _methodChannel?.invokeMethod("marker#updateOrAddMarkers", {
  67. "id": "marker_${Random().nextInt(1000000)}",
  68. "latitude": Random().nextDouble() * (40.0 - 22.0) + 22.0,
  69. "longitude": Random().nextDouble() * (120.0 - 110.0) + 110.0,
  70. "markerName": "Marker ${Random().nextInt(1000000)}",
  71. "markerType": 1,
  72. "isSelected": false
  73. });
  74. }
  75. ),
  76. CupertinoButton(
  77. child: Text("Clear Markers"),
  78. onPressed: () {
  79. _methodChannel?.invokeMethod("map#clear");
  80. }
  81. ),
  82. CupertinoButton(
  83. child: Text("Add Polyline"),
  84. onPressed: () {
  85. final points = List<Map<String, double>>.generate(20, (_) => {
  86. "latitude": Random().nextDouble() * (40.0 - 22.0) + 22.0,
  87. "longitude": Random().nextDouble() * (120.0 - 110.0) + 110.0
  88. });
  89. _methodChannel?.invokeMethod("polyline#addPolyline", {
  90. "points": points,
  91. "mapPadding": {
  92. "top": 10,
  93. "left": 10,
  94. "bottom": 600,
  95. "right": 10
  96. }
  97. });
  98. // start marker
  99. _methodChannel?.invokeMethod("marker#updateOrAddMarkers", {
  100. "id": "marker_${Random().nextInt(1000000)}",
  101. "latitude": points[0]["latitude"],
  102. "longitude": points[0]["longitude"],
  103. "markerName": "",
  104. "markerType": 3,
  105. "isSelected": false
  106. });
  107. // end marker
  108. _methodChannel?.invokeMethod("marker#updateOrAddMarkers", {
  109. "id": "marker_${Random().nextInt(1000000)}",
  110. "latitude": points[points.length - 1]["latitude"],
  111. "longitude": points[points.length - 1]["longitude"],
  112. "markerName": "Marker ${Random().nextInt(1000000)}",
  113. "markerType": 4,
  114. "isSelected": false
  115. });
  116. }
  117. )
  118. ],
  119. )
  120. )
  121. ],
  122. ),
  123. );
  124. }
  125. }