### Display Amap Map Widget Source: https://github.com/kuloud/amap_map/blob/main/README.md Renders the AmapMap widget, displaying a map with a specified initial camera position. It also includes a callback for when the map is created, providing access to the AMapController. ```dart import 'package:amap_map_example/base_page.dart'; import 'package:flutter/material.dart'; import 'package:amap_map/amap_map.dart'; import 'package:x_amap_base/x_amap_base.dart'; class ShowMapPage extends BasePage { ShowMapPage(String title, String subTitle) : super(title, subTitle); @override Widget build(BuildContext context) { return _ShowMapPageBody(); } } class _ShowMapPageBody extends StatefulWidget { @override State createState() => _ShowMapPageState(); } class _ShowMapPageState extends State<_ShowMapPageBody> { static final CameraPosition _kInitialPosition = const CameraPosition( target: LatLng(39.909187, 116.397451), zoom: 10.0, ); @override Widget build(BuildContext context) { final AMapWidget map = AMapWidget( initialCameraPosition: _kInitialPosition, onMapCreated: onMapCreated, ); return ConstrainedBox( constraints: BoxConstraints.expand(), child: Container( height: MediaQuery.of(context).size.height, width: MediaQuery.of(context).size.width, child: map, ), ); } AMapController _mapController; void onMapCreated(AMapController controller) { setState(() { _mapController = controller; }); } } ``` -------------------------------- ### Initialize AmapMap Plugin Source: https://github.com/kuloud/amap_map/blob/main/README.md Initializes the AmapMap plugin within your Flutter application. This step is crucial and should be performed in the first widget launched by `runApp`. It requires the build context and your Amap API keys. ```dart import 'package:amap_map/amap_map.dart'; import 'package:x_amap_base/x_amap_base.dart'; // AMapApiKey 和 AMapPrivacyStatement 定义在 package `x_amap_base` 中,需要一并引入 class DemoWidget extends State { @override Widget build(BuildContext context) { AMapInitializer.init(context, apiKey: ConstConfig.amapApiKeys); return Scaffold( // ... ); } } ``` -------------------------------- ### Add amap_map Dependency Source: https://github.com/kuloud/amap_map/blob/main/README.md This command adds the amap_map package to your Flutter project's dependencies, allowing you to use its features for integrating Amap maps. ```bash flutter pub add amap_map ``` -------------------------------- ### AMapController for Map Interactions Source: https://github.com/kuloud/amap_map/blob/main/README.md The AMapController provides methods to interact with the Amap Map. It allows changing the camera view with animations, taking map snapshots, clearing cache, and converting between geographical coordinates (LatLng) and screen coordinates. ```dart class AMapController { ///改变地图视角 /// ///通过[CameraUpdate]对象设置新的中心点、缩放比例、放大缩小、显示区域等内容 /// ///(注意:iOS端设置显示区域时,不支持duration参数,动画时长使用iOS地图默认值350毫秒) /// ///可选属性[animated]用于控制是否执行动画移动 /// ///可选属性[duration]用于控制执行动画的时长,默认250毫秒,单位:毫秒 Future moveCamera(CameraUpdate cameraUpdate, {bool animated = true, int duration = 250}); ///地图截屏 Future takeSnapshot(); /// 清空缓存 Future clearDisk(); /// 经纬度转屏幕坐标 Future toScreenCoordinate(LatLng latLng); /// 屏幕坐标转经纬度 Future fromScreenCoordinate(ScreenCoordinate screenCoordinate); } ``` -------------------------------- ### AMapWidget for Displaying Amap Maps Source: https://github.com/kuloud/amap_map/blob/main/README.md The AMapWidget is a StatefulWidget used to display Amap Maps. It supports various configurations including initial camera position, map type, custom styles, location settings, zoom levels, map bounds, traffic display, POI interaction, building and label visibility, compass and scale display, gesture controls, logo positioning, markers, polylines, polygons, and callbacks for map events and location changes. ```dart ///用于展示高德地图的Widget class AMapWidget extends StatefulWidget { /// 初始化时的地图中心点 final CameraPosition initialCameraPosition; ///地图类型 final MapType mapType; ///自定义地图样式 final CustomStyleOptions? customStyleOptions; ///定位小蓝点 final MyLocationStyleOptions? myLocationStyleOptions; ///缩放级别范围 final MinMaxZoomPreference? minMaxZoomPreference; ///地图显示范围 final LatLngBounds? limitBounds; ///显示路况开关 final bool trafficEnabled; /// 地图poi是否允许点击 final bool touchPoiEnabled; ///是否显示3D建筑物 final bool buildingsEnabled; ///是否显示底图文字标注 final bool labelsEnabled; ///是否显示指南针 final bool compassEnabled; ///是否显示比例尺 final bool scaleEnabled; ///是否支持缩放手势 final bool zoomGesturesEnabled; ///是否支持滑动手势 final bool scrollGesturesEnabled; ///是否支持旋转手势 final bool rotateGesturesEnabled; ///是否支持倾斜手势 final bool tiltGesturesEnabled; /// logo 位置,此字段高德只支持Android,本插件iOS借用logoCenter做了实现 final LogoPosition? logoPosition; /// logo 底部间距(px),此字段高德只支持Android,本插件iOS借用logoCenter做了实现 final int? logoBottomMargin; /// logo 靠左间距(px),此字段高德只支持Android,本插件iOS借用logoCenter做了实现 final int? logoLeftMargin; /// 地图上显示的Marker final Set markers; /// 地图上显示的polyline final Set polylines; /// 地图上显示的polygon final Set polygons; /// 地图创建成功的回调, 收到此回调之后才可以操作地图 final MapCreatedCallback? onMapCreated; /// 相机视角持续移动的回调 final ArgumentCallback? onCameraMove; /// 相机视角移动结束的回调 final ArgumentCallback? onCameraMoveEnd; /// 地图单击事件的回调 final ArgumentCallback? onTap; /// 地图长按事件的回调 final ArgumentCallback? onLongPress; /// 地图POI的点击回调,需要`touchPoiEnabled`true,才能回调 final ArgumentCallback? onPoiTouched; ///位置回调 final ArgumentCallback? onLocationChanged; ///需要应用到地图上的手势集合 final Set> gestureRecognizers; /// 设置地图语言 final MapLanguage? mapLanguage; /// Marker InfoWindow 适配器 final InfoWindowAdapter? infoWindowAdapter; } ``` -------------------------------- ### Fix for Target SDK Version >= 30 Crash Source: https://github.com/kuloud/amap_map/blob/main/README.md This snippet addresses a crash issue in Android applications when the target SDK version is 30 or higher. The solution involves adding `android:allowNativeHeapPointerTagging="false"` to the application tag in the AndroidManifest.xml file. ```xml ... ``` -------------------------------- ### Update AmapMap Privacy Agreement Source: https://github.com/kuloud/amap_map/blob/main/README.md Updates the privacy agreement status for the Amap SDK. This is a required step for compliance with Gaode SDK's regulations, involving user authorization interaction before notifying the component. ```dart AMapInitializer.updatePrivacyAgree(ConstConfig.amapPrivacyStatement); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.