features定位.html 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>features定位</title>
  7. <link rel="stylesheet" href="../../dist/index.css" />
  8. <script src="../../dist/index.js"></script>
  9. <style>
  10. body {
  11. margin: 0
  12. }
  13. #map {
  14. height: 100vh;
  15. }
  16. .control {
  17. position: absolute;
  18. top: 20px;
  19. left: 40px;
  20. padding: 10px 10px 0;
  21. background: #fff;
  22. }
  23. </style>
  24. </head>
  25. <body>
  26. <div id="app">
  27. <div id="map"></div>
  28. <div class="control">
  29. <button id="fixed">setFitView</button>
  30. </div>
  31. </div>
  32. </body>
  33. <script>
  34. let map = new CTMapOl.extend.InitMap({
  35. domId: 'map'
  36. })
  37. function addCircle(p) {
  38. // 创建点要素
  39. let pointFeature = new CTMapOl.Feature({
  40. geometry: new CTMapOl.geom.Point(CTMapOl.proj.fromLonLat(p)) // 设置点要素的位置(这里以成都为例)
  41. });
  42. // 创建点要素的样式
  43. let pointStyle = new CTMapOl.style.Style({
  44. image: new CTMapOl.style.Circle({
  45. radius: 6,
  46. fill: new CTMapOl.style.Fill({ color: 'red' }),
  47. stroke: new CTMapOl.style.Stroke({ color: 'white', width: 2 })
  48. })
  49. });
  50. // 将点要素和样式添加到矢量图层
  51. let vectorLayer = new CTMapOl.layer.Vector({
  52. source: new CTMapOl.source.Vector({
  53. features: [pointFeature]
  54. }),
  55. style: pointStyle
  56. });
  57. // 将矢量图层添加到地图
  58. map.map.addLayer(vectorLayer);
  59. return pointFeature;
  60. }
  61. let points = [];
  62. [[104.06, 30.67],[105.06, 31.67],[114.06, 20.67]].forEach(v => {
  63. let f = addCircle(v);
  64. points.push(f)
  65. })
  66. let mapTool = new CTMapOl.extend.MapBasics(map)
  67. document.getElementById('fixed').onclick = () => {
  68. mapTool.setFitView({features: points})
  69. }
  70. </script>
  71. </html>