init project

This commit is contained in:
Kuloud
2023-12-22 21:23:24 +08:00
commit 1fb3d91106
461 changed files with 58770 additions and 0 deletions

View File

@ -0,0 +1,91 @@
import 'package:amap_map/amap_map.dart';
import 'package:amap_map/amap_map.dart';
import 'package:amap_map_example/base_page.dart';
import 'package:amap_map_example/const_config.dart';
import 'package:flutter/material.dart';
class MarkerAddAfterMapPage extends BasePage {
MarkerAddAfterMapPage(String title, String subTitle) : super(title, subTitle);
@override
Widget build(BuildContext context) => _Body();
}
class _Body extends StatefulWidget {
@override
_BodyState createState() => _BodyState();
}
class _BodyState extends State<_Body> {
static final LatLng defaultPosition = const LatLng(39.909187, 116.397451);
//需要先设置一个空的map赋值给AMapWidget的markers否则后续无法添加marker
final Map<String, Marker> _markers = <String, Marker>{};
LatLng _currentLatLng = defaultPosition;
//添加一个marker
void _addMarker() {
final _markerPosition =
LatLng(_currentLatLng.latitude, _currentLatLng.longitude + 2 / 1000);
final Marker marker = Marker(
position: _markerPosition,
//使用默认hue的方式设置Marker的图标
icon: BitmapDescriptor.defaultMarkerWithHue(BitmapDescriptor.hueOrange),
);
//调用setState触发AMapWidget的更新从而完成marker的添加
setState(() {
_currentLatLng = _markerPosition;
//将新的marker添加到map里
_markers[marker.id] = marker;
});
}
TextButton _createMyFloatButton(String label, VoidCallback onPressed) {
return TextButton(
onPressed: onPressed,
style: ButtonStyle(
shape: MaterialStateProperty.all(
RoundedRectangleBorder(borderRadius: BorderRadius.circular(10))),
//文字颜色
foregroundColor: MaterialStateProperty.all(Colors.white),
//水波纹颜色
overlayColor: MaterialStateProperty.all(Colors.blueAccent),
//背景颜色
backgroundColor: MaterialStateProperty.resolveWith((states) {
//设置按下时的背景颜色
if (states.contains(MaterialState.pressed)) {
return Colors.blueAccent;
}
//默认背景颜色
return Colors.blue;
}),
),
child: Text(label),
);
}
@override
Widget build(BuildContext context) {
final AMapWidget amap = AMapWidget(
apiKey: ConstConfig.amapApiKeys,
// //创建地图时给marker属性赋值一个空的set否则后续无法添加marker
markers: Set<Marker>.of(_markers.values),
);
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
flex: 10,
child: amap,
),
Expanded(
flex: 1,
child: _createMyFloatButton('添加marker', _addMarker),
),
],
),
);
}
}

View File

@ -0,0 +1,42 @@
import 'dart:math';
import 'package:amap_map/amap_map.dart';
import 'package:amap_map/amap_map.dart';
import 'package:amap_map_example/base_page.dart';
import 'package:amap_map_example/const_config.dart';
import 'package:flutter/material.dart';
class MarkerAddWithMapPage extends BasePage {
MarkerAddWithMapPage(String title, String subTitle) : super(title, subTitle);
@override
Widget build(BuildContext context) => _Body();
}
class _Body extends StatefulWidget {
@override
_BodyState createState() => _BodyState();
}
class _BodyState extends State<_Body> {
static final LatLng mapCenter = const LatLng(39.909187, 116.397451);
final Map<String, Marker> _initMarkerMap = <String, Marker>{};
@override
Widget build(BuildContext context) {
for (int i = 0; i < 10; i++) {
LatLng position = LatLng(mapCenter.latitude + sin(i * pi / 12.0) / 20.0,
mapCenter.longitude + cos(i * pi / 12.0) / 20.0);
Marker marker = Marker(position: position);
_initMarkerMap[marker.id] = marker;
}
final AMapWidget amap = AMapWidget(
apiKey: ConstConfig.amapApiKeys,
markers: Set<Marker>.of(_initMarkerMap.values),
);
return Container(
child: amap,
);
}
}

