openlayer获取画圆的鼠标点击down与up的点

openlayer获取鼠标按下与放开的点


现在有一需求,地图有画圆事件,要求鼠标有移动才能画,否则清除。通过测试,获取圆的直径来进行判断是不可行的,将地图放大,就算点一个点也会有直径。之后想通过鼠标按下的点与松开的点进行比较。
map添加mousedowm事件没效果,那就获取圆的中心点来作为按下的点。一个点有了。
map添加mouseup注册事件有一个问题:在回调函数完成之后才会调用mouseup,这样就不能在回调过程中判断两个点是不是一个坐标点。

var upPoint;//定义一个鼠标放开全局变量
var drawControls;
var map;
function add() {
	drawControls["circle"].activate();
}
$(function() {
	map = new OpenLayers.Map("wrapper", {
		allOverlays : true,
		numZoomLevels : 19,
		displayProjection : "EPSG:4490",
		controls : [new OpenLayers.Control.Navigation(), new OpenLayers.Control.ArgParser(), new OpenLayers.Control.Attribution()]
	});
map.events.register('mouseover', map, function (e) {
        var pixel = new OpenLayers.Pixel(e.xy.x,e.xy.y);
        var lonlat = map.getLonLatFromPixel(pixel);
    upPoint =  lonlat.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
	});
	drawControls = {
		circle : new OpenLayers.Control.DrawFeature(graphicLayer2, OpenLayers.Handler.RegularPolygon, {
			handlerOptions : {
				sides : 50,
				irregular : false
			}
		})
		};
		select_control: new OpenLayers.Control.SelectFeature(graphicLayer2, {
		click: true,
		onSelect : onFeatureSelect,
		onUnselect : onFeatureUnselect
	})
		for (var key in drawControls) {
		map.addControl(drawControls[key]);
		drawControls[key].featureAdded = onAddFeature;
		drawControls[key].onModificationEnd = onAddFeature;
	}
	});

如果不用transform,获取的中心点的Y坐标会更加精确,两个点的Y坐标有误差,总之就是匹配不上

function onAddFeature(feature) {
	var centroid = feature.geometry.getCentroid();//鼠标按下通过获取圆的中心点获取
	centroid.transform(new OpenLayers.Projection("EPSG:900913"), new OpenLayers.Projection("EPSG:4326"));
	if(upPoint.lon==centroid.x&&centroid.y==upPoint.lat){
		console.log('两个坐标一样');
	}else{
		console.log('两个坐标不一样');
	}
}

效果实现,记录一下,省的到处去翻。

你可能感兴趣的:(openlayer获取画圆的鼠标点击down与up的点)