博客列表 >vue3.0与leaflet的搭建和简易demo

vue3.0与leaflet的搭建和简易demo

P粉220197790
P粉220197790原创
2022年12月13日 13:10:47650浏览

<p>hint:<br/>node => 14.17.5<br/>npm => 6.14.14</p><p>选择安装leaflet版本 '^1.7.1'<br/>以实现散点图的业务为驱动?</p><p>Map的承载Div</p><p><template><br/>    <div id="myMap2" class="mainMap2"></div></template><br/></p><p>初始化地图并且实现了几个简易功能点:</p><p>根据数个点位生成polygon并加载随机点位加载svg标记点加载标记点后重新设置标记点大小颜色再次重绘<script>import 'leaflet/dist/leaflet.css'import L from 'leaflet'import {addPloygon,addScatterPointIcon2,newScatterStyle} from "@/utils/layers"import { <br/>  onMounted,<br/>} from 'vue'export default {  name: 'Map',  props: {<br/>  },  setup(props, context) {    console.log(props,context);    let mapMap = null<br/>    let scatterLayerGroup = L.featureGroup()    onMounted(()=>{      initMap()      addPloygon([[37, -109.05],[41, -109.03],[41, -102.05],[37, -102.04],[35, -122.04]]).addTo(mapMap)<br/>    })    //获取当前可是方位随机点位的经纬度<br/>    const getRandomLatLng = map => {      let bounds = map.getBounds(),<br/>        southWest = bounds.getSouthWest(),<br/>        northEast = bounds.getNorthEast(),<br/>        lngSpan = northEast.lng - southWest.lng,<br/>        latSpan = northEast.lat - southWest.lat;      return L.latLng(<br/>        southWest.lat + latSpan Math.random(),<br/>        southWest.lng + lngSpan Math.random()<br/>      );<br/>    };    const initMap = () =>{      console.log('初始化地图');      //天地图<br/>      const image = L.tileLayer('http://t{s}.tianditu.gov.cn/img_w/wmts?tk=bb11a33c4377f10603478ed166691455&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=img&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TileMatrix={z}&TileCol={x}&TileRow={y}&#39;, {        subdomains: [0, 1, 2, 3, 4, 5, 6, 7],<br/>      })      //注记<br/>      const cia = L.tileLayer('http://t{s}.tianditu.gov.cn/cia_w/wmts?tk=bb11a33c4377f10603478ed166691455&SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&LAYER=cia&STYLE=default&TILEMATRIXSET=w&FORMAT=tiles&TileMatrix={z}&TileCol={x}&TileRow={y}&#39;, {        subdomains: [0, 1, 2, 3, 4, 5, 6, 7],        transparent: true,        zIndex: 3,<br/>      })      //天地图图组<br/>      const tiandiMap = L.layerGroup([image, cia]);      let myCenter = [31.95723698714103, 104.29901249999988]; // 设置地图中心<br/>      mapMap = L.map('myMap2',{        center: myCenter,        minZoom:1,        layers: [tiandiMap],        zoom: 3<br/>      })      let initSiteLatlon = getRandomLatLng(mapMap)<br/>      scatterLayerGroup.addLayer(addScatterPointIcon2(initSiteLatlon))<br/>      scatterLayerGroup.addTo(mapMap);<br/>    }    const changeStyle = (val,scatterRadius) =>{      let newStyle = newScatterStyle(val.color,scatterRadius.value,false)<br/>      scatterLayerGroup.eachLayer(function (layer) {<br/>        layer.setIcon(newStyle)<br/>      });<br/>    }    return {<br/>      changeStyle<br/>    }<br/>  }<br/>}<br/></script><br/></p><p>这是封装的layer生成模块=>layers.js<br/>"leaflet.markercluster": "^1.5.0",</p><p>import L from 'leaflet'/<br/> add polygon<br/> @param {} latlngs <br/> /export function addPloygon (latlngs ) {    var layer = L.polygon(latlngs , {color: 'rgb(80,227,194)'})    return layer<br/>}/<br/> add scatterPoint<br/> @param {} latlngs <br/> /<br/> export function addScatterPoint (latlng) {    let circleMarker = L.circle(latlng,{        radius:800000,        fillColor:'red',        fillOpacity:'1',        color: 'red', //颜色<br/>    })<br/>    circleMarker.on('mouseover',function(){<br/>        circleMarker.bindPopup(&lt;p&gt;Hello world!&lt;br /&gt;This is a nice popup.&lt;/p&gt;).openPopup();<br/>    })<br/>    circleMarker.on('mouseout', function(){<br/>        circleMarker.closePopup()<br/>    });    return circleMarker<br/>}/<br/> add scatterPoint VIP<br/> @param {} latlngs 点经纬度<br/> /<br/> export function addScatterPointIcon2 (latlng) {    var iconSettings = {        mapIconUrl: '<svg version="1" xmlns="http://www.w3.org/2000/svg&quot; viewBox="0 0 149 178"><path fill="{mapIconColor}" stroke="#FFF" stroke-width="6" stroke-miterlimit="30" d="M126 23l-6-6A69 69 0 0 0 74 1a69 69 0 0 0-51 22A70 70 0 0 0 1 74c0 21 7 38 22 52l43 47c6 6 11 6 16 0l48-51c12-13 18-29 18-48 0-20-8-37-22-51z"/><circle fill="{mapIconColorInnerCircle}" cx="74" cy="75" r="61"/><circle fill="#FFF" cx="74" cy="75" r="{pinInnerCircleRadius}"/><image x="34" y="35" width="80" height="80"  xlink:href="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.51tfb.com%2Fuploadfile%2F20190316%2F201903160950595c8c568363cdc.jpg&refer=http%3A%2F%2Fwww.51tfb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633143290&t=bf91e17fa94f787ffafaa729eb843a6f"/></svg>&#39;,        mapIconColor: 'red',        mapIconColorInnerCircle: '#fff',        pinInnerCircleRadius:48<br/>    };    var divIcon = L.divIcon({        className: "leaflet-data-marker",      html: L.Util.template(iconSettings.mapIconUrl, iconSettings), //.replace('#','%23'),<br/>      iconAnchor  : [12, 32],<br/>      iconSize    : [30, 30],<br/>      popupAnchor : [0, -28]<br/>    });    var marker = L.marker(latlng, {        icon: divIcon,        id: 'scatter'<br/>    });    return marker<br/>}/<br/> new scatterPoint style<br/> marker.setIcon(newScatterStyle);<br/> @param {} color 颜色<br/> @param {} radius 半径<br/> @param {} isVip 是否是vip<br/> */<br/> export function newScatterStyle (color,radius,isVip) {    var iconSettings     if(isVip){<br/>        iconSettings = {            mapIconUrl: '<svg version="1" xmlns="http://www.w3.org/2000/svg&quot; viewBox="0 0 149 178"><path fill="{mapIconColor}" stroke="#FFF" stroke-width="6" stroke-miterlimit="30" d="M126 23l-6-6A69 69 0 0 0 74 1a69 69 0 0 0-51 22A70 70 0 0 0 1 74c0 21 7 38 22 52l43 47c6 6 11 6 16 0l48-51c12-13 18-29 18-48 0-20-8-37-22-51z"/><circle fill="{mapIconColorInnerCircle}" cx="74" cy="75" r="61"/><circle fill="#FFF" cx="74" cy="75" r="{pinInnerCircleRadius}"/><image x="34" y="35" width="80" height="80"  xlink:href="https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fwww.51tfb.com%2Fuploadfile%2F20190316%2F201903160950595c8c568363cdc.jpg&refer=http%3A%2F%2Fwww.51tfb.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1633143290&t=bf91e17fa94f787ffafaa729eb843a6f"/></svg>&#39;,            mapIconColor: color,            mapIconColorInnerCircle: '#fff',            pinInnerCircleRadius:61<br/>        };<br/>     }else{<br/>        iconSettings = {            mapIconUrl: '<svg version="1" xmlns="http://www.w3.org/2000/svg&quot; viewBox="0 0 149 178"><path fill="{mapIconColor}" stroke="#FFF" stroke-width="6" stroke-miterlimit="30" d="M126 23l-6-6A69 69 0 0 0 74 1a69 69 0 0 0-51 22A70 70 0 0 0 1 74c0 21 7 38 22 52l43 47c6 6 11 6 16 0l48-51c12-13 18-29 18-48 0-20-8-37-22-51z"/></svg>',            mapIconColor: color,            mapIconColorInnerCircle: '#fff',            pinInnerCircleRadius:61<br/>        };<br/>     }    var divIcon = L.divIcon({        className: "leaflet-data-marker",      html: L.Util.template(iconSettings.mapIconUrl, iconSettings), //.replace('#','%23'),<br/>      iconAnchor  : [12, 32],<br/>      iconSize    : [radius, radius],<br/>      popupAnchor : [0, -28]<br/>    });    return divIcon<br/>}<br/></p><p>总结:以前都是用的supermap的leaflet库,初次用vue3.0配合原生leaflet。目的也是为了去熟悉原生leaflet的文档,不得不说他的文档写的很不错,比openlayer好很多。学习中成长吧,为初学者留下一点案例资料。<br/>原作者地址:https://segmentfault.com/u/yo…</p><p>下面补充一下聚合图的实例?</p><p></p><p><template>  <div><br/>    <p>32</p><br/>    <div id="myMap2" class="mainMap2"></div><br/>  </div></template><script>import 'leaflet/dist/leaflet.css'import L from 'leaflet'import "leaflet.markercluster/dist/MarkerCluster.css"import "leaflet.markercluster/dist/MarkerCluster.Default.css"import "leaflet.markercluster";//定义markar样式import icon from "leaflet/dist/images/marker-icon.png";import iconShadow from "leaflet/dist/images/marker-shadow.png";let DefaultIcon = L.icon({  iconUrl: icon,  shadowUrl: iconShadow<br/>});<br/>L.Marker.prototype.options.icon = DefaultIcon;<br/></p><p><br/></p><p><br/></p>

声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议