(笔记)记项目中的一个算法

场景:主要是用来解决百度地图上面,marker由于经纬度相同而造成的重叠问题,需求是要区分开重叠的点(=.=!!!)。所以决定把经纬度相同的点取出来只加一个marker,然后infowindow上显示重叠点的所有信息。这个方案的难点在于,要把对象数组里经纬度相同的对象跟其他对象区分开来,由下面的算法来处理。

 1     // 把经纬度相同的点 与 其他点分开
 2     function separatePoints(oInfo) {
 3         var arrPoint = [],
 4               len = oInfo.length;
 5 
 6         for (var i = 0; i < len; i++) {
 7             var point = {
 8                 lng: oInfo[i].lon,
 9                 lat: oInfo[i].lat
10             };
11             arrPoint.push(point);
12         }
13 
14         var index = [];
15         for (var i = 0; i < len; i++) {
16             var flag = false,
17                   samePoints = [],
18                   thisPoint = arrPoint[i];
19             if (index.indexOf(i) == -1) {
20                 for (var j = i + 1; j < len; j++) {
21                     if (index.indexOf(j) == -1) {
22                         var thatPoint = arrPoint[j];
23                         if ((thisPoint.lng == thatPoint.lng) && (thisPoint.lat == thatPoint.lat)) {
24                             if (!flag) {
25                                 flag = true;
26                                 samePoints.push(oInfo[i]);
27                                 index.push(i);
28                             }
29                             samePoints.push(oInfo[j]);
30                             index.push(j);
31                         }
32                     }
33                 }
34                 if (samePoints.length > 0) {
35                     addSamePoint(samePoints);
36                 }
37             }
38         }
39 
40         var count = 0;
41         for (var x = 0, theLen = index.length; x < theLen; x++) {
42             oInfo.splice(index[x] - count, 1);
43             count++;
44         }
45         showBMPoint(oInfo);
46     }

 

posted @ 2016-11-21 10:22  wiHom-  阅读(204)  评论(0编辑  收藏  举报