component.tsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { IconAirPlain } from "@/assets/icons";
  2. import { useEditor } from "@/modules/editor";
  3. import { defineComponent, onMounted, ref, watch } from "vue";
  4. import { string } from "vue-types";
  5. import { useCompData } from ".";
  6. import { View } from "../View";
  7. // import AMapLoader from "@amap/amap-jsapi-loader";
  8. declare const AMap: any;
  9. export const Component = defineComponent({
  10. props: {
  11. compId: string().isRequired,
  12. },
  13. setup(props) {
  14. const { controls, store } = useEditor();
  15. const mapContainerRef = ref();
  16. const comp = useCompData(props.compId);
  17. const { value } = comp;
  18. // const AMap = ref();
  19. const map = ref();
  20. const marker = ref();
  21. function initMap() {
  22. // AMapLoader.load({
  23. // key: "389e81c4c61fd6dff4ab8f742af82e5f", // 申请好的Web端开发者Key,首次调用 load 时必填
  24. // version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  25. // plugins: ["AMap.Scale", "AMap.ToolBar", "AMap.MouseTool"], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  26. // Loca: {
  27. // version: "2.0.0",
  28. // },
  29. // })
  30. // .then((res) => {
  31. // AMap.value = res;
  32. map.value = new AMap.Map(mapContainerRef.value, {
  33. // viewMode: "3D",
  34. zoom: 11,
  35. center: value.addressInfo.lnglat,
  36. });
  37. AMap.plugin(["AMap.ToolBar"], () => {
  38. marker.value = new AMap.Marker({
  39. position: value.addressInfo.lnglat,
  40. });
  41. map.value.add(marker.value);
  42. const toolbar = new AMap.ToolBar({ position: "LT" }); // 缩放工具条实例化
  43. map.value.addControl(toolbar);
  44. });
  45. // })
  46. // .catch((e: any) => {
  47. // console.log("error", e);
  48. // });
  49. }
  50. function updateMap() {
  51. marker.value.setPosition(value.addressInfo.lnglat);
  52. map.value.add(marker.value);
  53. map.value.setZoomAndCenter(11, value.addressInfo.lnglat);
  54. }
  55. function openMap() {
  56. if (store.isEditMode) return;
  57. const options = {
  58. latitude: value.addressInfo.lnglat[1], // 纬度,浮点数,范围为90 ~ -90
  59. longitude: value.addressInfo.lnglat[0], // 经度,浮点数,范围为180 ~ -180。
  60. name: value.addressInfo.address, // 位置名
  61. address: value.addressInfo.address, // 地址详情说明
  62. };
  63. controls.wxCtrl.openMap(options);
  64. }
  65. watch(
  66. () => [value.addressInfo],
  67. () => updateMap()
  68. );
  69. onMounted(() => {
  70. initMap();
  71. });
  72. return () => (
  73. <View compId={props.compId}>
  74. <div
  75. ref={mapContainerRef}
  76. class="w-1/1"
  77. style={{
  78. height: "calc(100% - 44px)",
  79. }}
  80. ></div>
  81. <div
  82. class="absolute right-20px bottom-50px rounded-1/2 w-40px h-40px flex items-center justify-center bg-[#0085fe]"
  83. onClick={openMap}
  84. >
  85. <IconAirPlain class="text-20px" />
  86. </div>
  87. <div class="text-14px text-[#666] py-15px text-center bg-light-50">
  88. {value.addressInfo.address}
  89. </div>
  90. </View>
  91. );
  92. },
  93. });