image.png

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
    6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
    7. <title>Document</title>
    8. <script src="./dist/include-openlayers-local.js"></script>
    9. <script src='https://lib.baomitu.com/jquery/2.2.4/jquery.js'></script>
    10. <style>
    11. * {
    12. margin: 0;
    13. padding: 0
    14. }
    15. #map {
    16. position: absolute;
    17. top: 0;
    18. bottom: 0;
    19. z-index: 100;
    20. width: 100%;
    21. }
    22. .btn{
    23. position: absolute;
    24. z-index: 200;
    25. left:50px;
    26. }
    27. </style>
    28. </head>
    29. <body>
    30. <div class="btn">
    31. <button>显示矢量图</button>
    32. <button>显示卫星地球</button>
    33. </div>
    34. <div id="map">
    35. </div>
    36. <script>
    37. var gaodeMapLayer = new ol.layer.Tile({
    38. title: "高德地图",
    39. source: new ol.source.XYZ({
    40. url: 'http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}',
    41. wrapX: false
    42. })
    43. });
    44. var image = new ol.layer.Tile({
    45. title: "高德地图",
    46. source: new ol.source.XYZ({
    47. url: "http://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=6&x={x}&y={y}&z={z}",
    48. wrapX: false
    49. })
    50. })
    51. const map = new ol.Map({
    52. target: "map",
    53. layers: [gaodeMapLayer, image],
    54. view: new ol.View({
    55. center: [114, 30],
    56. zoom: 4,
    57. projection: "EPSG:4326"
    58. })
    59. })
    60. $(".btn button").click(function(){
    61. /* 点击对应的按钮让对应的图层显示 */
    62. const index = $(this).index();
    63. const layers = map.getLayers().getArray();
    64. layers.forEach((item,i)=>{
    65. if(i==index){
    66. item.setVisible(true)
    67. }else{
    68. item.setVisible(false)
    69. }
    70. })
    71. })
    72. </script>
    73. </body>
    74. </html>
    1. <script src="https://lib.baomitu.com/jquery/3.6.0/jquery.js"></script>

    参考代码