Oepnlayer结合HTML5 Canvas绘制底图与站点,可实现缩放平移
来源:互联网 发布:js 上传图片预览 编辑:程序博客网 时间:2024/05/05 14:41
1、准备
引用库:
<script type='text/javascript' src='OpenLayers.js'></script>
<script type='text/javascript' src='jQuery.js'></script>
元素:
<div id='map_element' style='position:absolute;width:1024px;height:768px;overflow:hidden;margin:50px;border:1px solid pink;z-index:1;'></div>
<canvas id="canvas" style="position:absolute;border:3px solid #aaa;display:block;margin:50px;"></canvas>
声明:
var map;
//canvas负责绘图,map_element负责监听鼠标事件(map_element在上,canvas在下)
var canvas,context,map_element;
canvas = document.getElementById("canvas");
map_element=document.getElementById('map_element');
2、Openlayer加载GeoServer发布的Dynamic图层
var options = {maxExtent:new OpenLayers.Bounds(497818.184013496,299871.242033547,503595.878318394,312211.085295515),maxResolution:125000,units:"m",center: new OpenLayers.LonLat(498925.826946, 304150.66056),projection: "EPSG:2436",numZoomLevels: 16};map = new OpenLayers.Map('map_element',options);var wms = new OpenLayers.Layer.WMS("OpenLayers WMS",//geoserver所在服务器地址"http://localhost:8281/geoserver/mytest/wms", {layers: "mytest:rainstation_output",});map.addLayer(wms);3、Openlayer添加站点
var pointFeature = new OpenLayers.Feature.Vector(point, null, style_point); ectorLayer_rain.addFeatures([pointFeature]); var marker = new OpenLayers.Marker(new OpenLayers.LonLat(datas_rain.points[i].lon,datas_rain.points[i].lat),jz.clone()); markers_rain.addMarker(marker); map.addLayer(markers_rain);
4、重要设置
//关键,设置中心点map.setCenter(new OpenLayers.LonLat(498925.826946, 304150.66056), 13);//将图层隐藏,否则map为白色,会覆盖canvaswms.setVisibility(false);5、Canvas绘制底图,geometry为线与面
function funDraw(extent){context.clearRect(0,0,canvas.width,canvas.height);//清空canvasfor(var i=0;i<baseMap_Json.features.length;i++){if(baseMap_Json.features[i].geometry.type=="Line"){for(var j=0;j<baseMap_Json.features[i].geometry.coordinates.length;j++){//自己修改后的公式,coordinates后加了[0],是因为调试后发现border.json的coordinates数据比模拟数据多了一对[]var X=(baseMap_Json.features[i].geometry.coordinates[0][j][0]-extent.left)*canvas.width/(extent.right-extent.left);var Y=(extent.top-baseMap_Json.features[i].geometry.coordinates[0][j][1])*canvas.height/(extent.top-extent.bottom);if(j==0){context.beginPath();context.moveTo(X,Y);}else if(j==baseMap_Json.features[i].geometry.coordinates[0].length-1){context.lineTo(X,Y);context.strokeStyle = "blue";context.stroke();}else{context.lineTo(X,Y);}}}else if(baseMap_Json.features[i].geometry.type=="Polygon"){for(var j=0;j<baseMap_Json.features[i].geometry.coordinates[0].length;j++){var X=(baseMap_Json.features[i].geometry.coordinates[0][j][0]-extent.left)*canvas.width/(extent.right-extent.left);var Y=(extent.top-baseMap_Json.features[i].geometry.coordinates[0][j][1])*canvas.height/(extent.top-extent.bottom);if(j==0){context.beginPath();context.moveTo(X,Y);}else if(j==baseMap_Json.features[i].geometry.coordinates[0].length-1){context.lineTo(X,Y);context.closePath();context.strokeStyle = "black";context.stroke();}else{context.lineTo(X,Y);}}}}}
6、Openlayer图层与Canvas联动(鼠标的平移缩放)//监听鼠标拖拽事件map_element.onmousedown=function(event){map_element.onmousemove=function(event){var extent=map.getExtent();map_element.style.cursor="move";funDraw(extent);//重新绘制地图}map_element.onmouseup=function(){map_element.onmousemove=null;map_element.onmouseup=null;map_element.style.cursor="default";}map_element.onmouseout=function(){map_element.onmousemove=null;map_element.onmouseup=null;map_element.style.cursor="default";}}//监听鼠标滚轮事件map_element.onmousewheel=map_element.onwheel=function(event){//chrome firefox浏览器兼容var t=setTimeout(test,5);//延迟执行,可获取地图最后状态的Extent}
1 0
- Oepnlayer结合HTML5 Canvas绘制底图与站点,可实现缩放平移
- arcgis api for js入门开发系列二十一 用HTML5 canvas绘制地图 瓦片加载平移缩放
- canvas加载图片·绘制像素图·旋转平移缩放图片
- Html5 canvas学习5-图形变形:缩放 旋转 平移 变形
- html5 canvas 实现简单绘制折线图
- canvas 平移 缩放 旋转
- HTML5 Canvas 绘制椭圆与椭圆弧的实现
- OpenGL绘制简单场景,实现旋转缩放平移和灯光效果
- html5 canvas 图像转换与缩放
- 可平移缩放旋转的ImageView的实现
- 利用canvas制作图片(可缩放和平移)+相框+文字
- html5学习笔记三:canvas中平移,缩放,旋转等图像变换问题
- HTML5 Canvas图像缩放
- Bitmap和Canvas的平移、缩放、旋转
- canvas旋转,平移,缩放一二例
- echart.js绘制可缩放折线图
- HTML5 vedio标签与canvas的结合实现视频同步模糊效果
- 利用 html5 canvas 简单绘制折线图
- 详述 Java 语言中的两种 for 循环
- yii2-rbac及yii2-admin可视化管理
- 虚拟机性能监控与实战
- 【bzoj2326】[HNOI2011]数学作业
- ubuntu中ifconfig -a只有lo的问题
- Oepnlayer结合HTML5 Canvas绘制底图与站点,可实现缩放平移
- 网易计算机视觉笔试题
- Android中多图片选择器ImagePicker库的使用(仿微信、非常容易集成)
- Ubuntu 15.10 搜狗搜狗输入法崩溃解决方法
- level使用说明
- SEO七大常见误区知多少
- bzoj4525
- git 找回丢失的对象 找回commit
- 网络流24题之T7 试题库问题