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) => {
removeScript(scriptElement);if (!hasCallback) {resolve(e);}
}, false );
scriptElement.addEventListener( “error”, (e) => {
removeScript(scriptElement);reject(e);
}, false );
if (hasCallback) { window.callback = function () {
resolve();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); } }
<a name="5mDfF"></a>### 2.使用```javascript<template><div class="m-map"><div class="search" v-if="placeSearch"><input type="text" placeholder="请输入关键字" v-model="searchKey" /><button type="button" @click="handleSearch">搜索</button><divid="js-result"v-show="searchKey"class="result"style="width: 100%; height: 100%;"></div></div><div id="js-container" class="map">正在加载数据 ...</div></div></template><script>import remoteLoad from "@/utils/remoteLoad.js";const MapKey = "cfd8da5cf010c5f7441834ff5e764f5b";const MapCityName = "郑州";export default {props: ["lat", "lng"],data() {return {searchKey: "",placeSearch: null,dragStatus: false,AMapUI: null,AMap: null,};},watch: {searchKey() {if (this.searchKey === "") {this.placeSearch.clear();}},},methods: {// 搜索handleSearch() {if (this.searchKey) {this.placeSearch.search(this.searchKey);}},// 实例化地图initMap() {// 加载PositionPicker,loadUI的路径参数为模块名中 'ui/' 之后的部分let AMapUI = (this.AMapUI = window.AMapUI);let AMap = (this.AMap = window.AMap);AMapUI.loadUI(["misc/PositionPicker"], (PositionPicker) => {let mapConfig = {zoom: 16,cityName: MapCityName,};if (this.lat && this.lng) {mapConfig.center = [this.lng, this.lat];}let map = new AMap.Map("js-container", mapConfig);// 加载地图搜索插件AMap.service("AMap.PlaceSearch", () => {this.placeSearch = new AMap.PlaceSearch({pageSize: 5,pageIndex: 1,citylimit: true,city: MapCityName,map: map,panel: "js-result",});});// 启用工具条AMap.plugin(["AMap.ToolBar"], function () {map.addControl(new AMap.ToolBar({position: "RB",}));});// 创建地图拖拽let positionPicker = new PositionPicker({mode: "dragMap", // 设定为拖拽地图模式,可选'dragMap'、'dragMarker',默认为'dragMap'map: map, // 依赖地图对象});// 拖拽完成发送自定义 drag 事件positionPicker.on("success", (positionResult) => {// 过滤掉初始化地图后的第一次默认拖放if (!this.dragStatus) {this.dragStatus = true;} else {this.$emit("drag", positionResult);}});// 启动拖放positionPicker.start();});},},async mounted() {// 已载入高德地图API,则直接初始化地图if (window.AMap && window.AMapUI) {this.initMap();// 未载入高德地图API,则先载入API再初始化} else {await remoteLoad(`http://webapi.amap.com/maps?v=1.3&key=${MapKey}`);await remoteLoad("http://webapi.amap.com/ui/1.0/main.js");this.initMap();}},};</script><style lang="css">.m-map {height: 800px;height: 800px;min-width: 500px;min-height: 300px;position: relative;}.m-map .map {width: 100%;height: 100%;}.m-map .search {position: absolute;top: 10px;left: 10px;width: 285px;z-index: 1;}.m-map .search input {width: 180px;border: 1px solid #ccc;line-height: 20px;padding: 5px;outline: none;}.m-map .search button {line-height: 26px;background: #fff;border: 1px solid #ccc;width: 50px;text-align: center;}.m-map .result {max-height: 300px;overflow: auto;margin-top: 10px;}</style>
