在Flutter中接入高德地图,可以使用amap_location插件进行定位和amap_flutter_map插件进行地图展示。以下是接入的步骤和示例代码:
- 在pubspec.yaml中添加依赖:
dependencies:
  amap_location: ^0.2.7
  amap_flutter_map: ^0.2.3
- 获取高德开放平台的API Key,并在代码中配置:
import 'package:amap_location/amap_location.dart';
import 'package:amap_flutter_map/amap_flutter_map.dart';
 
// 配置高德API Key
var apiKey = "你的高德API Key";
 
// 定位
AMapLocationClient locationClient = new AMapLocationClient(apiKey: apiKey);
 
// 地图
MapPage({Key key}) : super(key: key);
 
@override
Widget build(BuildContext context) {
  return new AMapFlutterMap(
    apiKey: apiKey,
    centerCoordinate: new LatLng(39.90923, 116.397428),
    zoomLevel: 13.5,
  );
}
- 在Android和iOS的原生项目中配置高德开放平台获取的API Key。
Android配置:
在android/app/src/main/AndroidManifest.xml中添加权限和API Key:
<manifest>
    <!-- 添加定位权限 -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <application>
        <!-- 高德API Key -->
        <meta-data
            android:name="com.amap.api.v2.apikey"
            android:value="你的高德API Key"/>
    </application>
</manifest>
iOS配置:
在Xcode中的Runner/Info.plist文件中添加API Key:
<dict>
    <!-- 高德API Key -->
    <key>AMapServices</key>
    <dict>
        <key>APIKey</key>
        <string>你的高德API Key</string>
    </dict>
</dict>
- 使用定位和地图功能。
定位示例:
locationClient.getLocation(true).then((location) {
  print("定位结果:${location.address}");
}).catchError((error) {
  print("定位出错:$error");
});
地图示例:
MapPage()
确保在实际设备上运行或模拟器,因为高德地图SDK需要设备的硬件和软件环境。