使用vue引入maptalks地图及聚合效果的实现


Posted in Javascript onAugust 10, 2020

1、安装maptalks.js

npm install maptalks --save

2、安装聚合mapkercluster

npm install maptalks.markercluster

3、vue页面引入

import * as maptalks from 'maptalks'

import {ClusterLayer} from 'maptalks.markercluster'

4、初始化地图并添加聚合

mounted() {
 let that = this
 //--0--//地图对象的初始化
 this.map = new maptalks.Map('map', {
  center: [109.1748453547,21.4586700546],
  //中心点标记红十字,用于开发debug
  centerCross : false,
  zoom: 13,
  minZoom : 10,
  maxZoom : 18,
  //缩放级别控件
  zoomControl : false, // add zoom control
  scaleControl : true, // add scale control
  //鹰眼控件
  overviewControl : true, // add overview control
  //设置瓦片图层的空间参考spatialReference默认就是3857,googlemap的分辨率
  spatialReference : {
   projection : 'EPSG:3857'
   //与map一样,支持更详细的设置resolutions,fullExtent等
  },
  baseLayer: new maptalks.TileLayer('base', {
   // urlTemplate: 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
   //renderer : 'canvas', // set TileLayer's renderer to canvas
   //底图服务器地址,如下为瓦片地址
   urlTemplate: 'http://xxx.xx.xxx.xxx:xxxx/mapdata/tiles/{z}/{x}/{y}.png',
   //tileSystem 控制瓦片的x,y以及行列,后两个是origin原点位置(很重要)
   tileSystem : [1, 1, -20037508.3427890,-20037508.3427890], // tile system
   //subdomains: ['a','b','c'],
   minZoom : 10,
   maxZoom : 18
   // css filter 滤镜配置
   // cssFilter : 'sepia(60%) invert(95%)',
   // attribution: '© <a href="http://maptalks.org/" rel="external nofollow" target="_blank">Maptalk for Amap</a> contributors'
  }),
  layers : [
   new maptalks.VectorLayer('v')
  ],
  attribution: {//左下角info
   content: '©qmap'
  }
 })
 
 // 拖动范围限制,黑框控
 let extent = new maptalks.Extent(108.8584570000,20.9790840000,110.0569128018,22.1177123207)
 // var extent = new maptalks.Extent(112.5381688894,26.8876543885,112.5605009244,26.9012691519);
 // set map's max extent to map's extent at zoom 14
 this.map.setMaxExtent(extent)
 this.map.setZoom(this.map.getZoom(), { animation : false })
 this.map.getLayer('v')
  .addGeometry(
   new maptalks.Polygon(extent.toArray(), {
    symbol : { 'polygonOpacity': 0, 'lineWidth': 0 }
   })
  )
 
// 往地图上添加点位
 
this.markInfo()
},
 
