map.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. <template>
  2. <view class="content">
  3. <view id="amap" class="amap" style="width: 100%; height: 59vh;" :pointList="pointList"
  4. :change:pointList="ModuleMap.setParkList">
  5. </view>
  6. <view class="btns">
  7. <view @click="back">取消</view>
  8. <view @click="checkAdd">确定</view>
  9. </view>
  10. <view class="inputCon">
  11. <view class="searchView">
  12. <text class="iconfont icon-sousuo"></text>
  13. <input type="text" placeholder="搜索地点" v-model="searchWords" confirm-type="search" @confirm="searchFn" />
  14. <text @click="cancel">取消</text>
  15. </view>
  16. </view>
  17. <view class="list">
  18. <view class="item" v-for="(add,index) in dataTips" :key="add.id" @click="select(add,index)"
  19. :class="selectIndex==index?'active':''">
  20. <view class="name">{{add.name}}</view>
  21. <view class="address">{{add.district || ''}}{{add.address || ''}}</view>
  22. </view>
  23. </view>
  24. <!-- <view style="position: absolute;top:30upx;width: 100%;">
  25. <view class="map-min-2-search-box">
  26. <input
  27. v-model="address"
  28. placeholder="搜索地点"
  29. class="locationpicker-search-content-ipt"
  30. />
  31. <view class="locationpicker-search-clear" @click="onClearInput">
  32. <image
  33. src="https://lbs.gtimg.com/visual/miniprogram-plugin/location-picker/assets/btn_close@2x.png"
  34. class="locationpicker-search-clear-ipt"
  35. mode=""
  36. ></image>
  37. </view>
  38. <view class="locationpicker-search--content-btn-line"></view>
  39. <view class="locationpicker-search-content-btn" @click="searchAddress">搜索</view>
  40. <button class="cu-btn bg-blue" @click="surePoint">确认</button>
  41. </view>
  42. </view> -->
  43. </view>
  44. </template>
  45. <script>
  46. var amapFile = require('@/static/map/amap-wx.130.js'); //如:..­/..­/libs/amap-wx.js
  47. // 创建地图
  48. var myAmapFun = new amapFile.AMapWX({
  49. key: '915d1d1ed40dc41450eda1d930ea1b2d'
  50. });
  51. export default {
  52. data() {
  53. return {
  54. markers: [],
  55. longitude: '',
  56. latitude: '',
  57. pointList: null,
  58. address: '',
  59. selectIndex: undefined,
  60. selectAddr: {},
  61. searchWords: "",
  62. id: 0, // 使用 marker点击事件 需要填写id
  63. title: 'map',
  64. // latitude: 39.909,
  65. // longitude: 116.39742,
  66. dataTips: [],
  67. map: null,
  68. centerArr: [],
  69. city:'',
  70. currentAddress:{}
  71. };
  72. },
  73. onShow() {
  74. },
  75. methods: {
  76. checkAdd() {
  77. console.log(this.markers);
  78. uni.setStorageSync('address', this.markers[0]);
  79. uni.setStorageSync('currentAddress', this.currentAddress);
  80. // var pages = getCurrentPages();// 获取所有的页面栈
  81. // var prevPage = pages[pages.length - 2]; // 找到上一个页面,注意是页面,如果是页面中有组件,则需要通过页面接受到数据后,再次往组件中传递
  82. // prevPage.$vm.addressObj = this.selectAddr;//在上一个页面中就可以用addressObj进行接收
  83. // uni.navigateBack();
  84. this.back()
  85. },
  86. back() {
  87. // 返回上一页
  88. uni.navigateBack({
  89. delta: 1 // 返回的页面数,默认为1
  90. })
  91. },
  92. cancel() {
  93. if (this.searchWords) {
  94. this.searchWords = "";
  95. myAmapFun.getPoiAround({
  96. location: this.markers[0].longitude + ',' + this.markers[0].latitude,
  97. success: (data) => {
  98. console.log("获取当前的列表", data);
  99. this.dataTips = data.poisData;
  100. },
  101. fail: (info) => {
  102. console.log(info)
  103. }
  104. })
  105. }
  106. },
  107. reserGeo() {
  108. myAmapFun.getRegeo({
  109. success: (data) => {
  110. console.log("获取当前定位信息", data);
  111. },
  112. fail: (info) => {
  113. console.log(info)
  114. }
  115. })
  116. },
  117. // 根据地址列表中选择某一个地点
  118. select(add, index) {
  119. console.log(add, index);
  120. this.currentAddress = add
  121. if (!add) {
  122. return;
  123. }
  124. this.selectIndex = index;
  125. var location = add.location.split(",");
  126. this.selectAddr = {
  127. address: add.pname ? (add.pname + add.cityname + add.adname + add.address) : (add.district + add.address),
  128. latitude: location[1],
  129. longitude: location[0]
  130. };
  131. this.markers = [{
  132. latitude: location[1],
  133. longitude: location[0]
  134. }]
  135. // this.markers[0].latitude = +location[1];
  136. // this.markers[0].longitude = +location[0];
  137. console.log(this.markers[0]);
  138. },
  139. // 根据内容进行检索
  140. searchFn() {
  141. console.log("根据地址检索", this.searchWords);
  142. myAmapFun.getInputtips({
  143. keywords: this.searchWords,
  144. location: '',
  145. city:this.city || '太原' ,
  146. success: (data) => {
  147. console.log(111, data);
  148. if (data && data.tips) {
  149. this.dataTips = data.tips;
  150. }
  151. },
  152. fail: data => {
  153. console.log(222, data);
  154. }
  155. })
  156. },
  157. //标记点触发方法
  158. markerClick(e) {
  159. this.longitude = e.split(':')[0]
  160. this.latitude = e.split(':')[1]
  161. this.markers = [{
  162. latitude: this.latitude,
  163. longitude: this.longitude
  164. }]
  165. },
  166. },
  167. onLoad(e) {
  168. // myAmapFun.getRegeo({
  169. // success: (data)=>{
  170. // console.log("获取当前定位信息",data[0][longitude]);
  171. // // this.centerArr=[data[0].longitude,data[0].latitude]
  172. // },
  173. // fail: (info)=>{
  174. // console.log(info)
  175. // }
  176. // })
  177. if (e) {
  178. console.log(e);
  179. let point = JSON.parse(decodeURIComponent(e.position))
  180. // point.iconPath = '/static/avatar_boy.png';
  181. this.markers = [point]
  182. // 传入的标点(反显用)
  183. this.longitude = point.longitude
  184. this.latitude = point.latitude
  185. }
  186. },
  187. mounted() {
  188. // 将传入的标点传入下面的 ModuleMap 中
  189. this.pointList = [this.longitude, this.latitude];
  190. },
  191. }
  192. </script>
  193. <script module="ModuleMap" lang="renderjs">
  194. export default {
  195. data() {
  196. return {
  197. map: null,
  198. layer: null,
  199. markers: '',
  200. isExistImplement: false,
  201. markerId: '',
  202. parkList: [],
  203. }
  204. },
  205. watch: {
  206. //监听位置数据的变化,并初始化地图(有位置数据则默认给个标点)
  207. parkList(v) {
  208. setTimeout(() => {
  209. if (window.AMap) {
  210. this.initAmap(v);
  211. } else {
  212. const script = document.createElement('script');
  213. script.src =
  214. 'https://webapi.amap.com/maps?v=1.4.15&key=6b8b0fd9f75da24fb1c13edf7b2d7daa'; //这里是自己申请的高德的key
  215. script.onload = () => {
  216. this.initAmap(v);
  217. }
  218. document.head.appendChild(script);
  219. }
  220. });
  221. }
  222. },
  223. mounted() {
  224. },
  225. methods: {
  226. initAmap(marker) {
  227. this.map = new AMap.Map('amap', {
  228. resizeEnable: true,
  229. center: [112.34988, 37.827066], //中心点坐标
  230. zoom: 17, //显示的缩放级别
  231. zooms: [6, 30], //地图显示的缩放级别范围
  232. })
  233. //地图创建完成 标注点
  234. this.map.on("complete", () => {
  235. if (marker && marker.length) {
  236. this.markers = new AMap.Marker({
  237. // icon: new AMap.Icon({
  238. // image: 'static/img/map.png',
  239. // size: new AMap.Size(30, 34), //图标所处区域大小
  240. // imageSize: new AMap.Size(30, 34) //图标大小
  241. // }),
  242. position: [marker[0], marker[1]],
  243. offset: new AMap.Pixel(-15, -30)
  244. });
  245. this.markers.setMap(this.map);
  246. this.map.setCenter(marker)
  247. }
  248. })
  249. //点击事件
  250. this.map.on("click", (e) => {
  251. if (this.markers) {
  252. this.markers.setMap(null);
  253. this.markers = null;
  254. }
  255. if (!this.markers) {
  256. this.markers = new AMap.Marker({
  257. // icon: new AMap.Icon({
  258. // image: 'static/img/map.png',
  259. // size: new AMap.Size(30, 34), //图标所处区域大小
  260. // imageSize: new AMap.Size(30, 34) //图标大小
  261. // }),
  262. position: [e.lnglat.lng, e.lnglat.lat],
  263. offset: new AMap.Pixel(-15, -30)
  264. });
  265. this.markers.setMap(this.map);
  266. let str = e.lnglat.lng + ':' + e.lnglat.lat
  267. //地图点击事件 将位置信息传到上面的markerClick函数中
  268. this.$ownerInstance.callMethod('markerClick', str)
  269. }
  270. // this.createLabelsLayer();
  271. })
  272. },
  273. //接收传入的位置数据
  274. setParkList(newValue, oldValue, ownerInstance, instance) {
  275. this.parkList = newValue;
  276. },
  277. }
  278. }
  279. </script>
  280. <style scoped lang="scss">
  281. .map-min-2-search-box {
  282. height: 84rpx;
  283. background: #efefef;
  284. border-radius: 7px;
  285. height: 42px;
  286. margin: 0 14px 10px;
  287. display: -webkit-flex;
  288. display: flex;
  289. -webkit-justify-content: space-around;
  290. justify-content: space-around;
  291. input {
  292. cursor: auto;
  293. display: block;
  294. font-family: UICTFontTextStyleBody;
  295. height: 1.4rem;
  296. min-height: 1.4rem;
  297. overflow: hidden;
  298. text-overflow: clip;
  299. white-space: nowrap;
  300. }
  301. .locationpicker-search-content-ipt {
  302. height: 42px;
  303. padding-left: 10px;
  304. width: 70%;
  305. }
  306. .locationpicker-search-clear {
  307. height: 43px;
  308. text-align: center;
  309. width: 33px;
  310. .locationpicker-search-clear-ipt {
  311. height: 16px;
  312. margin-top: 13px;
  313. width: 16px;
  314. }
  315. }
  316. .locationpicker-search--content-btn-line {
  317. background: #c7c7c7;
  318. height: 16px;
  319. margin: 13px -10px 0 0;
  320. width: 1px;
  321. }
  322. .locationpicker-search-content-btn {
  323. color: #427cff;
  324. height: 42px;
  325. line-height: 42px;
  326. padding: 0 10px 0 20px;
  327. }
  328. }
  329. .content {
  330. height: 100vh;
  331. .list {
  332. height: calc(40vh - 100upx);
  333. overflow-y: auto;
  334. width: 702upx;
  335. margin: 0 auto 0;
  336. padding-bottom: 20upx;
  337. .item {
  338. border-bottom: 2upx solid #f3f3f3;
  339. &:last-child {
  340. border: none;
  341. }
  342. .address {
  343. font-size: 22upx;
  344. color: #666;
  345. margin: 10upx 0;
  346. }
  347. .name {
  348. font-size: 30upx;
  349. color: #333;
  350. margin-top: 10upx;
  351. }
  352. &.active {
  353. .name {
  354. font-weight: bold;
  355. color: #E13500;
  356. }
  357. .address {
  358. color: #E13500;
  359. }
  360. }
  361. }
  362. }
  363. .inputCon {
  364. width: 100%;
  365. background: #fff;
  366. top: 0;
  367. position: relative;
  368. z-index: 20;
  369. height: 100upx;
  370. display: flex;
  371. align-items: center;
  372. justify-content: center;
  373. .searchView {
  374. width: 702upx;
  375. height: 60upx;
  376. display: flex;
  377. align-items: center;
  378. line-height: 60upx;
  379. border-radius: 40upx;
  380. padding: 0 30upx;
  381. box-sizing: border-box;
  382. background: #f3f3f3;
  383. font-size: 26upx;
  384. .iconfont {
  385. color: #666;
  386. margin-right: 20upx;
  387. }
  388. input {
  389. flex: 1;
  390. }
  391. view {
  392. flex-shrink: 0;
  393. }
  394. }
  395. }
  396. }
  397. .btns {
  398. position: fixed;
  399. top: 0;
  400. left: 0;
  401. // height:100upx;
  402. width: 100%;
  403. // background:linear-gradient(to bottom,rgba(0,0,0,0.4),rgba(0,0,0,0));
  404. display: flex;
  405. align-items: center;
  406. justify-content: space-between;
  407. z-index: 1000 !important;
  408. view {
  409. margin: 50upx 24upx 0;
  410. font-size: 30upx;
  411. width: 100upx;
  412. height: 60upx;
  413. line-height: 60upx;
  414. text-align: center;
  415. border-radius: 10upx;
  416. color: #fff;
  417. &:first-child {
  418. background: #7e6d68;
  419. }
  420. &:last-child {
  421. background: #E13500;
  422. }
  423. }
  424. }
  425. </style>