map.vue 11 KB

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