当前位置:   article > 正文

Oepnlayer结合HTML5 Canvas绘制底图与站点,可实现缩放平移_openlayer canvas 平移

openlayer canvas 平移

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图层

  1. var options = {
  2. maxExtent:new OpenLayers.Bounds(497818.184013496,299871.242033547,503595.878318394,312211.085295515),
  3. maxResolution:125000,
  4. units:"m",
  5. center: new OpenLayers.LonLat(498925.826946, 304150.66056),
  6. projection: "EPSG:2436",
  7. numZoomLevels: 16
  8. };
  9. map = new OpenLayers.Map('map_element',options);
  10. var wms = new OpenLayers.Layer.WMS("OpenLayers WMS",
  11. //geoserver所在服务器地址
  12. "http://localhost:8281/geoserver/mytest/wms",
  13. {
  14. layers: "mytest:rainstation_output",
  15. });
  16. map.addLayer(wms);
3、Openlayer添加站点

  1. var pointFeature = new OpenLayers.Feature.Vector(point, null, style_point);
  2. ectorLayer_rain.addFeatures([pointFeature]);
  3. var marker = new OpenLayers.Marker(new OpenLayers.LonLat(datas_rain.points[i].lon,datas_rain.points[i].lat),jz.clone());
  4. markers_rain.addMarker(marker);
  5. map.addLayer(markers_rain);

4、重要设置

  1. //关键,设置中心点
  2. map.setCenter(new OpenLayers.LonLat(498925.826946, 304150.66056), 13);
  3. //将图层隐藏,否则map为白色,会覆盖canvas
  4. wms.setVisibility(false);
5、Canvas绘制底图,geometry为线与面

  1. function funDraw(extent){
  2. context.clearRect(0,0,canvas.width,canvas.height);//清空canvas
  3. for(var i=0;i<baseMap_Json.features.length;i++)
  4. {
  5. if(baseMap_Json.features[i].geometry.type=="Line")
  6. {
  7. for(var j=0;j<baseMap_Json.features[i].geometry.coordinates.length;j++)
  8. {
  9. //自己修改后的公式,coordinates后加了[0],是因为调试后发现border.json的coordinates数据比模拟数据多了一对[]
  10. var X=(baseMap_Json.features[i].geometry.coordinates[0][j][0]-extent.left)*canvas.width/(extent.right-extent.left);
  11. var Y=(extent.top-baseMap_Json.features[i].geometry.coordinates[0][j][1])*canvas.height/(extent.top-extent.bottom);
  12. if(j==0)
  13. {
  14. context.beginPath();
  15. context.moveTo(X,Y);
  16. }
  17. else if(j==baseMap_Json.features[i].geometry.coordinates[0].length-1)
  18. {
  19. context.lineTo(X,Y);
  20. context.strokeStyle = "blue";
  21. context.stroke();
  22. }
  23. else
  24. {
  25. context.lineTo(X,Y);
  26. }
  27. }
  28. }
  29. else if(baseMap_Json.features[i].geometry.type=="Polygon")
  30. {
  31. for(var j=0;j<baseMap_Json.features[i].geometry.coordinates[0].length;j++)
  32. {
  33. var X=(baseMap_Json.features[i].geometry.coordinates[0][j][0]-extent.left)*canvas.width/(extent.right-extent.left);
  34. var Y=(extent.top-baseMap_Json.features[i].geometry.coordinates[0][j][1])*canvas.height/(extent.top-extent.bottom);
  35. if(j==0)
  36. {
  37. context.beginPath();
  38. context.moveTo(X,Y);
  39. }
  40. else if(j==baseMap_Json.features[i].geometry.coordinates[0].length-1)
  41. {
  42. context.lineTo(X,Y);
  43. context.closePath();
  44. context.strokeStyle = "black";
  45. context.stroke();
  46. }
  47. else
  48. {
  49. context.lineTo(X,Y);
  50. }
  51. }
  52. }
  53. }
  54. }
6、Openlayer图层与Canvas联动(鼠标的平移缩放)

  1. //监听鼠标拖拽事件
  2. map_element.οnmοusedοwn=function(event){
  3. map_element.οnmοusemοve=function(event){
  4. var extent=map.getExtent();
  5. map_element.style.cursor="move";
  6. funDraw(extent);//重新绘制地图
  7. }
  8. map_element.οnmοuseup=function(){
  9. map_element.οnmοusemοve=null;
  10. map_element.οnmοuseup=null;
  11. map_element.style.cursor="default";
  12. }
  13. map_element.οnmοuseοut=function(){
  14. map_element.οnmοusemοve=null;
  15. map_element.οnmοuseup=null;
  16. map_element.style.cursor="default";
  17. }
  18. }
  19. //监听鼠标滚轮事件
  20. map_element.onmousewheel=map_element.οnwheel=function(event){//chrome firefox浏览器兼容
  21. var t=setTimeout(test,5);//延迟执行,可获取地图最后状态的Extent
  22. }



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/816682
推荐阅读
  

闽ICP备14008679号