前天突发奇想作一个地图定位的前端界面,因而就研究了一下百度定位功能。新手,历时两天终于完成了本次的设计。虽然看上去简单,可是用到的东西很多。我在网上找资源的时候还真没找到对应的资源,感受有必要发出来当作笔记,你们还能够自由扩展它的功能。
闲话很少说,介绍工具:
开发工具:webstrom;
平台框架:vue+webpack+echarts+百度地图。
预览效果:php
首先咱们要搭载百度地图的开发环境,这是我参考的博客地址:点击连接跳转html
加载echarts插件:
使用npm添加package.json文件中的配置并下载相关npm包依赖前端
npm install echarts --save
而后在项目文件的入口js文件main.js中添加vue
import echarts from "echarts" Vue.use(echarts) Vue.prototype.$echarts = echarts
环境搭载完成,直接上主菜:webpack
<template> <div style="width:800p; height:600px"> <div id="mapContainer" style="width:100%; height:100%"></div> </div> </template> <script> import 'echarts/map/js/china.js'; //引入中国地图 import BMap from 'BMap' //引入百度地图 //调用百度地图ip定位接口 let myCity = new BMap.LocalCity(); export default { name: '', data(){ return{ geoCoordMap:{}, geo:'' } }, mounted() { //获取IP地址接口信息 myCity.get(this.myFun); }, methods: { //echarts图的data数据处理 convertData (data) { let res = []; for (let i = 0; i < data.length; i++) { let geoCoord = this.geoCoordMap[data[i].name]; if (geoCoord) { res.push({ name: data[i].name, value: geoCoord.concat(data[i].value) }); } } return res; }, //百度地图接口信息处理函数 myFun(result){ this.geo = result.name; let arryMap=[result.center.lng,result.center.lat]; this.$set(this.geoCoordMap,result.name,arryMap); this.getMap(); }, //echarts图 getMap(){ let myChart = this.$echarts.init(document.getElementById('mapContainer')); myChart.setOption ({ backgroundColor: '#fff', title: { text: '位置 : ' + this.geo, x:'center', }, tooltip: { trigger: 'item', formatter: function (params) { return '位置' + ' : ' + params.name; } }, geo: { map: 'china', label: { emphasis: { show: false } }, itemStyle: { normal: { areaColor: '#fff', borderColor: '#111' }, emphasis: { areaColor: '#ccc' } } }, series: [ { name: '所在城市', type: 'effectScatter', coordinateSystem: 'geo', data: this.convertData([{name: this.geo, value: 9},]), symbolSize: 12, showEffectOn: 'render', rippleEffect: { brushType: 'stroke' }, hoverAnimation: true, label: { normal: { show: false }, emphasis: { show: false } }, itemStyle: { emphasis: { borderColor: '#fff', borderWidth: 1 } } } ] }) } } } </script>
还有根据浏览器定位的源代码,不过要通过使用者赞成。给出源代码,不作研究web
//百度地图浏览器定位 let geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition(function(r){ if(this.getStatus() == BMAP_STATUS_SUCCESS){ let mk = new BMap.Marker(r.point); map.addOverlay(mk); map.panTo(r.point); alert('您的位置:'+r.point.lng+','+r.point.lat); } else { alert('failed'+this.getStatus()); } },{enableHighAccuracy: true})
根据echarts和百度地图相关api,你们还能够在此基础上扩展不少功能,有相关问题或意见能够评论讨论哦。下面是官方文档飞机路线
echarts——>点我点我
百度地图——>点我点我npm