1.创建引入脚本

  • 创建 utils文件夹,创建 remoteLoad.js
    ```javascript /**

    • @description:加载script脚本
    • @return: / export default function remoteLoad(url, hasCallback) { return createScript(url); /*

      • 创建script
      • @param url
      • @returns {Promise} */ function createScript(url) { let scriptElement = document.createElement(“script”); document.body.appendChild(scriptElement); let promise = new Promise((resolve, reject) => { scriptElement.addEventListener( “load”, (e) => {

        1. removeScript(scriptElement);
        2. if (!hasCallback) {
        3. resolve(e);
        4. }

        }, false );

        scriptElement.addEventListener( “error”, (e) => {

        1. removeScript(scriptElement);
        2. reject(e);

        }, false );

        if (hasCallback) { window.callback = function () {

        1. resolve();
        2. window.____callback____ = null;

        }; } });

      if (hasCallback) { url += “&callback=callback“; }

      scriptElement.src = url;

      return promise; }

    /**

    • 移除script标签
    • @param scriptElement script dom */ function removeScript(scriptElement) { document.body.removeChild(scriptElement); } }
  1. <a name="5mDfF"></a>
  2. ### 2.使用
  3. ```javascript
  4. <template>
  5. <div class="m-map">
  6. <div class="search" v-if="placeSearch">
  7. <input type="text" placeholder="请输入关键字" v-model="searchKey" />
  8. <button type="button" @click="handleSearch">搜索</button>
  9. <div
  10. id="js-result"
  11. v-show="searchKey"
  12. class="result"
  13. style="width: 100%; height: 100%;"
  14. ></div>
  15. </div>
  16. <div id="js-container" class="map">正在加载数据 ...</div>
  17. </div>
  18. </template>
  19. <script>
  20. import remoteLoad from "@/utils/remoteLoad.js";
  21. const MapKey = "cfd8da5cf010c5f7441834ff5e764f5b";
  22. const MapCityName = "郑州";
  23. export default {
  24. props: ["lat", "lng"],
  25. data() {
  26. return {
  27. searchKey: "",
  28. placeSearch: null,
  29. dragStatus: false,
  30. AMapUI: null,
  31. AMap: null,
  32. };
  33. },
  34. watch: {
  35. searchKey() {
  36. if (this.searchKey === "") {
  37. this.placeSearch.clear();
  38. }
  39. },
  40. },
  41. methods: {
  42. // 搜索
  43. handleSearch() {
  44. if (this.searchKey) {
  45. this.placeSearch.search(this.searchKey);
  46. }
  47. },
  48. // 实例化地图
  49. initMap() {
  50. // 加载PositionPicker,loadUI的路径参数为模块名中 'ui/' 之后的部分
  51. let AMapUI = (this.AMapUI = window.AMapUI);
  52. let AMap = (this.AMap = window.AMap);
  53. AMapUI.loadUI(["misc/PositionPicker"], (PositionPicker) => {
  54. let mapConfig = {
  55. zoom: 16,
  56. cityName: MapCityName,
  57. };
  58. if (this.lat && this.lng) {
  59. mapConfig.center = [this.lng, this.lat];
  60. }
  61. let map = new AMap.Map("js-container", mapConfig);
  62. // 加载地图搜索插件
  63. AMap.service("AMap.PlaceSearch", () => {
  64. this.placeSearch = new AMap.PlaceSearch({
  65. pageSize: 5,
  66. pageIndex: 1,
  67. citylimit: true,
  68. city: MapCityName,
  69. map: map,
  70. panel: "js-result",
  71. });
  72. });
  73. // 启用工具条
  74. AMap.plugin(["AMap.ToolBar"], function () {
  75. map.addControl(
  76. new AMap.ToolBar({
  77. position: "RB",
  78. })
  79. );
  80. });
  81. // 创建地图拖拽
  82. let positionPicker = new PositionPicker({
  83. mode: "dragMap", // 设定为拖拽地图模式,可选'dragMap'、'dragMarker',默认为'dragMap'
  84. map: map, // 依赖地图对象
  85. });
  86. // 拖拽完成发送自定义 drag 事件
  87. positionPicker.on("success", (positionResult) => {
  88. // 过滤掉初始化地图后的第一次默认拖放
  89. if (!this.dragStatus) {
  90. this.dragStatus = true;
  91. } else {
  92. this.$emit("drag", positionResult);
  93. }
  94. });
  95. // 启动拖放
  96. positionPicker.start();
  97. });
  98. },
  99. },
  100. async mounted() {
  101. // 已载入高德地图API,则直接初始化地图
  102. if (window.AMap && window.AMapUI) {
  103. this.initMap();
  104. // 未载入高德地图API,则先载入API再初始化
  105. } else {
  106. await remoteLoad(`http://webapi.amap.com/maps?v=1.3&key=${MapKey}`);
  107. await remoteLoad("http://webapi.amap.com/ui/1.0/main.js");
  108. this.initMap();
  109. }
  110. },
  111. };
  112. </script>
  113. <style lang="css">
  114. .m-map {
  115. height: 800px;
  116. height: 800px;
  117. min-width: 500px;
  118. min-height: 300px;
  119. position: relative;
  120. }
  121. .m-map .map {
  122. width: 100%;
  123. height: 100%;
  124. }
  125. .m-map .search {
  126. position: absolute;
  127. top: 10px;
  128. left: 10px;
  129. width: 285px;
  130. z-index: 1;
  131. }
  132. .m-map .search input {
  133. width: 180px;
  134. border: 1px solid #ccc;
  135. line-height: 20px;
  136. padding: 5px;
  137. outline: none;
  138. }
  139. .m-map .search button {
  140. line-height: 26px;
  141. background: #fff;
  142. border: 1px solid #ccc;
  143. width: 50px;
  144. text-align: center;
  145. }
  146. .m-map .result {
  147. max-height: 300px;
  148. overflow: auto;
  149. margin-top: 10px;
  150. }
  151. </style>