View File

@ -0,0 +1,343 @@
import 'dart:async';
import 'dart:typed_data';
import 'dart:ui';
import 'package:amap_map_example/base_page.dart';
import 'package:amap_map_example/const_config.dart';
import 'package:amap_map_example/widgets/amap_switch_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:amap_map/amap_map.dart';
import 'package:amap_map/amap_map.dart';
import 'dart:math';
class MarkerConfigDemoPage extends BasePage {
MarkerConfigDemoPage(String title, String subTitle) : super(title, subTitle);
@override
Widget build(BuildContext context) {
return _Body();
}
}
class _Body extends StatefulWidget {
const _Body();
@override
State<StatefulWidget> createState() => _State();
}
class _State extends State<_Body> {
static final LatLng mapCenter = const LatLng(39.909187, 116.397451);
Map<String, Marker> _markers = <String, Marker>{};
BitmapDescriptor? _markerIcon;
String? selectedMarkerId;
void _onMapCreated(AMapController controller) {}
///通过BitmapDescriptor.fromAssetImage的方式获取图片
Future<void> _createMarkerImageFromAsset(BuildContext context) async {
if (_markerIcon == null) {
final ImageConfiguration imageConfiguration =
createLocalImageConfiguration(context);
BitmapDescriptor.fromAssetImage(imageConfiguration, 'assets/start.png')
.then(_updateBitmap);
}
}
///通过BitmapDescriptor.fromBytes的方式获取图片
Future<void> _createMarkerImageFromBytes(BuildContext context) async {
final Completer<BitmapDescriptor> bitmapIcon =
Completer<BitmapDescriptor>();
final ImageConfiguration config = createLocalImageConfiguration(context);
const AssetImage('assets/end.png')
.resolve(config)
.addListener(ImageStreamListener((ImageInfo image, bool sync) async {
final ByteData bytes =
(await image.image.toByteData(format: ImageByteFormat.png))!;
final BitmapDescriptor bitmap =
BitmapDescriptor.fromBytes(bytes.buffer.asUint8List());
bitmapIcon.complete(bitmap);
}));
bitmapIcon.future.then((value) => _updateBitmap(value));
}
void _updateBitmap(BitmapDescriptor bitmap) {
setState(() {
_markerIcon = bitmap;
});
}
void _add() {
final int markerCount = _markers.length;
LatLng markPostion = LatLng(
mapCenter.latitude + sin(markerCount * pi / 12.0) / 20.0,
mapCenter.longitude + cos(markerCount * pi / 12.0) / 20.0);
final Marker marker = Marker(
position: markPostion,
icon: _markerIcon!,
infoWindow: InfoWindow(title: '$markerCount 个Marker'),
onTap: (markerId) => _onMarkerTapped(markerId),
onDragEnd: (markerId, endPosition) =>
_onMarkerDragEnd(markerId, endPosition),
);
setState(() {
_markers[marker.id] = marker;
});
}
void _onMarkerTapped(String markerId) {
final Marker? tappedMarker = _markers[markerId];
final String? title = tappedMarker!.infoWindow.title;
print('$title 被点击了,markerId: $markerId');
setState(() {
selectedMarkerId = markerId;
});
}
void _onMarkerDragEnd(String markerId, LatLng position) {
final Marker? tappedMarker = _markers[markerId];
final String? title = tappedMarker!.infoWindow.title;
print('$title markerId: $markerId 被拖拽到了: $position');
}
void _remove() {
final Marker? selectedMarker = _markers[selectedMarkerId];
//有选中的Marker
if (selectedMarker != null) {
setState(() {
_markers.remove(selectedMarkerId);
});
} else {
print('无选中的Marker无法删除');
}
}
void _removeAll() {
if (_markers.length > 0) {
setState(() {
_markers.clear();
selectedMarkerId = null.toString();
});
}
}
void _changeInfo() async {
final Marker marker = _markers[selectedMarkerId]!;
final String newTitle = marker.infoWindow.title! + '*';
if (selectedMarkerId != null) {
setState(() {
_markers[selectedMarkerId!] = marker.copyWith(
infoWindowParam: marker.infoWindow.copyWith(
titleParam: newTitle,
),
);
});
}
}
void _changeAnchor() {
final Marker marker = _markers[selectedMarkerId]!;
final Offset currentAnchor = marker.anchor;
double dx = 0;
double dy = 0;
if (currentAnchor.dx < 1) {
dx = currentAnchor.dx + 0.1;
} else {
dx = 0;
}
if (currentAnchor.dy < 1) {
dy = currentAnchor.dy + 0.1;
} else {
dy = 0;
}
final Offset newAnchor = Offset(dx, dy);
setState(() {
_markers[selectedMarkerId!] = marker.copyWith(
anchorParam: newAnchor,
);
});
}
void _changePosition() {
final Marker marker = _markers[selectedMarkerId]!;
final LatLng current = marker.position;
final Offset offset = Offset(
mapCenter.latitude - current.latitude,
mapCenter.longitude - current.longitude,
);
setState(() {
_markers[selectedMarkerId!] = marker.copyWith(
positionParam: LatLng(
mapCenter.latitude + offset.dy,
mapCenter.longitude + offset.dx,
),
);
});
}
Future<void> _changeAlpha() async {
final Marker marker = _markers[selectedMarkerId]!;
final double current = marker.alpha;
setState(() {
_markers[selectedMarkerId!] = marker.copyWith(
alphaParam: current < 0.1 ? 1.0 : current * 0.75,
);
});
}
Future<void> _changeRotation() async {
final Marker marker = _markers[selectedMarkerId]!;
final double current = marker.rotation;
setState(() {
_markers[selectedMarkerId!] = marker.copyWith(
rotationParam: current == 330.0 ? 0.0 : current + 30.0,
);
});
}
Future<void> _toggleVisible(value) async {
final Marker marker = _markers[selectedMarkerId]!;
setState(() {
_markers[selectedMarkerId!] = marker.copyWith(
visibleParam: value,
);
});
}
Future<void> _toggleDraggable(value) async {
final Marker marker = _markers[selectedMarkerId]!;
setState(() {
_markers[selectedMarkerId!] = marker.copyWith(
draggableParam: value,
);
});
}
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
///以下几种获取自定图片的方式使用其中一种即可。
//最简单的方式
if (null == _markerIcon) {
_markerIcon = BitmapDescriptor.fromIconPath('assets/location_marker.png');
}
//通过BitmapDescriptor.fromAssetImage的方式获取图片
// _createMarkerImageFromAsset(context);
//通过BitmapDescriptor.fromBytes的方式获取图片
// _createMarkerImageFromBytes(context);
final AMapWidget map = AMapWidget(
apiKey: ConstConfig.amapApiKeys,
onMapCreated: _onMapCreated,
markers: Set<Marker>.of(_markers.values),
);
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
height: MediaQuery.of(context).size.height * 0.6,
width: MediaQuery.of(context).size.width,
child: map,
),
Expanded(
child: SingleChildScrollView(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
children: <Widget>[
Column(
children: <Widget>[
TextButton(
child: const Text('添加'),
onPressed: _add,
),
TextButton(
child: const Text('移除'),
onPressed:
(selectedMarkerId == null) ? null : _remove,
),
TextButton(
child: const Text('更新InfoWidow'),
onPressed:
(selectedMarkerId == null) ? null : _changeInfo,
),
TextButton(
child: const Text('修改锚点'),
onPressed: (selectedMarkerId == null)
? null
: _changeAnchor,
),
TextButton(
child: const Text('修改透明度'),
onPressed: (selectedMarkerId == null)
? null
: _changeAlpha,
),
],
),
Column(
children: <Widget>[
TextButton(
child: const Text('全部移除'),
onPressed: _markers.length > 0 ? _removeAll : null,
),
AMapSwitchButton(
label: Text('允许拖动'),
onSwitchChanged: (selectedMarkerId == null)
? null
: _toggleDraggable,
defaultValue: false,
),
AMapSwitchButton(
label: Text('显示'),
onSwitchChanged: (selectedMarkerId == null)
? null
: _toggleVisible,
defaultValue: true,
),
TextButton(
child: const Text('修改坐标'),
onPressed: (selectedMarkerId == null)
? null
: _changePosition,
),
TextButton(
child: const Text('修改旋转角度'),
onPressed: (selectedMarkerId == null)
? null
: _changeRotation,
),
],
),
],
)
],
),
),
),
],
),
);
}
}

