2017年04月13日 22:11:41
功能需求:
地图加载后,显示点聚合功能,聚合点显示各个地级市的名字和该地区的充电站数量,点击聚合点之后,地图放大定位到该聚合点的坐标,同时所有聚合点隐藏,显示所有的充电站点标记和每一个充电站的服务半径,点击充电站点标记,进一步缩放大并显示该充电站详细信息。
遇到的问题:
①不同缩放等级下,半径,聚合点,点标记这三组对象的显示与隐藏。
解决办法:把每个对象的所有的数据放到数组里面,调用show或者hide方法,在监听地图等级的程序里面去执行。
varmarkers_ju = []; //存放所有的点聚合标记
varmarkers_ju = []; //存放所有的点聚合标记
varcirrcles = []; //存放所有的半径标记
varcircleFlag = true; //半径画否 标志位 为true时说明半径还未画
//监听当前地图缩放等级,隐藏充电桩信息
AMap.event.addListener(map,'zoomend',function(){
if(province=="mm省"){
var poss = [115.86059,27.933078];
show_map(7,poss);
}else if(province=="nn市"){
var poss = [106.504962,29.533155];
show_map(10,poss);
}
});
functionshow_map(index,poss){
//当地图缩放等级小于要求的等级,强制变回要求的等级并定位到指定位置
if(map.getZoom() < index){
map.setZoom(index);
map.setCenter(poss);
}
if(map.getZoom() == index){
if(cirrcles != null){
cirrcles.forEach(function(temp) {
var cirrcle = temp;
cirrcle.hide();
});
}
if(markers_ju != null){
markers_ju.forEach(function(temp) {
var marker = temp;
marker.show();
});
}
if(markers != null){
markers.forEach(function(temp) {
var marker = temp;
marker.hide();
});
}
};
if(map.getZoom() > index){
if(cirrcles != null){
cirrcles.forEach(function(temp) {
var cirrcle = temp;
cirrcle.show();
});
}
if(markers != null){
markers.forEach(function(temp) {
var marker = temp;
marker.show();
});
}
if(circleFlag== true){
mf();
}
if(markers_ju != null){
markers_ju.forEach(function(temp){
var marker = temp;
marker.hide();
});
}
};
}
② 关于聚合点
本来使用的 向地图添加marker时指定图标作为marker标记,但是没法在图标上显示地区名字和站的数量的动态信息,本打算去获取页面div解决,但是最终失败,因为通过class获取的div不但有聚合点的,还有点标记的,没办法区分。最终使用的解决方案:使用的是
marker的content属性,在这个属性里面去拼接html,html里面去引入图标和动态信息以及样式布局,如下 注意: ‘/’要进行转义处理。
marker= new AMap.Marker({
offset: {x: -8,y: -34},
map: map,
title:stationName,
name:stationName,
extData: extdata_ju,
position:pos,
content: '<divstyle="background-image:url('+path+'\/governmentWorkstation\/resource\/image\/home\/dianjuhe.png);width:70px;height: 70px;"><p style=" white-space:nowrap;text-align:center;padding-top: 15px;margin-bottom: 0px;color:white;">'+stationName+'</p><pstyle="text-align:center;color: white;">'+shuliang+'</p></div>'
});
③ 点标记要在地图上添加半径功能,但是半径颜色很深,预期是浅色的。原因就是代码逻辑错误导致重复画半径。
主要是逻辑没有处理好,半径画一次就行了,即化半径的方法只执行一次,避免循环上的调用。不想让他显示就让他隐藏。
解决办法是引入标志位思路。
只要第一次画完半径,修改标志位,以后进行判断来决定是不是要走画半径的方法,避免半径重复画。
if(circleFlag){
mf();
}else{
mf_2();
}
说明:mf()和mf_2()唯一的区别就是前者比后者多了一个画半径的方法。其他一样的部分为要走的业务方法。