记录一下集成百度地图的步骤
1、申请百度地图ak(过程简单,不赘述)
2、AndroidMenifest.xml中的application里引入ak信息:
android:name=".MyApplication"
android:icon="@mipmap/ic_launcher">
android:name="com.baidu.lbsapi.API_KEY"
android:value="你申请的ak" />
3、在pubspec.yaml中引入依赖(记得get package一下)
dependencies:
flutter:
sdk: flutter
flutter_bmflocation: ^1.0.2
flutter_bmfmap: ^1.0.2
4、替换Application为百度的
import com.baidu.flutter_bmfbase.BmfMapApplication;
public class MyApplication extends BmfMapApplication {
}
android:name=".MyApplication"
android:icon="@mipmap/ic_launcher">
5、添加资源文件
6、初始化定位
import 'dart:async';
import 'package:flutter_bmflocation/bdmap_location_flutter_plugin.dart';
import 'package:flutter_bmflocation/flutter_baidu_location_android_option.dart';
import 'package:flutter_bmfmap/BaiduMap/map/bmf_map_controller.dart';
import 'package:flutter_bmfmap/BaiduMap/models/overlays/bmf_marker.dart';
import 'package:flutter_bmfbase/BaiduMap/bmfmap_base.dart' show BMFCoordinate;
class LocationUtils {
static LocationFlutterPlugin _locationPlugin = LocationFlutterPlugin();
static StreamSubscription
// 启动定位
static void handleStartLocation({Function callback}) {
if (null != _locationPlugin) {
_setupListener(callback);
_setLocOption();
_locationPlugin.startLocation();
}
}
static _setLocOption() async {
BaiduLocationAndroidOption androidOption = new BaiduLocationAndroidOption();
// androidOption.setCoorType("bd09ll");
/// 可选,设置返回经纬度坐标类型,默认gcj02
/// gcj02:国测局坐标;
/// bd09ll:百度经纬度坐标;
/// bd09:百度墨卡托坐标;
/// 海外地区定位,无需设置坐标类型,统一返回wgs84类型坐标
androidOption.setIsNeedAltitude(true);
/// 可选,设置是否需要返回海拔高度信息,true为需要返回
androidOption.setIsNeedAddres(true);
/// 可选,设置是否需要返回地址信息,true为需要返回
androidOption.setIsNeedLocationPoiList(true);
/// 可选,设置是否需要返回周边poi信息,true为需要返回
androidOption.setIsNeedNewVersionRgc(true);
/// 可选,设置是否需要返回新版本rgc信息,true为需要返回
androidOption.setIsNeedLocationDescribe(true);
/// 可选,设置是否需要返回位置描述信息,true为需要返回
androidOption.setOpenGps(true);
/// 可选,设置是否需要使用gps,true为需要使用
androidOption.setLocationMode(LocationMode.Hight_Accuracy);
/// 可选,设置定位模式,可选的模式有高精度、低功耗、仅设备,默认为高精度模式,可选值如下:
/// 高精度模式: LocationMode.Hight_Accuracy
/// 低功耗模式:LocationMode.Battery_Saving
/// 仅设备(Gps)模式:LocationMode.Device_Sensors
androidOption.setScanspan(1000);
/// 可选,设置发起定位请求的间隔,int类型,单位ms
/// 如果设置为0,则代表单次定位,即仅定位一次,默认为0
/// 如果设置非0,需设置1000ms以上才有效
androidOption.setLocationPurpose(BDLocationPurpose.SignIn);
/// 可选,设置场景定位参数,包括签到场景、运动场景、出行场景,可选值如下:
/// 签到场景: BDLocationPurpose.SignIn
/// 运动场景: BDLocationPurpose.Transport
/// 出行场景: BDLocationPurpose.Sport
Map androidMap = androidOption.getMap();
_locationPlugin.prepareLoc(androidMap, null);
}
// 返回定位信息
static void _setupListener(callback) {
if (_locationListener != null) {
return;
}
_locationListener =
_locationPlugin.onResultCallback().listen((Map
result) { callback(result);
});
}
static void cancel() {
// 取消监听
if (null != _locationListener) {
_locationListener.cancel();
}
}
/// 创建完成回调
static onBMFMapCreated(
BMFMapController controller, double latitude, double longitude) {
BMFMapController myMapController = controller;
List
_markers = []; /// 批量添加大头针
_markers.add(BMFMarker(
position: BMFCoordinate(latitude + 0.0002, longitude + 0.0002),
title: '第二个',
subtitle: 'test',
identifier: 'flutter_marker',
icon: 'assets/icon_binding_point.png',
enabled: false,
draggable: false));
myMapController?.addMarkers(_markers);
/// 地图加载回调
myMapController?.setMapDidLoadCallback(callback: () {
print('mapDidLoad-地图加载完成');
});
}
}
7、在widget中使用
import 'dart:async';
import 'dart:io';
import 'package:chargebd/locationUtils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bmflocation/bdmap_location_flutter_plugin.dart';
import 'package:flutter_bmflocation/flutter_baidu_location.dart';
import 'package:flutter_bmflocation/flutter_baidu_location_android_option.dart';
import 'package:flutter_bmflocation/flutter_baidu_location_ios_option.dart';
import 'package:flutter_bmfbase/BaiduMap/bmfmap_base.dart'
show BMFCoordinate, BMFEdgeInsets, BMFMapSDK, BMF_COORD_TYPE;
import 'package:flutter_bmfmap/BaiduMap/map/bmf_map_controller.dart';
import 'package:flutter_bmfmap/BaiduMap/map/bmf_map_view.dart';
import 'package:flutter_bmfmap/BaiduMap/models/bmf_map_options.dart';
import 'package:flutter_bmfmap/BaiduMap/models/overlays/bmf_marker.dart';
void main() {
runApp(MyApp());
if (Platform.isIOS) {
// BMFMapSDK.setApiKeyAndCoordType(
// '请输入百度开放平台申请的iOS端API KEY', BMF_COORD_TYPE.BD09LL);
} else if (Platform.isAndroid) {
// Android 目前不支持接口设置Apikey,
// 请在主工程的Manifest文件里设置,详细配置方法请参考[https://lbsyun.baidu.com/ 官网][https://lbsyun.baidu.com/)demo
BMFMapSDK.setCoordType(BMF_COORD_TYPE.BD09LL);
}
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: MyHomePage(title: 'Flutter Demo Home Page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State
{ BMFMapOptions mapOptions;
BaiduLocation _baiduLocation;
@override
void initState() {
super.initState();
LocationUtils.handleStartLocation(callback: locationCallback);
}
@override
Widget build(BuildContext context) {
var screenSize = MediaQuery.of(context).size;
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children:
[ Container(
height: screenSize.height,
width: screenSize.width,
child: mapOptions == null
? null
: Stack(
children:
[ BMFMapWidget(
onBMFMapCreated: (controller) {
LocationUtils.onBMFMapCreated(
controller,
_baiduLocation?.latitude,
_baiduLocation?.longitude);
},
mapOptions: mapOptions,
),
],
),
)
],
),
),
);
}
locationCallback(Map
result) { setState(() {
try {
_baiduLocation = BaiduLocation.fromMap(result);
print('lat==${_baiduLocation.latitude}');
print('lon==${_baiduLocation.longitude}');
mapOptions = BMFMapOptions(
center: BMFCoordinate(
_baiduLocation?.latitude ?? 0, _baiduLocation?.longitude ?? 0),
zoomLevel: 18,
mapPadding: BMFEdgeInsets(left: 30, top: 0, right: 30, bottom: 0));
} catch (e) {
print(e);
}
});
}
@override
void dispose() {
super.dispose();
LocationUtils.cancel();
}
}
8、实现效果