View File

@ -0,0 +1,99 @@
import 'package:amap_map/amap_map.dart';
import 'package:amap_map/amap_map.dart';
import 'package:amap_map_example/base_page.dart';
import 'package:amap_map_example/const_config.dart';
import 'package:flutter/material.dart';
class MarkerCustomIconPage extends BasePage {
MarkerCustomIconPage(String title, String subTitle) : super(title, subTitle);
@override
Widget build(BuildContext context) => _Body();
}
class _Body extends StatefulWidget {
@override
_BodyState createState() => _BodyState();
}
class _BodyState extends State<_Body> {
static final LatLng markerPosition = const LatLng(39.909187, 116.397451);
final Map<String, Marker> _initMarkerMap = <String, Marker>{};
String? _currentMarkerId;
bool _hasInitMarker = false;
static final String _startIconPath = 'assets/start.png';
static final String _endIconPath = 'assets/end.png';
String _iconPath = _startIconPath;
void _initMarker(BuildContext context) async {
if (_hasInitMarker) {
return;
}
Marker marker = Marker(
position: markerPosition,
icon: BitmapDescriptor.fromIconPath(_iconPath));
setState(() {
_hasInitMarker = true;
_currentMarkerId = marker.id;
_initMarkerMap[marker.id] = marker;
});
}
void _updateMarkerIcon() async {
Marker marker = _initMarkerMap[_currentMarkerId]!;
setState(() {
_iconPath = _iconPath == _startIconPath ? _endIconPath : _startIconPath;
_initMarkerMap[_currentMarkerId!] =
marker.copyWith(iconParam: BitmapDescriptor.fromIconPath(_iconPath));
});
}
TextButton _createMyFloatButton(String label, VoidCallback onPressed) {
return TextButton(
onPressed: onPressed,
style: ButtonStyle(
shape: MaterialStateProperty.all(
RoundedRectangleBorder(borderRadius: BorderRadius.circular(10))),
//文字颜色
foregroundColor: MaterialStateProperty.all(Colors.white),
//水波纹颜色
overlayColor: MaterialStateProperty.all(Colors.blueAccent),
//背景颜色
backgroundColor: MaterialStateProperty.resolveWith((states) {
//设置按下时的背景颜色
if (states.contains(MaterialState.pressed)) {
return Colors.blueAccent;
}
//默认背景颜色
return Colors.blue;
}),
),
child: Text(label),
);
}
@override
Widget build(BuildContext context) {
_initMarker(context);
final AMapWidget amap = AMapWidget(
apiKey: ConstConfig.amapApiKeys,
markers: Set<Marker>.of(_initMarkerMap.values),
);
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
flex: 10,
child: amap,
),
Expanded(
flex: 1,
child: _createMyFloatButton('更改图标', _updateMarkerIcon),
),
],
));
}
}

