map.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. <template>
  2. <el-dialog
  3. title="地图选点"
  4. :visible.sync="dialogVisible"
  5. width="1200px"
  6. :before-close="dialogVisible == false"
  7. :destroy-on-close="true"
  8. >
  9. <div>
  10. <el-input
  11. placeholder="请输入内容"
  12. v-model="input"
  13. class="input-with-select"
  14. id="input"
  15. style="width: 90%"
  16. >
  17. </el-input>
  18. <el-button
  19. type="primary"
  20. icon="el-icon-search"
  21. @click="autoInput"
  22. ></el-button>
  23. </div>
  24. <div style="margin-top: 10px">已选择地址:{{ address }}</div>
  25. <div id="container" style="margin-top: 10px"></div>
  26. <el-form
  27. label-width="120px"
  28. :model="form"
  29. :rules="rules"
  30. ref="ruleForm"
  31. style="margin-top: 30px"
  32. >
  33. <el-form-item label="详情地址" prop="addrDetail">
  34. <el-input v-model="form.addrDetail" style="width: 500px"></el-input>
  35. </el-form-item>
  36. <el-form-item label="仓库" prop="storeHouse">
  37. <el-select
  38. value-key="value"
  39. clearable
  40. v-model="form.storeHouseId"
  41. placeholder="请选择"
  42. @change="selectChange"
  43. style="width: 300px"
  44. >
  45. <el-option
  46. v-for="(item, index) in houseList"
  47. :label="item.name"
  48. :key="index"
  49. :value="item.id"
  50. />
  51. </el-select>
  52. </el-form-item>
  53. <el-form-item label="设为默认地址">
  54. <el-switch
  55. v-model="form.defaultTag"
  56. active-color="#13ce66"
  57. inactive-color="#ff4949"
  58. :active-value="1"
  59. :inactive-value="0"
  60. >
  61. </el-switch>
  62. </el-form-item>
  63. </el-form>
  64. <span slot="footer" class="dialog-footer">
  65. <el-button @click="closeMap">取 消</el-button>
  66. <el-button type="primary" @click="SubmitMapPoint">确 定</el-button>
  67. </span>
  68. </el-dialog>
  69. </template>
  70. <style scoped>
  71. #container {
  72. padding: 0px;
  73. margin: 0px;
  74. width: 100%;
  75. height: 500px;
  76. }
  77. </style>
  78. <script>
  79. import { getWarehouseInfo } from "@/api/manage/address";
  80. import AMapLoader from "@amap/amap-jsapi-loader";
  81. export default {
  82. data() {
  83. return {
  84. map: null,
  85. marker: null,
  86. geocoder: null,
  87. dialogVisible: false,
  88. input: "",
  89. markers: [],
  90. address: "",
  91. addressParams: {},
  92. form: {
  93. storeHouseId: "",
  94. },
  95. rules: {
  96. addrDetail: [
  97. { required: true, message: "请填写详细地址", trigger: "blur" },
  98. ],
  99. },
  100. houseList: [],
  101. };
  102. },
  103. mounted() {
  104. this.initAMap();
  105. },
  106. unmounted() {
  107. this.map?.destroy();
  108. },
  109. methods: {
  110. selectChange(val) {
  111. if (!val) this.form.storeHouse = "";
  112. },
  113. loadMap(data) {
  114. // 处理传入的数据
  115. if (data) {
  116. if (typeof data.cityCode === "number") {
  117. this.gethouse(data.cityCode);
  118. }
  119. this.form = {
  120. ...data,
  121. defaultTag: data.defaultTag ?? 0,
  122. storeHouseId: JSON.parse(data.storeHouse).storeHouseId,
  123. };
  124. console.log(JSON.parse(data.storeHouse));
  125. // 构建完整地址
  126. this.address = [
  127. data.provinceName,
  128. data.cityName,
  129. data.countyName,
  130. data.addr,
  131. ]
  132. .filter(Boolean)
  133. .join("");
  134. // 获取仓库信息
  135. } else {
  136. this.form = {};
  137. }
  138. this.dialogVisible = true;
  139. this.initAMap();
  140. },
  141. closeMap() {
  142. this.address = "";
  143. this.input = "";
  144. this.dialogVisible = false;
  145. this.form = {};
  146. this.map?.destroy();
  147. },
  148. SubmitMapPoint() {
  149. const selectedHouse = this.houseList.find(
  150. (item) => item.id === this.form.storeHouseId
  151. );
  152. if (selectedHouse) {
  153. let params = {
  154. name: selectedHouse.name || "",
  155. id: selectedHouse.id || "",
  156. };
  157. this.form.storeHouse = params;
  158. }
  159. if (!this.address) {
  160. this.$message.error("请先在地图选择省市区!");
  161. return;
  162. }
  163. this.$refs.ruleForm.validate((valid) => {
  164. if (valid) {
  165. let params = {
  166. map: this.addressParams,
  167. form: this.form,
  168. };
  169. this.$emit("address", params);
  170. } else {
  171. console.log("error submit!!");
  172. return false;
  173. }
  174. });
  175. },
  176. gethouse(cityCode) {
  177. getWarehouseInfo({ cityCode: cityCode }).then((res) => {
  178. if (res.code == 200) {
  179. this.houseList = res.data;
  180. }
  181. });
  182. },
  183. // 获取搜索信息
  184. autoInput() {
  185. let that = this;
  186. var keywords = this.input;
  187. if (this.markers.length > 0) {
  188. this.map.remove(this.markers);
  189. }
  190. AMap.plugin(["AMap.PlaceSearch"], function () {
  191. //构造地点查询类
  192. var placeSearch = new AMap.PlaceSearch({
  193. pageSize: 5, // 单页显示结果条数
  194. pageIndex: 1, // 页码
  195. map: that.map, // 展现结果的地图实例
  196. // panel: "panel", // 结果列表将在此容器中进行展示。
  197. autoFitView: true, // 是否自动调整地图视野使绘制的 Marker点都处于视口的可见范围
  198. });
  199. placeSearch.search(keywords);
  200. placeSearch.on("markerClick", function (e) {
  201. let obj = { ...e.data };
  202. obj.address =
  203. e.data.pname +
  204. "" +
  205. e.data.cityname +
  206. "" +
  207. e.data.adname +
  208. "" +
  209. e.data.address +
  210. "" +
  211. e.data.name;
  212. e.data.cityCode = obj.adcode.substring(0, 4) + "00";
  213. that.addressParams = obj;
  214. that.address = obj.address;
  215. that.gethouse(e.data.cityCode);
  216. });
  217. });
  218. },
  219. getLocation() {
  220. const _this = this;
  221. AMap.plugin("AMap.CitySearch", function () {
  222. var citySearch = new AMap.CitySearch();
  223. citySearch.getLocalCity(function (status, result) {
  224. if (status === "complete" && result.info === "OK") {
  225. console.log(result.rectangle.split(";"));
  226. let xy = result.rectangle.split(";")[0].split(",");
  227. console.log("xxx", _this.map.getCenter());
  228. _this.map.setCenter(xy);
  229. }
  230. });
  231. });
  232. },
  233. initAMap() {
  234. window._AMapSecurityConfig = {
  235. securityJsCode: "aae243a4d33170d5331abec91fe2cd0c",
  236. };
  237. let that = this;
  238. AMapLoader.load({
  239. key: "2e75f4c22217359628e22c2e170b4778", // 申请好的Web端开发者Key,首次调用 load 时必填
  240. version: "2.0", // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
  241. plugins: [
  242. "AMap.Geocoder",
  243. "AMap.Autocomplete",
  244. "AMap.PlaceSearch",
  245. "AMap.Scale",
  246. "AMap.OverView",
  247. "AMap.Geolocation",
  248. "AMap.ToolBar",
  249. "AMap.MapType",
  250. "AMap.PolyEditor",
  251. "AMap.CircleEditor",
  252. ], //需要使用的的插件列表,如比例尺'AMap.Scale',支持添加多个如:['...','...']
  253. })
  254. .then((AMap) => {
  255. that.map = new AMap.Map("container", {
  256. // 设置地图容器id
  257. viewMode: "2D", // 是否为3D地图模式
  258. zoom: 11, // 初始化地图级别.
  259. resizeEnable: true,
  260. });
  261. that.geocoder = new AMap.Geocoder();
  262. that.map.on("click", that.handleMapClick);
  263. setTimeout(() => {
  264. that.getLocation();
  265. }, 500);
  266. })
  267. .catch((e) => {
  268. console.log(e);
  269. });
  270. },
  271. handleMapClick(e) {
  272. if (this.marker) {
  273. if (this.marker) {
  274. this.marker.setMap(null);
  275. this.marker = null;
  276. }
  277. }
  278. if (!this.marker) {
  279. this.marker = new AMap.Marker({
  280. icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
  281. position: [e.lnglat.lng, e.lnglat.lat],
  282. imageSize: new AMap.Size(12, 12),
  283. offset: new AMap.Pixel(-13, -30),
  284. });
  285. this.marker.setMap(this.map);
  286. this.regeoCode(e.lnglat);
  287. }
  288. },
  289. markerBlur(e) {
  290. console.log(e);
  291. },
  292. regeoCode(lnglat) {
  293. let that = this;
  294. this.map.add(this.marker);
  295. this.marker.setPosition(lnglat);
  296. // 创建高德地图的 Geocoder 实例
  297. this.geocoder.getAddress(lnglat, function (status, result) {
  298. if (status === "complete" && result.regeocode) {
  299. var address = result.regeocode.formattedAddress;
  300. result.regeocode.addressComponent.location = lnglat;
  301. result.regeocode.addressComponent.address = address;
  302. let addrs = result.regeocode.addressComponent;
  303. result.regeocode.addressComponent.pname = addrs.province;
  304. result.regeocode.addressComponent.cityname = addrs.city;
  305. result.regeocode.addressComponent.adname = addrs.district;
  306. result.regeocode.addressComponent.pcode =
  307. result.regeocode.addressComponent.adcode.substring(0, 3) + "000";
  308. result.regeocode.addressComponent.cityCode =
  309. result.regeocode.addressComponent.adcode.substring(0, 4) + "00";
  310. that.gethouse(result.regeocode.addressComponent.cityCode);
  311. that.address = address;
  312. console.log("cccc", that.address);
  313. that.$forceUpdate();
  314. that.addressParams = result.regeocode.addressComponent;
  315. } else {
  316. that.$message.error("根据经纬度查询地址失败");
  317. }
  318. });
  319. },
  320. },
  321. };
  322. </script>
  323. <style>
  324. html,
  325. body,
  326. #container {
  327. height: 100%;
  328. width: 100%;
  329. }
  330. .amap-icon img,
  331. .amap-marker-content img {
  332. width: 25px;
  333. height: 34px;
  334. }
  335. .marker {
  336. position: absolute;
  337. top: -20px;
  338. right: -118px;
  339. color: #fff;
  340. padding: 4px 10px;
  341. box-shadow: 1px 1px 1px rgba(10, 10, 10, 0.2);
  342. white-space: nowrap;
  343. font-size: 12px;
  344. font-family: "";
  345. background-color: #25a5f7;
  346. border-radius: 3px;
  347. }
  348. .input-card {
  349. width: 18rem;
  350. z-index: 170;
  351. }
  352. .input-card .btn {
  353. margin-right: 0.8rem;
  354. }
  355. .input-card .btn:last-child {
  356. margin-right: 0;
  357. }
  358. </style>