npm install vue-baidu-map —save
    image.png

    1. //全局注册 main.js
    2. import BaiduMapfrom 'vue-baidu-map'
    3. Vue.use(BaiduMap, {
    4. ak:'你申请的key'
    5. })

    image.png
    image.png

    1. // bmpgl.js
    2. export function BMPGL() {
    3. return new Promise(function(resolve, reject) {
    4. window.init = function() {
    5. // eslint-disable-next-line
    6. resolve(BMapGL)
    7. }
    8. const script = document.createElement('script')
    9. script.type = 'text/javascript'
    10. script.src = `http://api.map.baidu.com/api?v=1.0&type=webgl&ak=${'0hly7GIXoAwFRNsdjI7GntYnsnGX7sBL'}&callback=init`
    11. script.onerror = reject
    12. document.head.appendChild(script)
    13. })
    14. }

    image.png
    使用示例直接把我method都放上来吧,之前写的,不太想找啦

    1. methods: {
    2. showP() {
    3. this.$router.push('/newpopup')
    4. this.badges=false
    5. console.log(this.badges);
    6. },
    7. onSelect(action) {
    8. Toast(action.text);
    9. },
    10. init() {
    11. // 传入密钥获取地图回调。
    12. BMPGL().then((BMapGL) => {
    13. // 创建地图实例
    14. let map = new BMapGL.Map('allmap')
    15. // 创建点坐标 axios => res 获取的初始化定位坐标
    16. let point = new BMapGL.Point(118.294346,32.289725)
    17. let point1 = new BMapGL.Point(118.33035,32.28887)
    18. var marker = new BMapGL.Marker(point1) // 创建标注
    19. map.addOverlay(marker) // 将标注添加到地图中
    20. var circle = new BMapGL.Circle(point1, 6, { strokeColor: 'Red', strokeWeight: 6, strokeOpacity: 1, Color: 'Red', fillColor: '#f03' })
    21. map.addOverlay(circle)
    22. // 初始化地图,设置中心点坐标和地图级别
    23. map.centerAndZoom(point, 14)
    24. // 开启鼠标滚轮缩放
    25. map.enableScrollWheelZoom(true)
    26. var locationControl = new BMapGL.LocationControl({
    27. // 控件的停靠位置(可选,默认左上角)
    28. anchor: BMAP_ANCHOR_BOTTOM_LEFT,
    29. // 控件基于停靠位置的偏移量(可选)
    30. offset: new BMapGL.Size(10, 10)
    31. })
    32. // 将控件添加到地图上
    33. map.addControl(locationControl)
    34. var scaleCtrl = new BMapGL.ScaleControl({
    35. // 控件的停靠位置(可选,默认左上角)
    36. anchor: BMAP_ANCHOR_BOTTOM_LEFT,
    37. // 控件基于停靠位置的偏移量(可选)
    38. offset: new BMapGL.Size(70, 0)
    39. })
    40. map.addControl(scaleCtrl)
    41. // 添加定位事件
    42. locationControl.addEventListener("locationSuccess", function(e) {
    43. var address = ''
    44. address += e.addressComponent.province
    45. address += e.addressComponent.city
    46. address += e.addressComponent.district
    47. address += e.addressComponent.street
    48. address += e.addressComponent.streetNumber
    49. alert("当前定位地址为:" + address)
    50. })
    51. locationControl.addEventListener("locationError",function(e) {
    52. alert(e.message)
    53. })
    54. var points = [];
    55. for (var i = 0; i < this.path.length; i++) {
    56. points.push(new BMapGL.Point(this.path[i].lng, this.path[i].lat));
    57. }
    58. function ZoomControl() {
    59. this.defaultAnchor = BMAP_ANCHOR_TOP_LEFT;
    60. this.defaultOffset = new BMapGL.Size(20, 20)
    61. }
    62. function ClearControl() {
    63. this.defaultAnchor = BMAP_ANCHOR_TOP_RIGHT;
    64. this.defaultOffset = new BMapGL.Size(0, 20)
    65. }
    66. function CircleControl() {
    67. this.defaultAnchor = BMAP_ANCHOR_TOP_RIGHT;
    68. this.defaultOffset = new BMapGL.Size(60, 0)
    69. }
    70. function locatedControl() {
    71. this.defaultAnchor = BMAP_ANCHOR_TOP_RIGHT;
    72. this.defaultOffset = new BMapGL.Size(20, 130)
    73. }
    74. //通过JavaScript的prototype属性继承于BMap.Control
    75. locatedControl.prototype = new BMapGL.Control();
    76. ZoomControl.prototype = new BMapGL.Control();
    77. ClearControl.prototype = new BMapGL.Control();
    78. CircleControl.prototype = new BMapGL.Control();
    79. //自定义控件必须实现自己的initialize方法,并且将控件的DOM元素返回
    80. //在本方法中创建个div元素作为控件的容器,并将其添加到地图容器中
    81. locatedControl.prototype.initialize = function(map) {
    82. var div = document.createElement('div');
    83. //添加文字说明
    84. div.appendChild(document.createTextNode('宝贝位置'));
    85. // 设置样式
    86. div.style.textAlign = "center";
    87. div.style.fontSize = "15px";
    88. div.style.width = "60px";
    89. div.style.height = "25px";
    90. div.style.cursor = "pointer";
    91. div.style.padding = "7px 10px";
    92. div.style.boxShadow = "0 2px 6px 0 rgba(27, 142, 236, 0.5)";
    93. div.style.borderRadius = "5px";
    94. div.style.backgroundColor = "white";
    95. // 绑定事件,点击一次放大两级
    96. div.onclick = function(e){
    97. let point1 = new BMapGL.Point(118.33035,32.28887)
    98. map.centerAndZoom(point1);
    99. var marker = new BMapGL.Marker(point1) // 创建标注
    100. map.addOverlay(marker) // 将标注添加到地图中
    101. var circle = new BMapGL.Circle(point1, 6, { strokeColor: 'Red', strokeWeight: 6, strokeOpacity: 1, Color: 'Red', fillColor: '#f03' })
    102. map.addOverlay(circle)
    103. }
    104. // 添加DOM元素到地图中
    105. map.getContainer().appendChild(div);
    106. // 将DOM元素返回
    107. return div;
    108. }
    109. ClearControl.prototype.initialize = function(map) {
    110. //创建一个dom元素
    111. var div = document.createElement('div');
    112. //添加文字说明
    113. div.appendChild(document.createTextNode('清空'));
    114. // 设置样式
    115. div.style.textAlign = 'center'
    116. div.style.fontSize = "15px";
    117. div.style.width = "40px";
    118. div.style.height = "25px";
    119. div.style.cursor = "pointer";
    120. div.style.padding = "7px 10px";
    121. div.style.boxShadow = "0 2px 6px 0 rgba(27, 142, 236, 0.5)";
    122. div.style.borderRadius = "5px";
    123. div.style.backgroundColor = "white";
    124. // 绑定事件,点击一次放大两级
    125. div.onclick = function(e){
    126. map.clearOverlays();
    127. }
    128. // 添加DOM元素到地图中
    129. map.getContainer().appendChild(div);
    130. // 将DOM元素返回
    131. return div;
    132. }
    133. CircleControl.prototype.initialize = function(map) {
    134. //创建一个dom元素
    135. var div = document.getElementById("btn");
    136. var div2 = document.getElementById("xlk")
    137. //添加文字说明
    138. // div2.appendChild(document.createTextNode('围栏范围'));
    139. // //设置样式
    140. // div2.style.fontSize = "15px";
    141. // div2.style.width = "60px";
    142. // div2.style.height = "25px";
    143. // div2.style.cursor = "pointer";
    144. // div2.style.padding = "7px 10px";
    145. // div2.style.boxShadow = "0 2px 6px 0 rgba(27, 142, 236, 0.5)";
    146. // div2.style.borderRadius = "5px";
    147. // div2.style.backgroundColor = "white";
    148. var fw = 3000
    149. div2.onclick = function(e) {
    150. console.log(fw);
    151. if (fw==1000) {
    152. fw = 3000
    153. }
    154. else if(fw==3000) {
    155. fw=5000
    156. console.log(fw);
    157. }else if(fw==5000){
    158. fw=1000
    159. }
    160. }
    161. // 绑定事件,点击一次放大两级
    162. div.onclick = function(e){
    163. var circle = new BMapGL.Circle(new BMapGL.Point(118.303976,32.294609),fw,{fillColor:"blue", strokeWeight: 1 ,fillOpacity: 0.3, strokeOpacity: 0.3});
    164. map.addOverlay(circle);
    165. function addMarker(points) {
    166. var marker = new BMapGL.Marker(points)
    167. // //循环建立标注点
    168. // for(var i=0, pointsLen = points.length; i<pointsLen; i++) {
    169. // var marker = new BMap.Marker(points[i]); //将点转化成标注点
    170. map.addOverlay(marker); //将标注点添加到地图上
    171. //添加监听事件
    172. (function() {
    173. var thePoint = points;
    174. marker.addEventListener("click",
    175. function() {
    176. showInfo(this,thePoint);
    177. });
    178. })();
    179. // }
    180. }
    181. function showInfo(thisMarker,point) {
    182. //判断点是否在
    183. if(BMapLib.GeoUtils.isPointInCircle(point,circle)){
    184. var infoWindow = new BMapGL.InfoWindow("孩子在围栏内");
    185. thisMarker.openInfoWindow(infoWindow); //图片加载完后重绘infoWindow
    186. }else
    187. {
    188. var infoWindow = new BMapGL.InfoWindow("孩子不在围栏内");
    189. thisMarker.openInfoWindow(infoWindow); //图片加载完后重绘infoWindow
    190. Notify({ type: 'warning', message: '孩子不在围栏内' });
    191. }
    192. }
    193. addMarker(new BMapGL.Point(118.330997,32.275866));
    194. addMarker(new BMapGL.Point(118.33035,32.28887));
    195. }
    196. map.getContainer().appendChild(div);
    197. map.getContainer().appendChild(div2);
    198. // 将DOM元素返回
    199. return div,div2;
    200. }
    201. ZoomControl.prototype.initialize = function(map) {
    202. //创建一个dom元素
    203. var div = document.createElement('div');
    204. //添加文字说明
    205. div.appendChild(document.createTextNode('历史轨迹'));
    206. // 设置样式
    207. div.style.fontSize = "15px";
    208. div.style.width = "60px";
    209. div.style.height = "25px";
    210. div.style.cursor = "pointer";
    211. div.style.padding = "7px 10px";
    212. div.style.boxShadow = "0 2px 6px 0 rgba(27, 142, 236, 0.5)";
    213. div.style.borderRadius = "5px";
    214. div.style.backgroundColor = "white";
    215. // 绑定事件,点击一次放大两级
    216. div.onclick = function(e){
    217. // var points = [];
    218. // for (var i = 0; i < path.length; i++) {
    219. // points.push(new BMapGL.Point(path[i].lng, path[i].lat));
    220. // }
    221. var myP1 = new BMapGL.Point(118.294346,32.289725); //起点z
    222. var myP2 = new BMapGL.Point(118.33035,32.28887); //终点
    223. window.run = function() {
    224. var driving = new BMapGL.WalkingRoute(map); // 驾车实例
    225. driving.search(myP1, myP2);
    226. driving.setSearchCompleteCallback(function() {
    227. var pts = driving.getResults().getPlan(0).getRoute(0).getPath(); // 通过驾车实例,获得一系列点的数组
    228. var paths = pts.length; // 获得有几个点
    229. var pl = new BMapGL.Polyline(pts);
    230. map.addOverlay(pl)
    231. var carMk = new BMapGL.Marker(pts[0], { icon: myIcon });
    232. map.addOverlay(carMk);
    233. var i = 0;
    234. function resetMkPoint(i) {
    235. carMk.setPosition(pts[i]);
    236. if (i < paths) {
    237. setTimeout(function() {
    238. i++;
    239. resetMkPoint(i);
    240. }, 100);
    241. }
    242. }
    243. setTimeout(function() {
    244. resetMkPoint(5);
    245. }, 100);
    246. });
    247. };
    248. // var pl = new BMapGL.Polyline(points);
    249. // map.setHeading(64.5); // 左右偏移设置
    250. // map.setTilt(73); // 上下偏移设置
    251. // map.addOverlay(pl);
    252. var myIcon = new BMapGL.Icon("https://img0.baidu.com/it/u=800885108,2483076055&fm=26&fmt=auto&gp=0.jpg", new BMapGL.Size(40, 60), { // 小车图片
    253. offset: new BMap.Size(0, -5), //相当于CSS精灵
    254. imageOffset: new BMapGL.Size(0, 0) //图片的偏移量。为了是图片底部中心对准坐标点。
    255. });
    256. // window.run = function () {
    257. // var paths = points.length
    258. // var carMk = new BMapGL.Marker(points[0], { icon: myIcon });
    259. // map.addOverlay(carMk);
    260. // var i =0;
    261. // function resetMkPoint(i) {
    262. // carMk.setPosition(points[i]);
    263. // if (i < paths) {
    264. // setTimeout(function() {
    265. // i++;
    266. // resetMkPoint(i);
    267. // }, 1000);
    268. // }
    269. // }
    270. // setTimeout(function() {
    271. // resetMkPoint(5);
    272. // }, 1000);
    273. // }
    274. setTimeout(function() {
    275. run();
    276. }, 500);
    277. }
    278. // 添加DOM元素到地图中
    279. map.getContainer().appendChild(div);
    280. // 将DOM元素返回
    281. return div;
    282. }
    283. //创建控件元素
    284. var ClearCtrl = new ZoomControl();
    285. var myZoomCtrl = new ClearControl();
    286. var CircleCtrl = new CircleControl();
    287. var locatedCtrl = new locatedControl();
    288. //添加到地图中
    289. map.addControl(myZoomCtrl);
    290. map.addControl(ClearCtrl);
    291. map.addControl(CircleCtrl);
    292. map.addControl(locatedCtrl)
    293. })
    294. .catch((err) => {
    295. console.log(err)
    296. })
    297. }
    298. }