methods: {
 setCenter: function(center) {
  //标注点平移到某个点
  let centerV = maptalks1.CRSTransform.transform(center, 'bd09ll', 'gcj02')
  this.map.animateTo({
   zoom: 17,
   center: centerV
  }, {
   duration: 1000
  })
 },
 // 上图
 markInfo: function () {
  let that = this
  that.map.removeLayer(that.clusterLayer)
  let markers = []
  //--2--//前端聚合查询
  // data from realworld.50000.1.js
  //需要引入maptalks.markercluster.js
  //数据格式[lon,lat,name]
  // 如:[[21.8129763667, 109.2714296333, "晓港名城4号楼"],[21.8131727667, 109.2710308833, "晓港名城6号楼"]]
  for (let i = 0; i < that.addressPoints.length; i++) {
   let a = that.addressPoints[i]
   markers.push(new maptalks.Marker(maptalks1.CRSTransform.transform([a.latitude, a.longitude], 'bd09ll', 'gcj02'),
    {
     'properties': {
      'name': a.name,
      'onSale': a.onSale
     },
     symbol : [
      {
       'markerFile'  : a.onSale ? require('../../../static/img/on.png') : require('../../../static/img/off.png'),//标注点图标
       'markerWidth' : 30,
       'markerHeight' : 35
      },{
       'textName' : '{name}',
       'textSize' : 12,
       'textDy'  : -50,
       'textHaloRadius' : 5,
       'textHaloFill' : a.onSale ? '#FFB427' : '#B9B9B9',
       'textFill' : '#fff' // color
      }
     ]
    }
   ))//.on('mousedown', onClick))
  }
  let clusterLayer = new ClusterLayer('cluster', markers, {
   'noClusterWithOneMarker' : true,
   'noClusterWithHowMany': 8,//聚合的最小个数
   'maxClusterZoom' : 15,
   //"count" is an internal variable: marker count in the cluster.
   'symbol': {
    'markerType' : 'ellipse',
    'markerFill' : { property:'count', type:'interval', stops: [[0, 'rgb(135, 196, 240)'], [9, '#1bbc9b'],[50, 'rgb(116, 115, 149)'],
      [99, 'rgb(216, 115, 149)']]},
    'markerFillOpacity' : 0.7,
    'markerLineOpacity' : 1,
    'markerLineWidth' : 3,
    'markerLineColor' : '#fff',
    'markerWidth' : { property:'count', type:'interval', stops: [[0, 40], [9, 60], [50, 70],[99, 80]] },
    'markerHeight' : { property:'count', type:'interval', stops: [[0, 40], [9, 60], [50, 70],[99, 80]] }
   },
   'drawClusterText': true,
   'geometryEvents' : true,
   'single': true
  })
  that.map.addLayer(clusterLayer)
  that.clusterLayer = clusterLayer
 
  function onClick(e) {
   e.target.setInfoWindow({
    'content': '<div class="content-' + e.target.properties.onSale + '">' + e.target.properties.name + '</div>',
    'width' : 150,
    'dy' : 5,
    'autoPan': true,
    'custom': false,
    'autoOpenOn' : 'click', //set to null if not to open when clicking on marker
    'autoCloseOn' : 'click'
   })
  }
 }
}

补充知识:vue集成maptalk实现geojson3D渲染

我就废话不多说了,大家还是直接看代码吧~

//实例化地图对象
  let map = new maptalks.Map("map",{
   center: [13.416935229170008, 52.529564137540376],
   zoom: 20,
   dragPitch : true,
   //allow map to drag rotating, true by default
   dragRotate : true,
   //enable map to drag pitching and rotating at the same time, false by default
   dragRotatePitch : true,
   baseLayer: new maptalks.TileLayer('base', {
    urlTemplate: 'https://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png',
    subdomains: ['a','b','c','d'],
    attribution: '© <a href="http://osm.org" rel="external nofollow" >OpenStreetMap</a> contributors, © <a href="https://carto.com/" rel="external nofollow" >CARTO</a>'
   })
  });

// features to draw
//将Buildings中的数据,添加到features中
  let features = [];

  buildings.forEach(function (b) {
   console.log(b.features);
   features = features.concat(b.features);
  });

// the ThreeLayer to draw buildings
  let threeLayer = new ThreeLayer('t', {
   forceRenderOnMoving : true,
   forceRenderOnRotating : true
  });


  threeLayer.prepareToDraw = function (gl, scene, camera) {

   let me = this;
   let light = new THREE.DirectionalLight(0xffffff);
   light.position.set(0, -10, 10).normalize();
   scene.add(light);

   features.forEach(function (g) {
    let heightPerLevel = 5;
    let levels = g.properties.levels || 1;
    let color = 0x2685a7

    let m = new THREE.MeshPhongMaterial({color: color, opacity : 0.7});
    //change to back side with THREE <= v0.94
    // m.side = THREE.BackSide;

    let mesh = me.toExtrudeMesh(maptalks.GeoJSON.toGeometry(g), heightPerLevel, m, heightPerLevel);
    if (Array.isArray(mesh)) {
     scene.add.apply(scene, mesh);
    } else {
     scene.add(mesh);
    }
   });
  };

  threeLayer.addTo(map);

