openLayers2点聚合常用操作

1、地图初始化及响应事件

	map = new Geo.View2D.Map("mapContainerId", {   
		eventListeners: {
			'zoomend': zoomendF
		}
	})
	// 地图注册事件的另一种方式
	map.events.register('moveend', this, moveendF)
	
	// 基础图层
	let baseLayer = new OpenLayers.Layer('baseLayer', {
		maxResolution: (360 / 256) / Math.pow(2, 10),
	    minResolution: (360 / 256) / Math.pow(2, 19),
	    maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90),
	    displayInLayerSwitcher: false,
	    isBaseLayer: true,
    })
	map.addControl(new Geo.View2D.Control.MousePosition())
	map.addLayer(baseLayer)

2、点聚合、描点、点及聚合点自定义图标

	clusterLayer = new OpenLayers.Layer.Vector('clusterLayer', {
		strategis: [strategy],
		styleMap: new OpenLayers.StyleMap({
			default: style,
			select: {}
		})
	})
	for (let i = 0; i < list.length; i++) {
		let point3 = new Geo.Geometry.Point(list[i].lon, list[i].lat);
        let geometry = point3.clone();
        // 第二个参数,将点信息放到cluster的attributes属性中
        let pointFeature3 = new Geo.Feature.Vector(geometry, list[i]); 
	}
	// strategy定义
	let strategy = new Geo.Strategy.Cluster({
        distance: 40,
        active: true
    })
 	strategy.distance = 50 || strategy.distance;
 	strategy.threshold = 1 || strategy.threshold;

	// style定义
	style = new OpenLayers.Style({
		pointRadius: '${getPointRadius}',
		label: '${getLabel}',
		externalGraphic: '${getIcon}',   // 点、聚合点图标
		graphicHeight: '${getHeight}',  // 图标宽高,可根据实际情况处理
		graphicWidth: '${getWidth}'
		...
		其他属性
		...
	}{
		context: {
			getPointRadius: function(feature) {
				let pix = 10;
                if (feature.cluster) {
                    pix = Math.min(feature.attributes.count, 7) + 10;
                }
                return pix;
			},
			getLabel: function(feature) {
				// 聚合点展示点个数,单个点则不展示
				return feature.attributes.count > 1 ? feature.attibutes.count : ''
			},
			getIcon: function(feature) {
				if(feature.attributes.count > 1) {
					return '单个点图标路径'
				} else {
					return '聚合点图标'
				}
			}
		}
	})

3、鼠标悬浮展示弹窗

hoverSelect = new OpenLayers.Control.SelectFeature(clusterLayer, {
	hover: true,
	highlightOnly: true
})
map.addControl(hoverSelect)
hoverSelect.activate()
let popup = null
hoverSelect.events.on({
	'featurehighlighted': function(data){
		let {feature} = data
        if (feature.popup) {
             return
         }
		let content = '自定义内容'
		popup = new OpenLayers.Popup("chicken",
            feature.geometry.getBounds().getCenterLonLat(),
            new OpenLayers.Size(280, 140),
            content,
            null,
            true,
            onPopupClose
        )
        feature.popup = popup
        map.addPopup(popup)
	},
	'featureunhighlighted': function (event) {
         let feature = event.feature
         if (feature.popup) {
             map.removePopup(feature.popup);
             feature.popup.destroy()
             delete feature.popup
         }
         if (feature.attributes && feature.attributes.count == 1) {
             map.removePopup(popup)
         }
     }
})


function onPopupClose() {
	 hoverSelect.unselectAll()
}

4、鼠标点击展示弹窗

iSelect = new OpenLayers.Control.SelectFeature(clusterLayer, {
	 clickout: true,
     toggle: false,
     multiple: false,
     hover: false,
     toggleKey: "ctrlKey", // ctrl key removes from selection
     multipleKey: "shiftKey", // shift key adds to selection,
     box: false
})
map.addControl(hoverSelect)
iSelect.activate()
clusterLayer.events.on({
	featureselected: function(event) {
		 if (feature.popup) {
            map.removePopup(feature.popup);
             feature.popup.destroy();
             delete feature.popup;
         }
         // 打开弹窗,操作同上
	},
	featureunselected:  function() {
		// 关闭弹窗,操作同上
	}
})

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值