2023-12-22 21:23:24 +08:00
|
|
|
|
import 'package:amap_map/amap_map.dart';
|
|
|
|
|
import 'package:amap_map_example/widgets/amap_switch_button.dart';
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter/services.dart';
|
|
|
|
|
|
2023-12-30 07:54:59 +08:00
|
|
|
|
class CustomMapStylePage extends StatefulWidget {
|
2024-08-26 09:55:49 +08:00
|
|
|
|
CustomMapStylePage({super.key});
|
2023-12-22 21:23:24 +08:00
|
|
|
|
|
|
|
|
|
@override
|
2024-08-26 09:55:49 +08:00
|
|
|
|
State<CustomMapStylePage> createState() => _CustomMapStyleState();
|
2023-12-22 21:23:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-30 07:54:59 +08:00
|
|
|
|
class _CustomMapStyleState extends State<CustomMapStylePage> {
|
2023-12-22 21:23:24 +08:00
|
|
|
|
bool _mapCreated = false;
|
|
|
|
|
|
2024-08-26 09:55:49 +08:00
|
|
|
|
final CustomStyleOptions _customStyleOptions = CustomStyleOptions(false);
|
2023-12-22 21:23:24 +08:00
|
|
|
|
//加载自定义地图样式
|
|
|
|
|
void _loadCustomData() async {
|
|
|
|
|
ByteData styleByteData = await rootBundle.load('assets/style.data');
|
|
|
|
|
_customStyleOptions.styleData = styleByteData.buffer.asUint8List();
|
|
|
|
|
ByteData styleExtraByteData =
|
|
|
|
|
await rootBundle.load('assets/style_extra.data');
|
|
|
|
|
_customStyleOptions.styleExtraData =
|
|
|
|
|
styleExtraByteData.buffer.asUint8List();
|
|
|
|
|
//如果需要加载完成后直接展示自定义地图,可以通过setState修改CustomStyleOptions的enable为true
|
2023-12-29 21:48:01 +08:00
|
|
|
|
setState(() {
|
|
|
|
|
_customStyleOptions.enabled = true;
|
|
|
|
|
});
|
2023-12-22 21:23:24 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
_loadCustomData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
final AMapWidget map = AMapWidget(
|
|
|
|
|
onMapCreated: onMapCreated,
|
|
|
|
|
customStyleOptions: _customStyleOptions,
|
|
|
|
|
);
|
|
|
|
|
return ConstrainedBox(
|
|
|
|
|
constraints: BoxConstraints.expand(),
|
|
|
|
|
child: Stack(
|
|
|
|
|
alignment: Alignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
Container(
|
|
|
|
|
height: MediaQuery.of(context).size.height,
|
|
|
|
|
width: MediaQuery.of(context).size.width,
|
|
|
|
|
child: map,
|
|
|
|
|
),
|
|
|
|
|
Positioned(
|
|
|
|
|
top: 30,
|
|
|
|
|
child: Container(
|
|
|
|
|
color: Color(0xFF00BFFF),
|
|
|
|
|
child: AMapSwitchButton(
|
|
|
|
|
label: Text(
|
|
|
|
|
'自定义地图',
|
|
|
|
|
style: TextStyle(color: Colors.white),
|
|
|
|
|
),
|
|
|
|
|
defaultValue: _customStyleOptions.enabled,
|
|
|
|
|
onSwitchChanged: (value) => {
|
|
|
|
|
if (_mapCreated)
|
|
|
|
|
{
|
|
|
|
|
setState(() {
|
|
|
|
|
_customStyleOptions.enabled = value;
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void onMapCreated(AMapController controller) {
|
|
|
|
|
_mapCreated = true;
|
|
|
|
|
}
|
|
|
|
|
}
|