以上这篇使用vue引入maptalks地图及聚合效果的实现就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Javascript 相关文章推荐
分享十五个最佳jQuery 幻灯插件和教程
Mar 27 Javascript
javascript随机之洗牌算法深入分析
Jun 07 Javascript
jQuery.ajax 跨域请求webapi设置headers的解决方案
Aug 08 Javascript
微信小程序 textarea 详解及简单使用方法
Dec 05 Javascript
详解vue 中使用 AJAX获取数据的方法
Jan 18 Javascript
node操作mysql数据库实例详解
Mar 17 Javascript
cropper js基于vue的图片裁剪上传功能的实现代码
Mar 01 Javascript
基于vue的验证码组件的示例代码
Jan 22 Javascript
详解无限滚动插件vue-infinite-scroll源码解析
May 12 Javascript
微信小程序webview组件交互,内联h5页面并网页实现微信支付实现解析
Aug 16 Javascript
ES6常用小技巧总结【去重、交换、合并、反转、迭代、计算等】
Dec 21 Javascript
vue-cli4项目开启eslint保存时自动格式问题
Jul 13 Javascript
vue-video-player实现实时视频播放方式(监控设备-rtmp流)
Aug 10 #Javascript
解决vue+webpack项目接口跨域出现的问题
Aug 10 #Javascript
vue 导航锚点_点击平滑滚动,导航栏对应变化详解
Aug 10 #Javascript
vue添加锚点,实现滚动页面时锚点添加相应的class操作
Aug 10 #Javascript
vue 实现锚点功能操作
Aug 10 #Javascript
vscode 使用Prettier插件格式化配置使用代码详解
Aug 10 #Javascript
Vue-cli 移动端布局和动画使用详解
Aug 10 #Javascript
You might like
Gregarius中文日期格式问题解决办法
2008/04/22 PHP
php中substr()函数参数说明及用法实例
2014/11/15 PHP
php实现比较两个字符串日期大小的方法
2015/05/12 PHP
php pthreads多线程的安装与使用
2016/01/19 PHP
PHP设计模式之装饰器模式实例详解
2018/02/07 PHP
js自带函数备忘 数组
2006/12/29 Javascript
JS对象与JSON格式数据相互转换
2012/02/20 Javascript
jQuery 获取和设置select下拉框的值实现代码
2013/11/08 Javascript
Javascript与jQuery方法的隐藏与显示
2015/01/19 Javascript
jQuery实现炫酷的鼠标轨迹特效
2015/02/01 Javascript
JavaScript从数组中删除指定值元素的方法
2015/03/18 Javascript
18个非常棒的jQuery代码片段
2015/11/02 Javascript
jQuery实现微信长按识别二维码功能
2016/08/26 Javascript
微信小程序访问node.js接口服务器搭建教程
2017/04/25 Javascript
ES6新特性:使用export和import实现模块化详解
2017/07/31 Javascript
详解VueJS应用中管理用户权限
2018/02/02 Javascript
详解Angular系列之变化检测(Change Detection)
2018/02/26 Javascript
详解node Async/Await 更好的异步编程解决方案
2018/05/10 Javascript
解决element UI 自定义传参的问题
2018/08/22 Javascript
weui中的picker使用js进行动态绑定数据问题
2019/11/06 Javascript
如何在vue项目中嵌入jsp页面的方法(2种)
2020/02/06 Javascript
使用url_helper简化Python中Django框架的url配置教程
2015/05/30 Python
Python调用SQLPlus来操作和解析Oracle数据库的方法
2016/04/09 Python
python 如何快速找出两个电子表中数据的差异
2017/05/26 Python
python @property的用法及含义全面解析
2018/02/01 Python
pandas or sql计算前后两行数据间的增值方法
2018/04/20 Python
pip安装时ReadTimeoutError的解决方法
2018/06/12 Python
python实现高斯投影正反算方式
2020/01/17 Python
约瑟夫·特纳男装:Joseph Turner
2017/10/10 全球购物
高中班主任评语大全
2014/04/25 职场文书
亲子阅读的活动方案
2014/08/15 职场文书
以幸福为主题的活动方案
2014/08/22 职场文书
承德避暑山庄导游词
2015/02/03 职场文书
工作时间证明
2015/06/15 职场文书
怎样做好公众演讲能力?
2019/08/28 职场文书
设置IIS Express并发数
2022/07/07 Servers