map.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <template>
  2. <div id="wrap">
  3. <div id="searchWrap">
  4. <!-- <div class="searchWrap">
  5. <input type="text" v-model="address" @input="search" /><button
  6. @click="search"
  7. >
  8. 搜索
  9. </button>
  10. </div> -->
  11. <div id="result" class="amap_lib_placeSearch" v-show="hide">
  12. <div
  13. class="amap_lib_placeSearch_list amap-pl-pc"
  14. v-for="(item, index) in poiArr"
  15. @click="openMarkerTipById(index, $event)"
  16. @mouseout="onmouseout_MarkerStyle(index + 1, $event)"
  17. :key="index"
  18. >
  19. <div class="poibox" style="border-bottom: 1px solid #eaeaea">
  20. <div
  21. class="amap_lib_placeSearch_poi poibox-icon"
  22. :class="index == selectedIndex ? 'selected' : ''"
  23. >
  24. {{ index + 1 }}
  25. </div>
  26. <div
  27. class="poi-img"
  28. v-if="item.url"
  29. :style="
  30. 'background-image:url(' +
  31. item.url +
  32. '?operate=merge&amp;w=90&amp;h=56&amp;position=5)'
  33. "
  34. ></div>
  35. <h3 class="poi-title">
  36. <span class="poi-name">{{ item.name }}</span>
  37. </h3>
  38. <div class="poi-info">
  39. <p class="poi-addr">地址:{{ item.address }}</p>
  40. <p class="poi-tel">电话:{{ item.tel }}</p>
  41. </div>
  42. <div class="clear"></div>
  43. </div>
  44. </div>
  45. </div>
  46. </div>
  47. <div id="iCenter"></div>
  48. <!-- <button class="btn" @click="fetAddressName">获取当前位置和名字</button> -->
  49. </div>
  50. </template>
  51. <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.14&key=5df9445f578191c7d4fd5f3ce6b8712c"></script>
  52. <script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
  53. <script>
  54. import AMapLoader from "@amap/amap-jsapi-loader";
  55. window._AMapSecurityConfig = {
  56. securityJsCode: "b5c2691e1ffcf58a735e502081ae781f",
  57. };
  58. export default {
  59. props: ["newAddress", "dataObj"], // 父组件传过来的地址和地址经纬度信息,
  60. data() {
  61. return {
  62. address: this.newAddress ? this.newAddress : "郑州", //保存地址的汉字名字
  63. map1: "",
  64. map: "", //保存地址的经纬度
  65. poiArr: [], //左边搜索出来的数组
  66. windowsArr: [], //信息窗口的数组
  67. marker: [],
  68. mapObj: "", //地图对象
  69. selectedIndex: -1,
  70. hide: true,
  71. clickType: 1,
  72. location: {
  73. P: this.dataObj.lat,
  74. Q: this.dataObj.lng,
  75. },
  76. aMap:null,
  77. addressData:{}
  78. };
  79. },
  80. mounted() {
  81. console.log(333, this.dataObj, this.location);
  82. this.mapInit();
  83. // this.placeSearch(this.address);
  84. },
  85. methods: {
  86. showToast(address) {
  87. // this.placeSearch(address.address);
  88. console.log(444, address);
  89. this.location.P = address.lat;
  90. this.location.Q = address.lng;
  91. this.address = address.address;
  92. let that = this;
  93. new AMap.InfoWindow({
  94. content: "<h3>" + "当前选中地址" + "</h3>" + that.address,
  95. size: new AMap.Size(300, 0),
  96. autoMove: true,
  97. offset: new AMap.Pixel(-4, -10),
  98. }).open(that.mapObj, that.location);
  99. },
  100. cancelSave() {
  101. eventBus.$emit("cancelSave");
  102. },
  103. saveAddress() {
  104. let addressName, location;
  105. if (this.clickType == 1) {
  106. let address = this.poiArr[this.selectedIndex];
  107. addressName = address.name + address.address;
  108. location = address.location;
  109. console.log(address.name + address.address, address.location);
  110. } else if (this.clickType == 2) {
  111. console.log(this.address, this.map);
  112. addressName = this.address;
  113. location = this.map;
  114. } else if (this.clickType == 3) {
  115. console.log(this.address, this.map1);
  116. addressName = this.address;
  117. location = this.map1;
  118. }
  119. eventBus.$emit("saveAddress", [addressName, location]);
  120. },
  121. // 经纬度转化为详细地址
  122. getAddress(lng,lat) {
  123. let that = this;
  124. AMap.plugin("AMap.Geocoder", function () {
  125. let geocoder = new AMap.Geocoder({
  126. radius: 100,
  127. extensions: "all",
  128. });
  129. geocoder.getAddress(
  130. // [that.map1.lng, that.map1.lat],
  131. [lng,lat],
  132. function (status, result) {
  133. if (status === "complete" && result.info === "OK") {
  134. let address = result.regeocode.formattedAddress;
  135. // console.log(result.regeocode);
  136. // that.address = result.regeocode.formattedAddress;
  137. that.addressData.addressName = result.regeocode.formattedAddress
  138. that.$emit('uploadAddress',that.addressData)
  139. // that.placeSearch(that.address)
  140. }
  141. }
  142. );
  143. });
  144. },
  145. // 地图点击事件
  146. // testevent() {
  147. // let that = this;
  148. // this.clickType = 3;
  149. // // var map=new AMap.Map('iCenter');//重新new出一个对象,传入参数是div的id
  150. // // console.log(this.mapEl);
  151. // this.mapEl.event.addListener(this.mapObj, "click", function (e) {
  152. // // this.aMap.event.addListener(this.mapObj, "click", function (e) {
  153. // //添加点击事件,传入对象名,事件名,回调函数
  154. // that.map1 = e.lnglat;
  155. // that.getAddress();
  156. // setTimeout(() => {
  157. // new AMap.InfoWindow({
  158. // content: "<h3>" + "当前选中地址" + "</h3>" + that.address,
  159. // size: new AMap.Size(300, 0),
  160. // autoMove: true,
  161. // offset: new AMap.Pixel(-4, -10),
  162. // }).open(that.mapObj, e.lnglat);
  163. // }, 100);
  164. // });
  165. // },
  166. //创建一个map
  167. mapInit() {
  168. AMapLoader.reset();
  169. AMapLoader.load({
  170. key: "5df9445f578191c7d4fd5f3ce6b8712c", // 申请好的Web端开发者Key,首次调用 load 时必填
  171. version: "2.0", // 指定要加载的 JSAPI 的版本,缺少时默认为 1.4.15
  172. plugins: [
  173. "AMap.AutoComplete",
  174. "AMap.Marker",
  175. "AMap.PlaceSearch",
  176. "AMap.Geolocation",
  177. ], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
  178. }).then((AMap) => {
  179. this.aMap = AMap;
  180. // 设置地图容器id
  181. this.mapEl = new AMap.Map("result", {
  182. resizeEnable: true,
  183. viewMode: "3D", // 是否为3D地图模式
  184. zoom: 12, // 初始化地图级别
  185. // center: [116.397428, 39.90923] // 初始化地图中心点位置 默认当前行政区
  186. });
  187. // this.autoComplete = new AMap.AutoComplete({
  188. // city: "全国",
  189. // });
  190. // if (this.info.lng && this.info.lat) {
  191. // this.addmark(this.currentAddr);
  192. // }
  193. this.mapEl.on("click", (e) => {
  194. this.addressData.latitude = e.lnglat.getLat()
  195. this.addressData.longitude = e.lnglat.getLng()
  196. this.getAddress(e.lnglat.getLng(), e.lnglat.getLat());
  197. });
  198. });
  199. // this.mapObj = new AMap.Map("iCenter", {
  200. // resizeEnable: true,
  201. // zoom: 10,
  202. // });
  203. // this.testevent();
  204. },
  205. //根据名字地址去搜索结果
  206. // placeSearch(name) {
  207. // let that = this;
  208. // this.hide = true;
  209. // var MSearch;
  210. // this.mapObj.plugin(
  211. // ["AMap.PlaceSearch", "AMap.ToolBar", "AMap.Scale"],
  212. // () => {
  213. // this.mapObj.addControl(new AMap.ToolBar());
  214. // this.mapObj.addControl(new AMap.Scale());
  215. // MSearch = new AMap.PlaceSearch({
  216. // //构造地点查询类
  217. // city: that.address, //城市
  218. // });
  219. // AMap.event.addListener(
  220. // MSearch,
  221. // "complete",
  222. // this.keywordSearch_CallBack
  223. // ); //返回地点查询结果
  224. // MSearch.search(name); //关键字查询
  225. // }
  226. // );
  227. // },
  228. //结果的回调
  229. keywordSearch_CallBack(data) {
  230. console.log(111, data);
  231. var poiArr = data.poiList.pois;
  232. var resultCount = poiArr.length;
  233. this.poiArr = poiArr; //左边要渲染的数据
  234. for (var i = 0; i < resultCount; i++) {
  235. this.addmarker(i, poiArr[i]);
  236. console.log(poiArr[i]);
  237. this.poiArr[i].url = this.poiArr[i].photos
  238. ? this.poiArr[i].photos[0]
  239. ? this.poiArr[i].photos[0].url
  240. : ""
  241. : "";
  242. }
  243. this.mapObj.setFitView();
  244. },
  245. //添加marker&infowindow
  246. addmarker(i, d) {
  247. var lngX = d.location.getLng();
  248. var latY = d.location.getLat();
  249. console.log(lngX, latY);
  250. var markerOption = {
  251. map: this.mapObj,
  252. position: new AMap.LngLat(lngX, latY),
  253. };
  254. var mar = new AMap.Marker(markerOption);
  255. this.marker.push(new AMap.LngLat(lngX, latY));
  256. var infoWindow = new AMap.InfoWindow({
  257. content:
  258. "<h3>" +
  259. "当前选中位置:" +
  260. d.name +
  261. "</h3>" +
  262. this.TipContents(d.name, d.address),
  263. size: new AMap.Size(300, 0),
  264. autoMove: true,
  265. offset: new AMap.Pixel(0, -30),
  266. });
  267. console.log();
  268. this.windowsArr.push(infoWindow);
  269. var _this = this;
  270. var aa = (e) => {
  271. this.clickType = 2;
  272. var obj = mar.getPosition();
  273. this.map = obj; //这里保存的地址经纬度
  274. this.address = d.name + d.address; //这里保存的是地址名字
  275. infoWindow.open(_this.mapObj, obj);
  276. };
  277. AMap.event.addListener(mar, "click", aa);
  278. },
  279. TipContents(name, address) {
  280. //窗体内容
  281. if (
  282. name == "" ||
  283. name == "undefined" ||
  284. name == null ||
  285. name == " undefined" ||
  286. typeof name == "undefined"
  287. ) {
  288. type = "暂无";
  289. }
  290. if (
  291. address == "" ||
  292. address == "undefined" ||
  293. address == null ||
  294. address == " undefined" ||
  295. typeof address == "undefined"
  296. ) {
  297. address = "暂无";
  298. }
  299. var str = `地址:${address}`;
  300. return str;
  301. },
  302. openMarkerTipById(pointid, event) {
  303. //根据id 打开搜索结果点tip
  304. this.clickType = 1;
  305. event.currentTarget.style.background = "#CAE1FF";
  306. this.selectedIndex = pointid;
  307. // this.map = this.marker[pointid]
  308. this.map1 = this.poiArr[pointid].location;
  309. console.log(222, this.mapObj, this.marker[pointid]);
  310. console.log(this.marker[pointid], this.poiArr[pointid]);
  311. this.address = this.poiArr[pointid].address + this.poiArr[pointid].name;
  312. this.windowsArr[pointid].open(this.mapObj, this.marker[pointid]);
  313. },
  314. onmouseout_MarkerStyle(pointid, event) {
  315. //鼠标移开后点样式恢复
  316. event.currentTarget.style.background = "";
  317. },
  318. search() {
  319. this.windowsArr = [];
  320. this.marker = [];
  321. this.mapObj = "";
  322. this.mapInit();
  323. // this.placeSearch(this.address);
  324. },
  325. },
  326. };
  327. </script>
  328. <style lang="scss">
  329. #wrap {
  330. width: 100%;
  331. display: flex;
  332. #iCenter {
  333. height: 400px;
  334. position: relative;
  335. display: flex;
  336. flex: 1;
  337. }
  338. #searchWrap {
  339. width: 100%;
  340. position: relative;
  341. height: 400px;
  342. .searchWrap {
  343. position: absolute;
  344. width: 100%;
  345. z-index: 9;
  346. display: flex;
  347. align-items: center;
  348. input {
  349. width: 260px;
  350. height: 24px;
  351. }
  352. button {
  353. width: 36px;
  354. height: 28px;
  355. }
  356. }
  357. #result {
  358. width: 100%;
  359. position: absolute;
  360. top: 30px;
  361. height: 100%;
  362. z-index: 8;
  363. overflow-y: scroll;
  364. border-right: 1px solid #ccc;
  365. }
  366. }
  367. .amap_lib_placeSearch {
  368. height: 100%;
  369. overflow-y: scroll;
  370. .poibox {
  371. border-bottom: 1px solid #eaeaea;
  372. cursor: pointer;
  373. padding: 5px 0 5px 10px;
  374. position: relative;
  375. min-height: 35px;
  376. .selected {
  377. background-image: url(https://webapi.amap.com/theme/v1.3/markers/n/mark_r.png) !important;
  378. }
  379. &:hover {
  380. background: #f6f6f6;
  381. }
  382. .poi-img {
  383. float: right;
  384. margin: 3px 8px 0;
  385. width: 90px;
  386. height: 56px;
  387. overflow: hidden;
  388. }
  389. .poi-title {
  390. margin-left: 25px;
  391. font-size: 13px;
  392. overflow: hidden;
  393. }
  394. .poi-info {
  395. word-break: break-all;
  396. margin: 0 0 0 25px;
  397. overflow: hidden;
  398. p {
  399. color: #999;
  400. font-family: Tahoma;
  401. line-height: 20px;
  402. font-size: 12px;
  403. }
  404. }
  405. .poibox-icon {
  406. margin-left: 7px;
  407. margin-top: 4px;
  408. }
  409. .amap_lib_placeSearch_poi {
  410. background: url(https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png)
  411. no-repeat;
  412. height: 31px;
  413. width: 19px;
  414. cursor: pointer;
  415. left: -1px;
  416. text-align: center;
  417. color: #fff;
  418. font: 12px arial, simsun, sans-serif;
  419. padding-top: 3px;
  420. position: absolute;
  421. }
  422. }
  423. }
  424. .btn {
  425. position: fixed;
  426. bottom: 20px;
  427. left: 50%;
  428. padding: 10px;
  429. }
  430. }
  431. </style>