app_map 0.0.3
app_map: ^0.0.3 copied to clipboard
map插件
example/lib/main.dart
import 'package:app_map/app_map.dart';
import 'package:app_map/app_map_controller.dart';
import 'package:app_map/app_map_setting.dart';
import 'package:app_map/app_map_view.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({super.key});
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
final _appMapPlugin = AppMap();
late AppMapController _appMapController;
Uint8List? _uint8list;
bool _isEnable = true;
double _zoom = 1;
final List<Map<String, double>> _coordinates = [];
double _latitude = 39.912;
double _longitude = 112.423;
@override
void initState() {
_appMapPlugin.init();
super.initState();
}
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
body: SafeArea(
top: false,
child: Column(
children: [
Expanded(
child: AppMapView(
latitude: 39.908692,
longitude: 116.397477,
zoom: 5,
onMapViewCreated: (controller) {
_appMapController = controller;
},
onMarkerClick: (index) {
print("onMarkerClick:$index");
},
onMapRegionChanged: (latitude, longitude, zoom) {
print("onMapRegionChanged:$latitude,$longitude,$zoom");
_latitude = latitude;
_longitude = longitude;
},
),
),
const SizedBox(height: 20),
if (_uint8list != null) Image.memory(_uint8list!, width: 400, height: 400),
Wrap(
spacing: 10,
runSpacing: 10,
children: [
buttonItem("设置卫星图", () {
_appMapController.setMapType(MapType.hybrid.name);
}),
buttonItem("设置标准地图", () {
_appMapController.setMapType(MapType.standard.name);
}),
buttonItem("设置日间模式", () {
_appMapController.setMapStyle(MapStyle.light.name);
}),
buttonItem("设置夜间模式", () {
_appMapController.setMapStyle(MapStyle.dark.name);
}),
buttonItem("设置位置", () {
_appMapController.updateLocation(39.912, 112.423, true);
}),
buttonItem("设置缩放等级", () {
print("zoom:$_zoom");
_appMapController.setZoomLevel(_zoom, 39.912, 112.423);
_zoom++;
}),
buttonItem("设置缩放到显示所有点", () {
if (_coordinates.isEmpty) {
return;
}
_appMapController.setIncludeMarkers(_coordinates, true);
}),
buttonItem("添加本地图片marker", () {
_appMapController.addMarker(
latitude: 39.912,
longitude: 112.423,
imageAsset: "assets/images/location.png",
imageBytes: null,
index: 0,
center: false,
title: "hhh");
}),
buttonItem("添加data marker", () async {
final ByteData byteData = await rootBundle.load("assets/images/location.png");
final Uint8List imageBytes = byteData.buffer.asUint8List();
_appMapController.addMarker(
latitude: 39.712,
longitude: 112.823,
imageAsset: null,
imageBytes: imageBytes,
index: 1,
center: true,
);
}),
buttonItem("添加折线", () {
List<Map<String, double>> coordinates = [];
coordinates.add({"latitude": 39.912, "longitude": 112.423});
coordinates.add({"latitude": 39.812, "longitude": 112.423});
coordinates.add({"latitude": 39.712, "longitude": 112.423});
coordinates.add({"latitude": 39.612, "longitude": 112.423});
_appMapController.addPolyline(coordinates, 1, color: Colors.green, lineWidth: 2, isDashed: true);
}),
buttonItem("更新折线", () {
List<Map<String, double>> coordinates = [];
coordinates.add({"latitude": 39.912, "longitude": 112.423});
coordinates.add({"latitude": 39.812, "longitude": 112.623});
coordinates.add({"latitude": 39.712, "longitude": 112.823});
coordinates.add({"latitude": 39.612, "longitude": 112.923});
_appMapController.updatePolyline(coordinates, 1, color: Colors.blue, lineWidth: 4, isDashed: false);
}),
buttonItem("清除marker", () {
_appMapController.cleanAllMarkers();
}),
buttonItem("清除折线", () {
_appMapController.cleanPolyline();
}),
buttonItem("屏幕截图", () async {
Uint8List? data = await _appMapController.takeSnapshot();
if (data != null) {
setState(() {
_uint8list = data;
});
}
}),
buttonItem("设置手势开关", () {
_isEnable = !_isEnable;
_appMapController.setGesturesEnabled(_isEnable);
}),
buttonItem("添加中心点折线", () {
_coordinates.add({"latitude": _latitude, "longitude": _longitude});
if (_coordinates.length > 1) {
_appMapController.addPolyline(_coordinates, 2, color: Colors.red, lineWidth: 2, isDashed: true);
} else if (_coordinates.length > 2) {
_appMapController.updatePolyline(_coordinates, 2, color: Colors.red, lineWidth: 2, isDashed: true);
}
_appMapController.addMarker(
latitude: _latitude,
longitude: _longitude,
imageAsset: "assets/images/location.png",
imageBytes: null,
index: _coordinates.length);
}),
],
)
],
),
),
),
);
}
Widget buttonItem(String text, Function onTap) {
return GestureDetector(
onTap: () {
onTap();
},
child: Container(
decoration: BoxDecoration(color: Colors.blue, borderRadius: BorderRadius.circular(10)),
padding: const EdgeInsets.symmetric(vertical: 5, horizontal: 10),
margin: const EdgeInsets.only(right: 10),
child: Text(text, style: const TextStyle(color: Colors.white, fontSize: 14)),
),
);
}
}