web123456

The number of display markers is different in different map levels

<template> <div class="hot_wrapper"> <div class="map"> <div style="height:100%;width:100%;outline: none!important;" id="container" tabindex="0"> </div> </div> </div> </template> <script> import AMap from "AMap"; export default { data() { return { adCode: 330600, // Shaoxing disProvince: null, // Regional layer depth: 2, // Regional level 0 provincial level 1 municipal level 2 district and county level colors: {}, // Regional color 330683 Shanchengzhou City // 330624 Xinchang County // 330604 Shangyu District // 330602 Yuecheng District // 330603 Keqiao District // 330681 Zhuji City city: 'Shaoxing City', mapData:[],// Map latitude and longitude data mapDataMarker:[],//The number of changes in the graph level changes pathContentArr:[],//Tips on the number of changes in the map level unitId:'',//Unit id iconPath:'',//Icon Path } }, methods:{ initMap() {// Create a map var that = this this.map = new AMap.Map('container', { zoom:10, zooms: [10,14], center:[120.535719,29.956348], // center:[120.580444,29.859701], resizeEnable: true, showIndoorMap: false, mapStyle:"amap://styles/0206dfbcbabc11e4971c1a2e6bcdda2e", // mapStyle:"amap://styles/darkblue", features:['point','road','bg'],// Map elements viewMode:"2D", pitch:45, zoomEnable:true, // dragEnable: false, }) AMap.plugin('', function() { that.geocoder = new AMap.Geocoder({ }) }) that.drawMarkerByDistance(that.map.getZoom())//Initial call to marker filtering method. Rendering the map this.map.on("zoomchange",function(){//Change the level. Point re-render //Clear the point information on the map for (var i = that.mapDataMarker.length - 1; i >= 0; i--) { that.mapDataMarker[i].setMap(null); } for (var i = that.pathContentArr.length - 1; i >= 0; i--) { that.pathContentArr[i].setMap(null); } that.mapDataMarker=[]; that.pathContentArr=[]; that.drawMarkerByDistance(that.map.getZoom()) }) }, }, Rad(d){//Convert latitude and longitude into a medium-sized table form of trigonometric function. return d * Math.PI / 180.0; }, //Calculate the distance, the parameters are the latitude and longitude of the first point; the latitude and longitude of the second point are respectively; GetDistance(lat1,lng1,lat2,lng2){ var radLat1 = this.Rad(lat1); var radLat2 = this.Rad(lat2); var a = radLat1 - radLat2; var b = this.Rad(lng1) - this.Rad(lng2); var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2))); s = s *6378.137 ;// EARTH_RADIUS; s = Math.round(s * 10000) / 10000; //The output is kilometers return s; }, // Calculate the distance rendering point drawMarkerByDistance(zoom){ var that=this; var perDistance=0; //Current cumulative kilometers var zoom=zoom||10; switch (zoom){ case 10: var targetDistance=20 //km break; case 11: var targetDistance=10; //km break; case 12: var targetDistance=5; //km break; case 13: var targetDistance=1; //km break; default: var targetDistance=0.25; //km } var prepareToShowMarker=[];//Copy the obtained latitude and longitude data and perform conditional filtering prepareToShowMarker=JSON.parse(JSON.stringify(that.mapData)) for(var i in prepareToShowMarker){ prepareToShowMarker[i].count=1 } var select=function(arr){//Filter function var flag=true; for(var i=0;i<arr.length;i++){ for(var j=i+1;j<arr.length;j++){ var curdistance= that.GetDistance(arr[i].latitude,arr[i].longitude,arr[j].latitude,arr[j].longitude); //If there are still close points if (curdistance<targetDistance) { flag=false; //Fixed to delete a point arr[i].count=arr[i].count+arr[j].count; arr.splice(j,1) //Randomly remove a point between i and j //var randomNum= (); // if(randomNum>0.5){ // arr[j].count=arr[j].count+arr[i].count; // }else{ // arr[i].count=arr[i].count+arr[j].count; // } // randomNum>0.5?(i,1):(j,1); break; } } if(flag==false){ break; } } // Exit recursion when the distance between all points in the array is greater than targetDistance if (flag==false) { select(arr); } } // Filter points select(prepareToShowMarker); // Render loop to the map prepareToShowMarker.forEach(function(marker){ //Point Rendering var index=new AMap.Marker({ map: that.map, icon: new AMap.Icon({ image: that.iconPath, size: new AMap.Size(70, 70), //Icon size imageSize: new AMap.Size(70, 70) }), position: [marker.longitude, marker.latitude], offset: new AMap.Pixel(-13, -30), clickable: true }); that.mapDataMarker.push(index) index.on('click', markerUnitClick); //Quantity prompt var num = new AMap.Marker({//Create alarm count content:`<span style="color:#fff;text-align: center;">${marker.count}</span>`, offset: new AMap.Pixel(10,-22) // Offset position relative to the base point }); num.setMap(that.map); num.setPosition([marker.longitude, marker.latitude]); that.pathContentArr.push(num) num.on('click', markerUnitClick); function markerUnitClick(){ that.$emit('markerUnitMap',marker.id); } }) } }