Map.vue 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. // getGPSOfBDCountries();
  24. onMounted(() => {
  25. const map = new BMap.Map(el.value);
  26. const point = new BMap.Point(116.404, 39.915); // 天安门
  27. map.centerAndZoom(point, 3);
  28. mapRef.value = map;
  29. // 绑定事件
  30. map.addEventListener('click', (e) => {
  31. console.log('-----start-----')
  32. loading.value = true;
  33. const { point } = e;
  34. getCountry(point.lng, point.lat).then(x => {
  35. emit('click', x);
  36. loading.value = false;
  37. console.log('-----end-----')
  38. }).catch((err) => {
  39. console.error(err);
  40. loading.value = false;
  41. });
  42. });
  43. // 显示 marker
  44. const icon = new BMap.Icon('./images/logo.png', new BMap.Size(16, 16));
  45. mockCountries.forEach(country => {
  46. const target = countryGPS[country];
  47. if (!target) {
  48. console.error(`${country} 在世界GPS列表中不存在`)
  49. return;
  50. }
  51. const point = new BMap.Point(target[0], target[1]);
  52. const marker = new BMap.Marker(point, { icon });
  53. map.addOverlay(marker);
  54. marker.setAnimation(BMAP_ANIMATION_BOUNCE);
  55. // map.getOverlays().forEach((overlay) => {
  56. // console.log('------overlay-------', overlay)
  57. // overlay.setAnimation(BMAP_ANIMATION_BOUNCE);
  58. // });
  59. });
  60. // window._AMapSecurityConfig = {
  61. // securityJsCode:'f33684b9573195f9f91a4c8bc779d7e2',
  62. // }
  63. // AMapLoader.load({
  64. // key: "378f2af0c01b00ec919ace1699f2466f", // 申请好的Web端开发者Key,首次调用 load 时必填
  65. // version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  66. // plugins: [], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  67. // }).then((AMap)=>{
  68. // const map = new AMap.Map(el.value, {
  69. // mapStyle: 'amap://styles/bff59c60c62af794ee93f1befde78625',
  70. // zoom: 3,
  71. // });
  72. // const countries = Object.keys(countryGPS);
  73. // countries.forEach((country) => {
  74. // const position = countryGPS[country];
  75. // const found = mockCountries.filter(x => x===country)[0];
  76. // console.log('----position---->', position);
  77. // const marker = new AMap.Marker({
  78. // map,
  79. // offset:[-100, -16],
  80. // content: `
  81. // <div class="map-marker-box">
  82. // <div class="map-marker-title">${country}</div>
  83. // ${found ? '<div class="map-marker-icon"/>' : ''}
  84. // </div>
  85. // `,
  86. // position,
  87. // });
  88. // });
  89. // mockCountries.forEach(country => {
  90. // const target = countryGPS[country];
  91. // if (!target) {
  92. // console.error(`${country} 在世界GPS列表中不存在`)
  93. // return;
  94. // }
  95. // const marker = new AMap.Marker({
  96. // map,
  97. // offset:[-100, -16],
  98. // content: `
  99. // <div class="map-marker-box">
  100. // <div class="map-marker-title">${country}</div>
  101. // <div class="map-marker-icon"/>
  102. // </div>
  103. // `,
  104. // position: target,
  105. // });
  106. // });
  107. // }).catch(e => {
  108. // console.error(e);
  109. // })
  110. });
  111. </script>
  112. <style lang="less" scoped>
  113. .map-pg {
  114. width: 100%;
  115. height: 100%;
  116. }
  117. </style>