View File

@ -0,0 +1,221 @@
import 'package:amap_map_example/base_page.dart';
import 'package:amap_map_example/widgets/amap_switch_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:amap_map/amap_map.dart';
import 'package:amap_map/amap_map.dart';
class PolygonDemoPage extends BasePage {
PolygonDemoPage(String title, String subTitle) : super(title, subTitle);
@override
Widget build(BuildContext context) {
return _Body();
}
}
class _Body extends StatefulWidget {
const _Body();
@override
State<StatefulWidget> createState() => _State();
}
class _State extends State<_Body> {
_State();
// Values when toggling Polygon color
int colorsIndex = 0;
List<Color> colors = <Color>[
Colors.purple,
Colors.red,
Colors.green,
Colors.pink,
];
Map<String, Polygon> _polygons = <String, Polygon>{};
String? selectedPolygonId;
void _onMapCreated(AMapController controller) {}
LatLng _createLatLng(double lat, double lng) {
return LatLng(lat, lng);
}
List<LatLng> _createPoints() {
final List<LatLng> points = <LatLng>[];
final int polygonCount = _polygons.length;
final double offset = polygonCount * 0.01;
points.add(_createLatLng(39.835334 + offset, 116.3710069));
points.add(_createLatLng(39.843082 + offset, 116.3709830));
points.add(_createLatLng(39.845932 + offset, 116.3642213));
points.add(_createLatLng(39.845924 + offset, 116.3595219));
points.add(_createLatLng(39.841562 + offset, 116.345568));
points.add(_createLatLng(39.835347 + offset, 116.34575));
return points;
}
void _add() {
final Polygon polygon = Polygon(
strokeColor: colors[++colorsIndex % colors.length],
fillColor: colors[++colorsIndex % colors.length],
strokeWidth: 15,
points: _createPoints(),
);
setState(() {
selectedPolygonId = polygon.id;
_polygons[polygon.id] = polygon;
});
}
void _remove() {
if (selectedPolygonId != null) {
//有选中的Marker
setState(() {
_polygons.remove(selectedPolygonId);
});
}
}
void _changeStrokeWidth() {
final Polygon? selectedPolygon = _polygons[selectedPolygonId];
if (selectedPolygon != null) {
double currentWidth = selectedPolygon.strokeWidth;
if (currentWidth < 50) {
currentWidth += 10;
} else {
currentWidth = 5;
}
//有选中的Polygon
setState(() {
_polygons[selectedPolygonId!] =
selectedPolygon.copyWith(strokeWidthParam: currentWidth);
});
} else {
print('无选中的Polygon无法修改宽度');
}
}
void _changeColors() {
final Polygon polygon = _polygons[selectedPolygonId]!;
setState(() {
_polygons[selectedPolygonId!] = polygon.copyWith(
strokeColorParam: colors[++colorsIndex % colors.length],
fillColorParam: colors[(colorsIndex + 1) % colors.length],
);
});
}
Future<void> _toggleVisible(value) async {
final Polygon polygon = _polygons[selectedPolygonId]!;
setState(() {
_polygons[selectedPolygonId!] = polygon.copyWith(
visibleParam: value,
);
});
}
void _changePoints() {
final Polygon polygon = _polygons[selectedPolygonId]!;
List<LatLng> currentPoints = polygon.points;
List<LatLng> newPoints = <LatLng>[];
newPoints.addAll(currentPoints);
newPoints.add(LatLng(39.828809, 116.360364));
setState(() {
_polygons[selectedPolygonId!] = polygon.copyWith(
pointsParam: newPoints,
);
});
}
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
final AMapWidget map = AMapWidget(
initialCameraPosition:
CameraPosition(target: LatLng(39.828809, 116.360364), zoom: 13),
onMapCreated: _onMapCreated,
polygons: Set<Polygon>.of(_polygons.values),
);
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
height: MediaQuery.of(context).size.height * 0.6,
width: MediaQuery.of(context).size.width,
child: map,
),
Expanded(
child: SingleChildScrollView(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
children: <Widget>[
Column(
children: <Widget>[
TextButton(
child: const Text('添加'),
onPressed: _add,
),
TextButton(
child: const Text('删除'),
onPressed:
(selectedPolygonId == null) ? null : _remove,
),
TextButton(
child: const Text('修改边框宽度'),
onPressed: (selectedPolygonId == null)
? null
: _changeStrokeWidth,
),
],
),
Column(
children: <Widget>[
TextButton(
child: const Text('修改边框和填充色'),
onPressed: (selectedPolygonId == null)
? null
: _changeColors,
),
AMapSwitchButton(
label: Text('显示'),
onSwitchChanged: (selectedPolygonId == null)
? null
: _toggleVisible,
defaultValue: true,
),
TextButton(
child: const Text('修改坐标'),
onPressed: (selectedPolygonId == null)
? null
: _changePoints,
),
],
),
],
)
],
),
),
),
],
),
);
}
}

