1. <body>
    2. <div id="map_container">
    3. </div>
    4. <script>
    5. /* 一、加载底图 */
    6. const map = new ol.Map({
    7. target: 'map_container',
    8. layers: [TianDiMap_vec, TianDiMap_cva],
    9. view: new ol.View({
    10. projection: 'EPSG:4326', // 经纬度坐标系(默认是默卡托坐标系)
    11. center: [0, 0],
    12. zoom: 2
    13. })
    14. })
    15. // 二. 通过Style类创建txt
    16. const icon_text = new ol.style.Style({
    17. text: new ol.style.Text({
    18. offsetX: 0,
    19. offsetY: 10,
    20. font: 'normal 12px 微软雅黑',
    21. text: '武汉市',
    22. fill: new ol.style.Fill({
    23. color: '#333'
    24. }),
    25. stroke: new ol.style.Stroke({
    26. color: "#ff2d51",
    27. width: 2
    28. })
    29. }),
    30. image: new ol.style.Icon({
    31. anchor: [0.5, 60],
    32. anchorOrigin: 'top-right',
    33. anchorXUnits: 'fraction',
    34. anchorYUnits: 'pixels',
    35. offsetOrigin: 'top-right',
    36. // offset:[0,10],
    37. //图标缩放比例
    38. scale: 0.5,
    39. //透明度
    40. opacity: 0.75,
    41. //图标的url
    42. src: '../image/location.png',
    43. })
    44. })
    45. // 三. 创建点要素, 设置点要素样式
    46. const point = new ol.Feature({
    47. geometry: new ol.geom.Point([114, 36]),
    48. })
    49. point.setStyle(icon_text)
    50. /* 四、创建矢量数据 */
    51. const source = new ol.source.Vector({
    52. features: [point],
    53. wrapX: false
    54. })
    55. /* 5、创建矢量图层 */
    56. const vector = new ol.layer.Vector({
    57. source: source
    58. })
    59. map.addLayer(vector)
    60. </script>
    61. </body>