map.vue 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <template>
  2. <div class="home">
  3. <div id="map" class="map-home" />
  4. </div>
  5. </template>
  6. <script>
  7. import { Map, View } from 'ol'
  8. import * as olProj from 'ol/proj'
  9. import TileLayer from 'ol/layer/Tile'
  10. import XYZ from 'ol/source/XYZ'
  11. import { Vector as VectorLayer } from 'ol/layer'
  12. import { Vector as VectorSource } from 'ol/source'
  13. export default {
  14. name: 'Home',
  15. components: {},
  16. data() {
  17. return {
  18. openMap: null
  19. }
  20. },
  21. mounted() {
  22. this.initMap()
  23. this.setMarker()
  24. },
  25. methods: {
  26. initMap() {
  27. this.openMap = new Map({
  28. target: 'map',
  29. layers: [
  30. new TileLayer({
  31. source: new XYZ({
  32. url: 'https://wprd0{1-4}.is.autonavi.com/appmaptile?lang=zh_cn&size=1&style=7&x={x}&y={y}&z={z}'
  33. })
  34. })
  35. ],
  36. view: new View({
  37. // 将西安作为地图中心
  38. center: olProj.fromLonLat([108.945951, 34.465262]),
  39. zoom: 1
  40. }),
  41. controls: []
  42. })
  43. },
  44. setMarker() {
  45. const _style = new Style({
  46. image: new sCircle({
  47. radius: 10,
  48. stroke: new Stroke({
  49. color: '#fff'
  50. }),
  51. fill: new Fill({
  52. color: '#3399CC'
  53. })
  54. })
  55. })
  56. const _feature = new Feature({
  57. geometry: new Point(olProj.fromLonLat([108.945951, 34.465262]))
  58. })
  59. _feature.setStyle(_style)
  60. const _marker = new VectorLayer({
  61. source: new VectorSource({
  62. features: [_feature]
  63. })
  64. })
  65. this.openMap.addLayer(_marker)
  66. }
  67. }
  68. }
  69. </script>
  70. <style lang="scss" scoped>
  71. .map-home {
  72. width: 100vw;
  73. height: 100vh;
  74. }
  75. </style>