|
|
import 'dart:io';
|
|
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
import 'package:flutter_bmfbase/BaiduMap/bmfmap_base.dart';
|
|
|
import 'package:flutter_bmfmap/BaiduMap/bmfmap_map.dart';
|
|
|
import 'package:hyzp_ybqx/components/hyxx_data_handle.dart';
|
|
|
|
|
|
///批量添加定位标记
|
|
|
bool enable = true;
|
|
|
bool dragable = true;
|
|
|
int g_zoomLevel_default = 14; //地图默认缩放倍数
|
|
|
int g_zoomLevel = 14; //地图缩放倍数
|
|
|
int g_zoomMin = 10;
|
|
|
int g_zoomMax = 18;
|
|
|
|
|
|
String g_myIconPathPre = "";
|
|
|
// g_myIconPathPre = /data/user/0/com.example.hyzp_yibin_bmfmap/app_flutter/myIcons
|
|
|
String g_myIconDir = "myIcons";
|
|
|
String g_myIconPre = "myIcon";
|
|
|
|
|
|
///批量添加定位标记
|
|
|
List<BMFMarker> g_listBMFMarker = [];
|
|
|
Map g_map_BMFMarkerID_dwIndex = {};
|
|
|
List<BMFText> g_listBMFText = [];
|
|
|
|
|
|
//https://www.cnblogs.com/ybmj/p/14408263.html
|
|
|
//百度地图的脑残设计,附上代码,为后来的码农们...
|
|
|
Future getListBMFMarker({List listDwinfo}) async {
|
|
|
if (null == listDwinfo) {
|
|
|
listDwinfo = listDwinfoGetList2;
|
|
|
}
|
|
|
|
|
|
//double _scale = 9900 / 10000; //自己控制off_latitude、off_longitude效果不好
|
|
|
|
|
|
// BMFMarker marker1 = BMFMarker(
|
|
|
// position: BMFCoordinate(28.807061, 104.607091),
|
|
|
// title: '1、江北振兴大道',
|
|
|
// subtitle: 'test',
|
|
|
// identifier: 'flutter_marker',
|
|
|
// icon: 'assets/images/location.png',
|
|
|
// enabled: enable,
|
|
|
// draggable: dragable);
|
|
|
|
|
|
int len = listDwinfo.length;
|
|
|
for (int i = 0; i < len; i++) {
|
|
|
BMFMarker marker;
|
|
|
if (Platform.isIOS) {
|
|
|
// if (true) {
|
|
|
// 用于安卓环境下测试
|
|
|
marker = BMFMarker(
|
|
|
position: getBMFCoordinate(listDwinfo[i]["dwzb"]),
|
|
|
// title: '${listDwinfo[i]["id"].toString()}、${listDwinfo[i]["dwmc"]}',
|
|
|
// title: '${(i + 1).toString()}、${listDwinfo[i]["dwmc"]}',
|
|
|
// subtitle: 'test',
|
|
|
// identifier: '${listDwinfo[i]["id"].toString()}、${listDwinfo[i]["dwmc"]}',
|
|
|
identifier: '${(i + 1).toString()}、${listDwinfo[i]["dwmc"]}',
|
|
|
|
|
|
// android版使用
|
|
|
// icon: 'assets/images/location.png',
|
|
|
|
|
|
// 为IOS在百度地图中显示文本图标
|
|
|
icon: "$g_myIconPathPre/${g_myIconPre}${i + 1}.png",
|
|
|
// icon: '/data/user/0/com.example.hyzp_yibin_bmfmap/app_flutter/myIcons/myIcon01.png',
|
|
|
|
|
|
/// 默认情况下, annotation view的中心位于annotation的坐标位置,
|
|
|
/// 可以设置centerOffset改变view的位置,正的偏移使view朝右下方移动,负的朝左上方,单位是像素
|
|
|
/// 目前Android只支持Y轴设置偏移量对应SDK的 yOffset(int yOffset) 方法
|
|
|
/// 添加标记 BMFMarker 百度官方有 centerOffset 偏移参数,
|
|
|
/// 文本标签 BMFText 官方没有 centerOffset 偏移参数,我取消了 BMFMarker 的偏移。你看这样行吗?
|
|
|
/// 按公司要求,为更准确定位,取消 BMFMarker 的偏移
|
|
|
centerOffset: BMFPoint(0, 0),
|
|
|
//标记中心偏移
|
|
|
enabled: enable,
|
|
|
draggable: dragable);
|
|
|
} else if (Platform.isAndroid) {
|
|
|
marker = BMFMarker(
|
|
|
position: getBMFCoordinate(listDwinfo[i]["dwzb"]),
|
|
|
// title: '${listDwinfo[i]["id"].toString()}、${listDwinfo[i]["dwmc"]}',
|
|
|
title: '${(i + 1).toString()}、${listDwinfo[i]["dwmc"]}',
|
|
|
subtitle: 'test',
|
|
|
// identifier: '${listDwinfo[i]["id"].toString()}、${listDwinfo[i]["dwmc"]}',
|
|
|
identifier: '${(i + 1).toString()}、${listDwinfo[i]["dwmc"]}',
|
|
|
|
|
|
// android版使用
|
|
|
icon: 'assets/images/location.png',
|
|
|
|
|
|
// 为IOS在百度地图中显示文本图标
|
|
|
// icon: "$g_myIconPathPre/${g_myIconPre}${i + 1}.png",
|
|
|
// icon: '/data/user/0/com.example.hyzp_yibin_bmfmap/app_flutter/myIcons/myIcon01.png',
|
|
|
|
|
|
/// 默认情况下, annotation view的中心位于annotation的坐标位置,
|
|
|
/// 可以设置centerOffset改变view的位置,正的偏移使view朝右下方移动,负的朝左上方,单位是像素
|
|
|
/// 目前Android只支持Y轴设置偏移量对应SDK的 yOffset(int yOffset) 方法
|
|
|
/// 添加标记 BMFMarker 百度官方有 centerOffset 偏移参数,
|
|
|
/// 文本标签 BMFText 官方没有 centerOffset 偏移参数,我取消了 BMFMarker 的偏移。你看这样行吗?
|
|
|
/// 按公司要求,为更准确定位,取消 BMFMarker 的偏移
|
|
|
centerOffset: BMFPoint(0, 0),
|
|
|
//标记中心偏移
|
|
|
enabled: enable,
|
|
|
draggable: dragable);
|
|
|
}
|
|
|
|
|
|
// 百度地图的脑残设计,用Flutter添加多个BMFMarker时,必须在添加BMFMarker时自己保存ID,
|
|
|
// 否则响应点击时无法确定用户点击的是哪个定位标注
|
|
|
// 代码不会自动返回,也没有任何文档说明,是花了一天时间搜索网络无果,是自己翻江倒海摸索出来的
|
|
|
// 下一句是关键代码,将添加的每个BMFMarker的id保存到一个map中,
|
|
|
// 这样在用setMapClickedMarkerCallback添加BMFMarker的通用响应函数中,便可以根据id号来判断用户点击的是哪一个定位标注
|
|
|
g_map_BMFMarkerID_dwIndex[marker.getId()] = i;
|
|
|
g_listBMFMarker.add(marker);
|
|
|
|
|
|
// android版专有
|
|
|
if (Platform.isAndroid) {
|
|
|
// if (false) {
|
|
|
// 用于安卓环境下测试
|
|
|
g_listBMFText.add(BMFText(
|
|
|
// text: '${listDwinfo[i]["id"].toString()}、${listDwinfo[i]["dwmc"]}',
|
|
|
text: '${(i + 1).toString()}、${listDwinfo[i]["dwmc"]}',
|
|
|
//纬度偏移-上下 off_latitude, 经度偏移-左右 off_longitude
|
|
|
//已经在zoomLevel = 15时调整好定位标记与文本标记的相对位置
|
|
|
// ,当地图缩放时,会发生位置变化,必须使用 Provider 或者 EventBus 进行跟踪更新
|
|
|
|
|
|
//https://time.geekbang.org/column/article/131890
|
|
|
//老师,provider、eventBus的用途有啥区别吗,都可以做状态的通知
|
|
|
// 作者回复: Provider 主要是用来做数据读写共享;event_bus主要是用来做数据状态通知、实现组件间单向数据传递。
|
|
|
//如果我们的应用足够简单,数据流动的方向和顺序是清晰的,我们只需要将数据映射成视图就可以了。
|
|
|
// 作为声明式的框架,Flutter 可以自动处理数据到渲染的全过程,通常并不需要 Provider 状态管理。
|
|
|
// position: getBMFCoordinate(listDwinfo[i]["dwzb"],
|
|
|
// off_latitude: -0.0002 * g_zoomLevel * _scale,
|
|
|
// off_longitude: -0.00009 * g_zoomLevel * _scale),
|
|
|
position: getBMFCoordinate(listDwinfo[i]["dwzb"]),
|
|
|
//自己控制off_latitude、off_longitude效果不好
|
|
|
bgColor: Colors.yellow,
|
|
|
fontColor: Colors.black,
|
|
|
fontSize: 35,
|
|
|
// typeFace:
|
|
|
// BMFTypeFace(familyName: BMFFamilyName.sMonospace, textStype: BMFTextStyle.BOLD_ITALIC),
|
|
|
typeFace: BMFTypeFace(familyName: BMFFamilyName.sMonospace, textStype: BMFTextStyle.BOLD),
|
|
|
alignY: BMFVerticalAlign.ALIGN_TOP,
|
|
|
alignX: BMFHorizontalAlign.ALIGN_LEFT,
|
|
|
rotate: 0.0,
|
|
|
zIndex: 99));
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
//文字覆盖物
|
|
|
// 文字(Text)在地图上也是一种覆盖物,由BMFText类定义,示例代码如下:
|
|
|
//
|
|
|
// /// text经纬度信息
|
|
|
// BMFCoordinate position = new BMFCoordinate(39.73235, 116.350338);
|
|
|
//
|
|
|
// /// 构造text
|
|
|
// BMFText bmfText = BMFText(
|
|
|
// text: 'hello world',
|
|
|
// position: position,
|
|
|
// bgColor: Colors.blue,
|
|
|
// fontColor: Colors.red,
|
|
|
// fontSize: 40,
|
|
|
// typeFace: BMFTypeFace( familyName: BMFFamilyName.sMonospace,
|
|
|
// textStype: BMFTextStyle.BOLD_ITALIC),
|
|
|
// alignY: BMFVerticalAlign.ALIGN_TOP,
|
|
|
// alignX: BMFHorizontalAlign.ALIGN_LEFT,
|
|
|
// rotate: 30.0);
|
|
|
//
|
|
|
// /// 添加text
|
|
|
// myMapController.addText(bmfText);
|
|
|
|
|
|
//从"dwzb": "104.607091|28.807061",得到BMFCoordinate(28.807061, 104.607091)
|
|
|
//纬度偏移-上下 off_latitude, 经度偏移-左右 off_longitude
|
|
|
//自己控制off_latitude、off_longitude效果不好
|
|
|
BMFCoordinate getBMFCoordinate(String dwzb, {double off_latitude = 0, double off_longitude = 0}) {
|
|
|
off_latitude = 0; //取消偏移
|
|
|
off_longitude = 0; //取消偏移
|
|
|
List _listCoordinateItem = dwzb.trim().split('|');
|
|
|
return BMFCoordinate(double.parse(_listCoordinateItem[1]) - off_latitude,
|
|
|
double.parse(_listCoordinateItem[0]) - off_longitude);
|
|
|
}
|