Map.vue 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <template>
  2. <div class="map-pg" ref="el"></div>
  3. <PageLoading :loading="loading" />
  4. </template>
  5. <script setup>
  6. import { onMounted, ref, watch } from 'vue';
  7. import PageLoading from '@/components/PageLoading.vue';
  8. import countryGPS from '@/utils/country_gps';
  9. import { getCountry } from '@/utils/maputil';
  10. const emit = defineEmits(['click']);
  11. const el = ref();
  12. const mapRef = ref();
  13. const loading = ref(false);
  14. // 展示部分国家
  15. const mockCountries = [
  16. '中国',
  17. '芬兰',
  18. '土耳其',
  19. '德国',
  20. '英国',
  21. '瑞士',
  22. '美国',
  23. '加拿大',
  24. '日本',
  25. '韩国',
  26. '马来西亚',
  27. '澳大利亚',
  28. '新西兰',
  29. '新加坡',
  30. ];
  31. // getGPSOfBDCountries();
  32. onMounted(() => {
  33. const map = new BMap.Map(el.value);
  34. const point = new BMap.Point(116.404, 39.915); // 天安门
  35. map.centerAndZoom(point, 3);
  36. mapRef.value = map;
  37. // 绑定事件
  38. map.addEventListener('click', (e) => {
  39. console.log('-----start-----')
  40. loading.value = true;
  41. const { point } = e;
  42. getCountry(point.lng, point.lat).then(x => {
  43. emit('click', x);
  44. loading.value = false;
  45. console.log('-----end-----')
  46. }).catch((err) => {
  47. console.error(err);
  48. loading.value = false;
  49. });
  50. });
  51. // 显示 marker
  52. const icon = new BMap.Icon('./images/logo.png', new BMap.Size(16, 16));
  53. mockCountries.forEach(country => {
  54. const target = countryGPS[country];
  55. if (!target) {
  56. console.error(`${country} 在世界GPS列表中不存在`)
  57. return;
  58. }
  59. const point = new BMap.Point(target[0], target[1]);
  60. const marker = new BMap.Marker(point, { icon });
  61. map.addOverlay(marker);
  62. marker.setAnimation(BMAP_ANIMATION_BOUNCE);
  63. // map.getOverlays().forEach((overlay) => {
  64. // console.log('------overlay-------', overlay)
  65. // overlay.setAnimation(BMAP_ANIMATION_BOUNCE);
  66. // });
  67. });
  68. // window._AMapSecurityConfig = {
  69. // securityJsCode:'f33684b9573195f9f91a4c8bc779d7e2',
  70. // }
  71. // AMapLoader.load({
  72. // key: "378f2af0c01b00ec919ace1699f2466f", // 申请好的Web端开发者Key,首次调用 load 时必填
  73. // version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  74. // plugins: [], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  75. // }).then((AMap)=>{
  76. // const map = new AMap.Map(el.value, {
  77. // mapStyle: 'amap://styles/bff59c60c62af794ee93f1befde78625',
  78. // zoom: 3,
  79. // });
  80. // const countries = Object.keys(countryGPS);
  81. // countries.forEach((country) => {
  82. // const position = countryGPS[country];
  83. // const found = mockCountries.filter(x => x===country)[0];
  84. // console.log('----position---->', position);
  85. // const marker = new AMap.Marker({
  86. // map,
  87. // offset:[-100, -16],
  88. // content: `
  89. // <div class="map-marker-box">
  90. // <div class="map-marker-title">${country}</div>
  91. // ${found ? '<div class="map-marker-icon"/>' : ''}
  92. // </div>
  93. // `,
  94. // position,
  95. // });
  96. // });
  97. // mockCountries.forEach(country => {
  98. // const target = countryGPS[country];
  99. // if (!target) {
  100. // console.error(`${country} 在世界GPS列表中不存在`)
  101. // return;
  102. // }
  103. // const marker = new AMap.Marker({
  104. // map,
  105. // offset:[-100, -16],
  106. // content: `
  107. // <div class="map-marker-box">
  108. // <div class="map-marker-title">${country}</div>
  109. // <div class="map-marker-icon"/>
  110. // </div>
  111. // `,
  112. // position: target,
  113. // });
  114. // });
  115. // }).catch(e => {
  116. // console.error(e);
  117. // })
  118. });
  119. </script>
  120. <style lang="less" scoped>
  121. .map-pg {
  122. width: 100%;
  123. height: 100%;
  124. }
  125. </style>