View File

@ -0,0 +1,304 @@
import 'package:amap_map_example/base_page.dart';
import 'package:amap_map_example/widgets/amap_switch_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:amap_map/amap_map.dart';
import 'package:amap_map/amap_map.dart';
class PolylineDemoPage extends BasePage {
PolylineDemoPage(String title, String subTitle) : super(title, subTitle);
@override
Widget build(BuildContext context) {
return _Body();
}
}
class _Body extends StatefulWidget {
const _Body();
@override
State<StatefulWidget> createState() => _State();
}
class _State extends State<_Body> {
_State();
// Values when toggling polyline color
int colorsIndex = 0;
List<Color> colors = <Color>[
Colors.purple,
Colors.red,
Colors.green,
Colors.pink,
];
Map<String, Polyline> _polylines = <String, Polyline>{};
String? selectedPolylineId;
void _onMapCreated(AMapController controller) {}
List<LatLng> _createPoints() {
final List<LatLng> points = <LatLng>[];
final int polylineCount = _polylines.length;
final double offset = polylineCount * -(0.01);
points.add(LatLng(39.938698 + offset, 116.275177));
points.add(LatLng(39.966069 + offset, 116.289253));
points.add(LatLng(39.944226 + offset, 116.306076));
points.add(LatLng(39.966069 + offset, 116.322899));
points.add(LatLng(39.938698 + offset, 116.336975));
return points;
}
void _add() {
final Polyline polyline = Polyline(
color: colors[++colorsIndex % colors.length],
width: 10,
points: _createPoints(),
onTap: _onPolylineTapped);
setState(() {
_polylines[polyline.id] = polyline;
});
}
void _remove() {
final Polyline? selectedPolyline = _polylines[selectedPolylineId];
//有选中的Marker
if (selectedPolyline != null) {
setState(() {
_polylines.remove(selectedPolylineId);
});
} else {
print('无选中的Polyline无法删除');
}
}
void _changeWidth() {
final Polyline? selectedPolyline = _polylines[selectedPolylineId]!;
//有选中的Polyline
if (selectedPolyline != null) {
double currentWidth = selectedPolyline.width;
if (currentWidth < 50) {
currentWidth += 10;
} else {
currentWidth = 5;
}
setState(() {
_polylines[selectedPolylineId!] =
selectedPolyline.copyWith(widthParam: currentWidth);
});
} else {
print('无选中的Polyline无法修改宽度');
}
}
void _onPolylineTapped(String polylineId) {
print('Polyline: $polylineId 被点击了');
setState(() {
selectedPolylineId = polylineId;
});
}
Future<void> _changeDashLineType() async {
final Polyline? polyline = _polylines[selectedPolylineId];
if (polyline == null) {
return;
}
DashLineType currentType = polyline.dashLineType;
if (currentType.index < DashLineType.circle.index) {
currentType = DashLineType.values[currentType.index + 1];
} else {
currentType = DashLineType.none;
}
setState(() {
_polylines[selectedPolylineId!] =
polyline.copyWith(dashLineTypeParam: currentType);
});
}
void _changeCapType() {
final Polyline? polyline = _polylines[selectedPolylineId]!;
if (polyline == null) {
return;
}
CapType capType = polyline.capType;
if (capType.index < CapType.round.index) {
capType = CapType.values[capType.index + 1];
} else {
capType = CapType.butt;
}
setState(() {
_polylines[selectedPolylineId!] =
polyline.copyWith(capTypeParam: capType);
});
}
void _changeJointType() {
final Polyline polyline = _polylines[selectedPolylineId]!;
JoinType joinType = polyline.joinType;
if (joinType.index < JoinType.round.index) {
joinType = JoinType.values[joinType.index + 1];
} else {
joinType = JoinType.bevel;
}
setState(() {
_polylines[selectedPolylineId!] =
polyline.copyWith(joinTypeParam: joinType);
});
}
Future<void> _changeAlpha() async {
final Polyline polyline = _polylines[selectedPolylineId]!;
final double current = polyline.alpha;
setState(() {
_polylines[selectedPolylineId!] = polyline.copyWith(
alphaParam: current < 0.1 ? 1.0 : current * 0.75,
);
});
}
Future<void> _toggleVisible(value) async {
final Polyline polyline = _polylines[selectedPolylineId]!;
setState(() {
_polylines[selectedPolylineId!] = polyline.copyWith(
visibleParam: value,
);
});
}
void _changeColor() {
final Polyline polyline = _polylines[selectedPolylineId]!;
setState(() {
_polylines[selectedPolylineId!] = polyline.copyWith(
colorParam: colors[++colorsIndex % colors.length],
);
});
}
void _changePoints() {
final Polyline polyline = _polylines[selectedPolylineId]!;
List<LatLng> currentPoints = polyline.points;
List<LatLng> newPoints = <LatLng>[];
newPoints.addAll(currentPoints);
newPoints.add(LatLng(39.835347, 116.34575));
setState(() {
_polylines[selectedPolylineId!] = polyline.copyWith(
pointsParam: newPoints,
);
});
}
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
final AMapWidget map = AMapWidget(
onMapCreated: _onMapCreated,
polylines: Set<Polyline>.of(_polylines.values),
);
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Container(
height: MediaQuery.of(context).size.height * 0.6,
width: MediaQuery.of(context).size.width,
child: map,
),
Expanded(
child: SingleChildScrollView(
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
Row(
children: <Widget>[
Column(
children: <Widget>[
TextButton(
child: const Text('添加'),
onPressed: _add,
),
TextButton(
child: const Text('删除'),
onPressed:
(selectedPolylineId == null) ? null : _remove,
),
TextButton(
child: const Text('修改线宽'),
onPressed: (selectedPolylineId == null)
? null
: _changeWidth,
),
TextButton(
child: const Text('修改透明度'),
onPressed: (selectedPolylineId == null)
? null
: _changeAlpha,
),
AMapSwitchButton(
label: Text('显示'),
onSwitchChanged: (selectedPolylineId == null)
? null
: _toggleVisible,
defaultValue: true,
),
],
),
Column(
children: <Widget>[
TextButton(
child: const Text('修改颜色'),
onPressed: (selectedPolylineId == null)
? null
: _changeColor,
),
TextButton(
child: const Text('修改线头样式'),
onPressed: (selectedPolylineId == null)
? null
: _changeCapType,
),
TextButton(
child: const Text('修改连接样式'),
onPressed: (selectedPolylineId == null)
? null
: _changeJointType,
),
TextButton(
child: const Text('修改虚线类型'),
onPressed: (selectedPolylineId == null)
? null
: _changeDashLineType,
),
TextButton(
child: const Text('修改坐标'),
onPressed: (selectedPolylineId == null)
? null
: _changePoints,
),
],
),
],
)
],
),
),
),
],
),
);
}
}

