pubspec.yaml 中添加依赖
amap_base: ^0.3.5
安卓 苹果添加地图配置依赖:
安卓:
build时报错
The Android Gradle plugin supports only Kotlin Gradle plugin version 1.3.0 and higher.
The following dependencies do not satisfy the required version:
project ':amp_base' -> org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.71
需要对gradle做降级处理,具体参考 https://www.jianshu.com/p/12ab33e8b8bb
降级后会继续报错:
跟踪错误信息中的错误文件位置将 '?' 删掉。
之后会继续报错,错误信息是
Execution failed for task ':app:transformDexArchiveWithExternalLibsDexMergerForDebug'.
解决办法一:
在build.gradle(app) 中尝试在android{...}的defaultConfig{...}中加入以下代码:
multiDexEnabled true
解决办法二:
在build.gradle(app) 中尝试在android{...}的dependencies{...}中加入:
'implementation 'com.android.support:support-v4:28.0.0'
设置key:
<application>
...
<meta-data
android:name="com.amap.api.v2.apikey"
android:value="您的Key"/>
</application>
苹果:
<key>io.flutter.embedded_views_preview</key>
<string>YES</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>App需要您的同意,才能始终访问位置</string>
<key>NSLocationUsageDescription</key>
<string>App需要您的同意,才能访问位置</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>App需要您的同意,才能在使用期间访问位置</string>
以上是安卓苹果的权限配置⬆️
以下Flutter 内GDMap通用代码⬇️
void main() async {
await AMap.init('f1a72858d51ce608a92a29f0f001f7d1');
runApp(MyApp());
}
//初始化定位监听
void _initLocation() async {
_amapLocation.init();
final options = LocationClientOptions(
isOnceLocation: true, //获取位置标题
locatingWithReGeocode: true, //获取坐标点
);
if (await Permissions().requestPermission()) {
_amapLocation.startLocate(options).listen((_) => setState(() {
_result =
'坐标:${_.longitude},${_.latitude},地点:${_.aoiName} @ ${DateTime.now().hour}:${DateTime.now().minute}:${DateTime.now().second}';
}));
} else {
setState(() {
_result = "无定位权限";
});
} }
AMapView(
onAMapViewCreated: (controller) {
setState(() => _controller = controller);
//_controller = controller;
controller.setZoomLevel(17);
controller.setMyLocationStyle(MyLocationStyle(
showsHeadingIndicator: false,
myLocationType: LOCATION_TYPE_FOLLOW,
showMyLocation: true,
));
},
amapOptions: AMapOptions(),
),
以上是定位与地图展示,下面我们介绍路线规划,首先需要做的事定义一个用来展示路线的类:
/// 等待页
Future<T> loading<T>(BuildContext context, Future<T> futureTask) {
// 是被future pop的还是按返回键pop的
bool popByFuture = true;
showDialog(
context: context,
builder: (context) {
return WillPopScope(
onWillPop: () async => false,
child: Center(
child: CupertinoActivityIndicator(),
),
);
},
barrierDismissible: false,
).whenComplete(() {
// 1. 如果是返回键pop的, 那么设置成true, 这样future完成时就不会pop了
// 2. 如果是future完成导致的pop, 那么这一行是没用任何作用的
popByFuture = false;
});
return futureTask.whenComplete(() {
// 由于showDialog会强制使用rootNavigator, 所以这里pop的时候也要用rootNavigator
if (popByFuture) {
Navigator.of(context, rootNavigator: true).pop(context);
}
});
}
String jsonFormat(Map<String, Object> json) {
JsonEncoder encoder = JsonEncoder.withIndent(' ');
return encoder.convert(json);
}
然后在调用处:
loading(
context,
AMapSearch().calculateDriveRoute(
RoutePlanParam(
from: LatLng(38.858448, 121.519633),
to: LatLng(38.882006, 121.587922),
),
),
).then((result) {
final allPoint = result.paths[0].steps
.expand((step) => step.polyline)
.toList();
result.paths[0].steps
.expand((step) => step.TMCs)
.forEach((tmc) {
_controller.addPolyline((PolylineOptions(
latLngList: tmc.polyline,
width: 15,
lineJoinType: PolylineOptions.LINE_JOIN_MITER,
lineCapType: PolylineOptions.LINE_CAP_TYPE_ROUND,
color: _getTmcColor(tmc.status),
)));
});
_controller.zoomToSpan(allPoint);
_controller.addMarkers(
markerList
.map((latLng) => MarkerOptions(
// icon:,
position: latLng,
))
.toList(),
);
}).catchError((e) =>
print('地图错误$e') //showError(context, e.toString())
);
位置搜索:
Button(
label: '开始搜索',
onPressed: (context) {
loading(
context,
AMapSearch().searchPoi(
PoiSearchQuery(
query: _queryController.text,
city: _cityController.text,
),
),
).then((poiResult) {
setState(() {
_maps = poiResult.toJson();
print(_maps["pois"][0]["title"]);
});
}).catchError(
(e) => print('地图错误$e') //showError(context, e.toString())
);
},
),
好啦 ,以上就是 amap_base插件对高德地图的 定位 展示 搜索 路线规划的常规操作,有疑问欢迎留下评论,下期 更新 三种 即时通讯!!!