aMap.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. <template>
  2. <view class="content">
  3. <map id="map" style="width: 100%; height: 59vh;" :latitude="latitude" :longitude="longitude" :markers="markers" @tap="onMapTap" :include-points="markers" :scale="10"/>
  4. <!-- <view id="container"></view> -->
  5. <view class="btns">
  6. <view @click="back">取消</view>
  7. <view @click="checkAdd">确定</view>
  8. </view>
  9. <view class="inputCon">
  10. <view class="searchView">
  11. <text class="iconfont icon-sousuo"></text>
  12. <input type="text" placeholder="搜索地点" v-model="searchWords" confirm-type="search" @confirm="searchFn"/>
  13. <text @click="cancel">取消</text>
  14. </view>
  15. </view>
  16. <view class="list">
  17. <view class="item" v-for="(add,index) in dataTips" :key="add.id" @click="select(add,index)"
  18. :class="selectIndex==index?'active':''">
  19. <view class="name">{{add.name}}</view>
  20. <view class="address">{{add.district || ''}}{{add.address || ''}}</view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. // 引入高德地图api提供的微信小程序的接口
  27. var amapFile = require('@/static/map/amap-wx.130.js');//如:..­/..­/libs/amap-wx.js
  28. // 创建地图
  29. var myAmapFun = new amapFile.AMapWX({key: '915d1d1ed40dc41450eda1d930ea1b2d'});
  30. console.log("创建了高德地图");
  31. export default{
  32. data(){
  33. return{
  34. selectIndex:undefined,
  35. selectAddr:{},
  36. searchWords:"",
  37. id:0, // 使用 marker点击事件 需要填写id
  38. title: 'map',
  39. latitude: 39.909,
  40. longitude: 116.39742,
  41. markers: [],
  42. markers: [{
  43. id:1,
  44. latitude: 39.909,
  45. longitude: 116.39742,
  46. // width:30,
  47. // height:30,
  48. // iconPath: '../../static/avatar_boy.png'
  49. }],
  50. dataTips:[],
  51. map: null,
  52. }
  53. },
  54. onLoad(options) {
  55. if(options && options.lng){
  56. this.longitude = options.lng;
  57. this.markers[0].longitude = this.longitude;
  58. }
  59. if(options && options.lat){
  60. this.latitude = options.lat;
  61. this.markers[0].latitude = this.latitude;
  62. }
  63. // this.initAmap();
  64. // 获取当前位置的地点列表
  65. // myAmapFun.getPoiAround({
  66. // success: (data)=>{
  67. // console.log("获取当前的列表",data);
  68. // this.dataTips = data.poisData;
  69. // },
  70. // fail: (info)=>{
  71. // console.log(info)
  72. // }
  73. // })
  74. },
  75. mounted() {
  76. this.onMapTap();
  77. },
  78. methods:{
  79. // 选择地点后,将选取的地点传递到前一个页面中
  80. checkAdd(){
  81. console.log(this.markers);
  82. uni.setStorageSync('address', this.markers[0]);
  83. // var pages = getCurrentPages();// 获取所有的页面栈
  84. // var prevPage = pages[pages.length - 2]; // 找到上一个页面,注意是页面,如果是页面中有组件,则需要通过页面接受到数据后,再次往组件中传递
  85. // prevPage.$vm.addressObj = this.selectAddr;//在上一个页面中就可以用addressObj进行接收
  86. // uni.navigateBack();
  87. this.back()
  88. },
  89. back(){
  90. // 返回上一页
  91. uni.navigateBack({
  92. delta: 1 // 返回的页面数,默认为1
  93. })
  94. },
  95. cancel(){
  96. if(this.searchWords){
  97. this.searchWords = "";
  98. myAmapFun.getPoiAround({
  99. location: this.markers[0].longitude+','+this.markers[0].latitude,
  100. success: (data)=>{
  101. console.log("获取当前的列表",data);
  102. this.dataTips = data.poisData;
  103. },
  104. fail: (info)=>{
  105. console.log(info)
  106. }
  107. })
  108. }
  109. },
  110. reserGeo(){
  111. myAmapFun.getRegeo({
  112. success: (data)=>{
  113. console.log("获取当前定位信息",data);
  114. },
  115. fail: (info)=>{
  116. console.log(info)
  117. }
  118. })
  119. },
  120. // 根据地址列表中选择某一个地点
  121. select(add,index){
  122. console.log(add,index);
  123. if(!add){
  124. return;
  125. }
  126. this.selectIndex = index;
  127. var location = add.location.split(",");
  128. console.log(location);
  129. this.selectAddr = {
  130. address:add.pname?(add.pname+add.cityname+add.adname+add.address):(add.district+add.address),
  131. latitude:location[1],
  132. longitude:location[0]
  133. };
  134. this.markers[0].latitude = +location[1];
  135. this.markers[0].longitude = +location[0];
  136. },
  137. // 在地图上点击进行选点,这个选点在地图缩放比例较大时无效,因为精读的问题。
  138. onMapTap(e){
  139. // const mapContext = uni.createMapContext('map');
  140. var mapContext = uni.createMapContext("map", this).$getAppMap();
  141. console.log( mapContext,2222222222222);
  142. mapContext.on('tap', (res) => {
  143. console.log('点击的坐标1222222:', res);
  144. // const { latitude, longitude } = res.detail;
  145. // console.log('点击的坐标:', latitude, longitude);
  146. // 在这里你可以对获取到的坐标进行处理或展示
  147. });
  148. // console.log(e);
  149. var location = e.detail.longitude +','+e.detail.latitude
  150. // this.selectAddr = {
  151. // address:add.pname?(add.pname+add.cityname+add.adname+add.address):(add.district+add.address),
  152. // latitude:location[1],
  153. // longitude:location[0]
  154. // };
  155. // this.markers[0].latitude = data[0].latitude;
  156. // this.markers[0].longitude = data[0].longitude;
  157. // myAmapFun.getRegeo({
  158. // location: location,
  159. // success: (data)=>{
  160. // console.log("获取指定定位信息",data);
  161. // this.selectAddr = {
  162. // address:data[0].regeocodeData.formatted_address,
  163. // latitude:e.detail.latitude,
  164. // longitude:e.detail.longitude
  165. // };
  166. // this.markers[0].latitude = data[0].latitude;
  167. // this.markers[0].longitude = data[0].longitude;
  168. // myAmapFun.getPoiAround({
  169. // location: data[0].longitude+','+data[0].latitude,
  170. // success: (data)=>{
  171. // console.log("获取当前的列表",data);
  172. // this.dataTips = data.poisData;
  173. // },
  174. // fail: (info)=>{
  175. // console.log(info)
  176. // }
  177. // })
  178. // },
  179. // fail: (info)=>{
  180. // console.log(info);
  181. // }
  182. // })
  183. },
  184. // 根据内容进行检索
  185. searchFn(){
  186. console.log("根据地址检索",this.searchWords);
  187. myAmapFun.getInputtips({
  188. keywords: this.searchWords,
  189. location: '',
  190. success: (data) => {
  191. console.log(111,data);
  192. if(data && data.tips){
  193. this.dataTips = data.tips;
  194. }
  195. },
  196. fail:data=>{
  197. console.log(222,data);
  198. }
  199. })
  200. }
  201. }
  202. }
  203. </script>
  204. <style lang="scss" scoped>
  205. .btns{
  206. position: fixed;
  207. top:0;
  208. left:0;
  209. // height:100upx;
  210. width:100%;
  211. // background:linear-gradient(to bottom,rgba(0,0,0,0.4),rgba(0,0,0,0));
  212. display: flex;
  213. align-items: center;
  214. justify-content: space-between;
  215. z-index:1000 !important;
  216. view{
  217. margin:50upx 24upx 0;
  218. font-size:30upx;
  219. width:100upx;
  220. height:60upx;
  221. line-height: 60upx;
  222. text-align: center;
  223. border-radius: 10upx;
  224. color:#fff;
  225. &:first-child{
  226. background:#7e6d68;
  227. }
  228. &:last-child{
  229. background:#E13500;
  230. }
  231. }
  232. }
  233. .content{
  234. height: 100vh;
  235. .list{
  236. height:calc(40vh - 100upx);
  237. overflow-y: auto;
  238. width:702upx;
  239. margin:-40upx auto 0;
  240. padding-bottom:20upx;
  241. .item{
  242. border-bottom:2upx solid #f3f3f3;
  243. &:last-child{
  244. border:none;
  245. }
  246. .address{
  247. font-size:22upx;
  248. color:#666;
  249. margin:10upx 0;
  250. }
  251. .name{
  252. font-size:30upx;
  253. color:#333;
  254. margin-top:10upx;
  255. }
  256. &.active{
  257. .name{
  258. font-weight: bold;
  259. color:#E13500;
  260. }
  261. .address{
  262. color:#E13500;
  263. }
  264. }
  265. }
  266. }
  267. .inputCon{
  268. width:100%;
  269. background:#fff;
  270. top:0;
  271. position: relative;
  272. z-index:20;
  273. height:100upx;
  274. display: flex;
  275. align-items: center;
  276. justify-content: center;
  277. .searchView{
  278. width:702upx;
  279. height:60upx;
  280. display: flex;
  281. align-items: center;
  282. line-height: 60upx;
  283. border-radius: 40upx;
  284. padding:0 30upx;
  285. box-sizing: border-box;
  286. background:#f3f3f3;
  287. font-size:26upx;
  288. .iconfont{
  289. color:#666;
  290. margin-right:20upx;
  291. }
  292. input{
  293. flex:1;
  294. }
  295. view{
  296. flex-shrink: 0;
  297. }
  298. }
  299. }
  300. }
  301. </style>