View File

@ -0,0 +1,124 @@
import 'package:amap_map_example/base_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:amap_map/amap_map.dart';
import 'package:amap_map/amap_map.dart';
class PolylineGeodesicDemoPage extends BasePage {
PolylineGeodesicDemoPage(String title, String subTitle)
: super(title, subTitle);
@override
Widget build(BuildContext context) {
return _Body();
}
}
class _Body extends StatefulWidget {
const _Body();
@override
State<StatefulWidget> createState() => _State();
}
class _State extends State<_Body> {
_State();
// Values when toggling polyline color
int colorsIndex = 0;
List<Color> colors = <Color>[
Colors.purple,
Colors.red,
Colors.green,
Colors.pink,
];
Map<String, Polyline> _polylines = <String, Polyline>{};
late String selectedPolylineId;
AMapController? _controller;
void _onMapCreated(AMapController controller) {
_controller = controller;
}
List<LatLng> _createPoints() {
final List<LatLng> points = <LatLng>[];
final int polylineCount = _polylines.length;
final int offset = polylineCount * (-1);
points.add(LatLng(39.905151 + offset, 116.401726));
points.add(LatLng(38.905151 + offset, 70.401726));
return points;
}
void _add() {
final Polyline polyline = Polyline(
color: colors[++colorsIndex % colors.length],
width: 10,
geodesic: true,
points: _createPoints());
setState(() {
_polylines[polyline.id] = polyline;
});
//移动到合适的范围
LatLngBounds bound =
LatLngBounds(southwest: LatLng(25.0, 70.0), northeast: LatLng(45, 117));
CameraUpdate update = CameraUpdate.newLatLngBounds(bound, 10);
_controller?.moveCamera(update);
}
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
final AMapWidget map = AMapWidget(
onMapCreated: _onMapCreated,
polylines: Set<Polyline>.of(_polylines.values),
);
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
flex: 10,
child: map,
),
Expanded(
flex: 1,
child: TextButton(
onPressed: _add,
style: ButtonStyle(
shape: MaterialStateProperty.all(RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10))),
//文字颜色
foregroundColor: MaterialStateProperty.all(Colors.white),
//水波纹颜色
overlayColor: MaterialStateProperty.all(Colors.blueAccent),
//背景颜色
backgroundColor: MaterialStateProperty.resolveWith((states) {
//设置按下时的背景颜色
if (states.contains(MaterialState.pressed)) {
return Colors.blueAccent;
}
//默认背景颜色
return Colors.blue;
}),
),
child: Text('添加大地曲线'),
)),
],
),
);
}
}

