amap_fl_plugin 0.1.3
amap_fl_plugin: ^0.1.3 copied to clipboard
Flutter plugin for AMap (Gaode) with 2D map, marker and location support.
example/lib/main.dart
import 'package:amap_fl_plugin/amap_fl_plugin.dart';
import 'package:flutter/material.dart';
import 'package:permission_handler/permission_handler.dart';
void main() {
runApp(const MyApp());
}
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: const AMapDemoPage(),
theme: ThemeData(useMaterial3: true, colorSchemeSeed: Colors.blue),
);
}
}
class AMapDemoPage extends StatefulWidget {
const AMapDemoPage({super.key});
@override
State<AMapDemoPage> createState() => _AMapDemoPageState();
}
class _AMapDemoPageState extends State<AMapDemoPage> {
AMapFlPluginController? _controller;
String _status = 'Map initializing...';
AMapLatLng? _lastLocation;
bool _didAutoCenterToUser = false;
final List<AMapLatLng> _simulatedTrack = <AMapLatLng>[];
static const _beijing = AMapLatLng(latitude: 39.909187, longitude: 116.397451);
static const _track = <AMapLatLng>[
AMapLatLng(latitude: 39.9085, longitude: 116.3970),
AMapLatLng(latitude: 39.9092, longitude: 116.3990),
AMapLatLng(latitude: 39.9100, longitude: 116.4015),
AMapLatLng(latitude: 39.9120, longitude: 116.4040),
];
@override
void initState() {
super.initState();
_requestLocationPermission();
}
Future<void> _requestLocationPermission() async {
final status = await Permission.locationWhenInUse.request();
if (!mounted) return;
if (!status.isGranted) {
setState(() {
_status = '定位权限未授权,定位功能不可用';
});
}
}
@override
void dispose() {
_controller?.dispose();
super.dispose();
}
Future<void> _offsetFromCurrent(double dLat, double dLng) async {
final point = _lastLocation;
if (point == null) {
setState(() {
_status = '还没拿到定位,无法偏移';
});
return;
}
final next = AMapLatLng(
latitude: point.latitude + dLat,
longitude: point.longitude + dLng,
);
setState(() {
_lastLocation = next;
if (_simulatedTrack.isEmpty) {
_simulatedTrack.add(point);
}
_simulatedTrack.add(next);
_status =
'模拟偏移到: ${next.latitude.toStringAsFixed(6)}, ${next.longitude.toStringAsFixed(6)}';
});
await _controller?.setCenter(next, zoom: 17);
if (_simulatedTrack.length >= 2) {
await _controller?.setTrack(_simulatedTrack, color: 0xFF00897B, width: 10);
}
}
Future<void> _resetSimulatedTrack() async {
setState(() {
_simulatedTrack.clear();
_status = '已清空模拟轨迹';
});
await _controller?.clearTrack();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('AMap FL Plugin Example')),
body: Column(
children: [
Expanded(
child: AMapView(
initialCenter: _beijing,
initialZoom: 14,
myLocationEnabled: true,
markers: const [
AMapMarker(
id: 'tiananmen',
position: _beijing,
title: '天安门',
snippet: '示例标记点',
),
],
onMapCreated: (controller) {
_controller = controller;
controller.onMapTap.listen((point) {
setState(() {
_status = 'Tap: ${point.latitude}, ${point.longitude}';
});
});
controller.onLocationChanged.listen((point) {
final isInvalid =
point.latitude.abs() < 0.000001 && point.longitude.abs() < 0.000001;
if (isInvalid) {
setState(() {
_status = '收到无效定位(0,0),已忽略';
});
return;
}
setState(() {
_lastLocation = point;
_status = 'Location: ${point.latitude}, ${point.longitude}';
});
if (!_didAutoCenterToUser) {
_didAutoCenterToUser = true;
controller.setCenter(point, zoom: 16);
}
});
},
),
),
Padding(
padding: const EdgeInsets.all(12),
child: Text(_status),
),
Padding(
padding: const EdgeInsets.only(bottom: 16),
child: Wrap(
spacing: 8,
runSpacing: 8,
children: [
ElevatedButton(
onPressed: () {
_controller?.setCenter(_beijing, zoom: 16);
},
child: const Text('回到北京中心'),
),
ElevatedButton(
onPressed: () {
final point = _lastLocation;
if (point == null) {
setState(() {
_status = '还没拿到定位,请稍等或检查定位权限';
});
return;
}
final isInvalid =
point.latitude.abs() < 0.000001 && point.longitude.abs() < 0.000001;
if (isInvalid) {
setState(() {
_status = '当前定位无效(0,0),已阻止跳转';
});
return;
}
_controller?.setCenter(point, zoom: 17);
},
child: const Text('定位到我'),
),
ElevatedButton(
onPressed: () {
_controller?.addMarker(
const AMapMarker(
id: 'new_marker',
position: AMapLatLng(latitude: 39.915, longitude: 116.404),
title: '新Marker',
snippet: '动态添加',
),
);
},
child: const Text('添加Marker'),
),
ElevatedButton(
onPressed: () {
_controller?.clearMarkers();
},
child: const Text('清空Marker'),
),
ElevatedButton(
onPressed: () {
_controller?.setTrack(_track, color: 0xFFE53935, width: 10);
},
child: const Text('绘制固定轨迹'),
),
ElevatedButton(
onPressed: () {
_controller?.clearTrack();
},
child: const Text('清除轨迹'),
),
ElevatedButton(
onPressed: () => _offsetFromCurrent(0.0005, 0),
child: const Text('模拟北移'),
),
ElevatedButton(
onPressed: () => _offsetFromCurrent(0, 0.0005),
child: const Text('模拟东移'),
),
ElevatedButton(
onPressed: _resetSimulatedTrack,
child: const Text('清空模拟轨迹'),
),
],
),
),
],
),
);
}
}