MapAttr.tsx 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // import AMapLoader from "@amap/amap-jsapi-loader";
  2. import { Input } from "ant-design-vue";
  3. import { defineComponent, onMounted, reactive, ref } from "vue";
  4. declare const AMap: any;
  5. export default defineComponent({
  6. emits: ["change"],
  7. setup(props, { emit }) {
  8. // const AMap = ref(AMap);
  9. const map = ref();
  10. const marker = ref();
  11. const geocoder = ref();
  12. const autoComplete = ref();
  13. const state = reactive({
  14. address: "天安门",
  15. lnglat: [116.397428, 39.90923],
  16. });
  17. function clickMap(e: any) {
  18. state.lnglat = [e.lnglat.getLng(), e.lnglat.getLat()];
  19. updateMarker();
  20. regeoCode();
  21. }
  22. function updateMarker() {
  23. marker.value.setPosition(state.lnglat);
  24. map.value.add(marker.value);
  25. map.value.setFitView(marker.value);
  26. }
  27. function geoCode() {
  28. geocoder.value.getLocation(
  29. state.address,
  30. (status: string, result: any) => {
  31. if (status === "complete" && result.geocodes.length) {
  32. const lnglat = result.geocodes[0].location;
  33. state.lnglat = lnglat;
  34. updateMarker();
  35. } else {
  36. console.error("根据地址查询位置失败");
  37. }
  38. }
  39. );
  40. }
  41. function regeoCode() {
  42. geocoder.value.getAddress(
  43. state.lnglat,
  44. function (status: any, result: any) {
  45. if (status === "complete" && result.regeocode) {
  46. const address = result.regeocode.formattedAddress;
  47. state.address = address;
  48. } else {
  49. console.error("根据经纬度查询地址失败");
  50. }
  51. }
  52. );
  53. }
  54. const searchAddress = () => {
  55. autoComplete.value.search(
  56. state.address,
  57. function (status: any, result: any) {
  58. console.error("result: ", result);
  59. // 搜索成功时,result即是对应的匹配数据
  60. }
  61. );
  62. };
  63. const initMap = () => {
  64. map.value = new AMap.Map("container", {
  65. //设置地图容器id
  66. viewMode: "3D", //是否为3D地图模式
  67. zoom: 11, //初始化地图级别
  68. center: state.lnglat, //初始化地图中心点位置
  69. });
  70. // AMapLoader.load({
  71. // key: "e8d3513c8551b20ec90b991249fa59a1", // 申请好的Web端开发者Key,首次调用 load 时必填
  72. // version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  73. // plugins: ["AMap.Scale", "AMap.ToolBar", "AMap.MouseTool"], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  74. // Loca: {
  75. // version: "2.0.0",
  76. // },
  77. // })
  78. // .then((res) => {
  79. // AMap.plugin(["AMap.Geocoder", "AMap.AutoComplete"], () => {
  80. // 使用geocoder做地理/逆地理编码
  81. geocoder.value = new AMap.Geocoder();
  82. autoComplete.value = new AMap.AutoComplete({ city: "全国" });
  83. // });
  84. marker.value = new AMap.Marker({
  85. position: state.lnglat, //位置
  86. });
  87. map.value.add(marker.value); //添加到地图
  88. map.value.on("click", clickMap);
  89. // })
  90. // .catch((e) => {
  91. // console.log("error", e);
  92. // });
  93. };
  94. onMounted(() => {
  95. initMap();
  96. });
  97. return () => {
  98. return (
  99. <div class="w-1/1">
  100. <Input.Search
  101. value={state.address}
  102. onChange={(e) => (state.address = e.target.value || "")}
  103. placeholder="请输入地名"
  104. class="w-1/1"
  105. onSearch={searchAddress}
  106. />
  107. <div id="container" class="w-1/1 h-200px mt-10px"></div>
  108. </div>
  109. );
  110. };
  111. },
  112. });