View File

@ -0,0 +1,112 @@
import 'package:amap_map/amap_map.dart';
import 'package:amap_map_example/base_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:amap_map/amap_map.dart';
class PolylineTextureDemoPage extends BasePage {
PolylineTextureDemoPage(String title, String subTitle)
: super(title, subTitle);
@override
Widget build(BuildContext context) {
return _Body();
}
}
class _Body extends StatefulWidget {
const _Body();
@override
State<StatefulWidget> createState() => _State();
}
class _State extends State<_Body> {
_State();
Map<String, Polyline> _polylines = <String, Polyline>{};
late String selectedPolylineId;
void _onMapCreated(AMapController controller) {}
List<LatLng> _createPoints() {
final List<LatLng> points = <LatLng>[];
final int polylineCount = _polylines.length;
final double offset = polylineCount * -(0.01);
points.add(LatLng(39.938698 + offset, 116.275177));
points.add(LatLng(39.966069 + offset, 116.289253));
points.add(LatLng(39.944226 + offset, 116.306076));
points.add(LatLng(39.966069 + offset, 116.322899));
points.add(LatLng(39.938698 + offset, 116.336975));
return points;
}
void _add() {
final Polyline polyline = Polyline(
width: 20,
customTexture:
BitmapDescriptor.fromIconPath('assets/texture_green.png'),
joinType: JoinType.round,
points: _createPoints());
setState(() {
_polylines[polyline.id] = polyline;
});
}
@override
void initState() {
super.initState();
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
final AMapWidget map = AMapWidget(
onMapCreated: _onMapCreated,
polylines: Set<Polyline>.of(_polylines.values),
);
return Container(
height: MediaQuery.of(context).size.height,
width: MediaQuery.of(context).size.width,
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
flex: 10,
child: map,
),
Expanded(
flex: 1,
child: TextButton(
onPressed: _add,
style: ButtonStyle(
shape: MaterialStateProperty.all(RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10))),
//文字颜色
foregroundColor: MaterialStateProperty.all(Colors.white),
//水波纹颜色
overlayColor: MaterialStateProperty.all(Colors.blueAccent),
//背景颜色
backgroundColor: MaterialStateProperty.resolveWith((states) {
//设置按下时的背景颜色
if (states.contains(MaterialState.pressed)) {
return Colors.blueAccent;
}
//默认背景颜色
return Colors.blue;
}),
),
child: Text('添加纹理线'),
)),
],
),
);
}
}