map.vue 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <template>
  2. <view class="page">
  3. <service-widget @btnClick="btn_click"></service-widget>
  4. <view class="search" @click="searchaddress">
  5. <view class="region dis a-c j-s" style="position: absolute; z-index: 9;">
  6. <text>{{city}}</text>
  7. <view style="width: 1px;height: 60%;background-color: #999;"></view>
  8. </view>
  9. <u-search style="position: relative;" placeholder="请输入位置/地名等" :show-action="false" :height='80'
  10. :input-style="{paddingLeft: '50px'}" :disabled="true">
  11. </u-search>
  12. <view v-if="searchshow" class="" style="width: 100%;height: 100vh;background-color: #fff;">
  13. </view>
  14. </view>
  15. <map v-if="mapshow" id="map1" style="width: 100%; height: 300px;" :key="timeKey" :latitude="latitude"
  16. :longitude="longitude" :markers="covers" :controls="controls" scale='16' theme="satellite"
  17. :showLocation="true" @controltap="controltap" @regionchange="getCenterLocation">
  18. <cover-image src="/static/image/map/p1.png"
  19. style="width: 30px; height: 30px;position: absolute;top: 42%;left:45%;"></cover-image>
  20. </map>
  21. <view class="Peripheral dis f-c">
  22. <view class="dis a-c j-s" v-for="(item,index) in addressList" :key="index" style="margin-bottom: 10px;"
  23. @click="addressClick(item.address)">
  24. <view class="">
  25. <text v-if="index==0" style="font-size: 16px;color:#2979ff;font-weight: bold;">{{item.name}}</text>
  26. <text v-else style="font-size: 16px;">{{item.name}}</text>
  27. <view class="dis a-c" style="color: #999;font-size: 12px;">
  28. <text>{{item.distance}}</text>
  29. <view style="width: 1px;height: 16px;background-color: #999;margin: 0 10px;"></view>
  30. <text>{{item.address}}</text>
  31. </view>
  32. </view>
  33. <u-icon v-if="index==0" name="checkmark-circle-fill" color="#2979ff" size="40"></u-icon>
  34. <u-icon v-else name="checkmark-circle" color="rgb(217,217,217)" size="40"></u-icon>
  35. </view>
  36. </view>
  37. </view>
  38. </template>
  39. <script>
  40. export default {
  41. data() {
  42. return {
  43. mapshow: false,
  44. timeKey: 0,
  45. // mapKey: 'f6a5fce80b1093d42749c7edbf6aaee6',
  46. address: "",
  47. city: "",
  48. cityInfo: {},
  49. searchshow: false,
  50. id: 0, // 使用 marker点击事件 需要填写id
  51. title: 'map',
  52. latitude: 0,
  53. longitude: 0,
  54. covers: [],
  55. controls: [{
  56. id: 1,
  57. position: {
  58. left: 360,
  59. top: 250,
  60. width: 40,
  61. height: 40,
  62. },
  63. iconPath: "/static/image/map/d1.png",
  64. clickable: true,
  65. }],
  66. addressList: [],
  67. keywords: "",
  68. }
  69. },
  70. onShow() {
  71. if (this.latitude == 0) {
  72. this.getCurrentLocation();
  73. }
  74. },
  75. onLoad() {},
  76. onReady() {
  77. this.mapContext = uni.createMapContext("map1", this);
  78. },
  79. methods: {
  80. btn_click() {
  81. uni.navigateTo({
  82. url: '/pages/chat/chat'
  83. })
  84. },
  85. getCurrentLocation() {
  86. //获取当前的地理位置
  87. let vthis = this;
  88. uni.getLocation({
  89. type: 'gcj02',
  90. geocode: true,
  91. success: function(res) {
  92. console.log(res);
  93. vthis.latitude = res.latitude;
  94. vthis.longitude = res.longitude;
  95. vthis.city = res.address.city;
  96. vthis.cityInfo = res;
  97. vthis.mapshow = true;
  98. vthis.addressConversion();
  99. },
  100. fail: function(err) {
  101. uni.showModal({
  102. title: '提示',
  103. content: '请打开定位服务',
  104. success: ({
  105. confirm,
  106. cancel
  107. }) => {
  108. if (confirm) {
  109. // android平台
  110. if (uni.getSystemInfoSync().platform == 'android') {
  111. var Intent = plus.android.importClass(
  112. 'android.content.Intent');
  113. var Settings = plus.android.importClass(
  114. 'android.provider.Settings');
  115. var intent = new Intent(Settings
  116. .ACTION_LOCATION_SOURCE_SETTINGS);
  117. var main = plus.android.runtimeMainActivity();
  118. main.startActivity(intent); // 打开系统设置GPS服务页面
  119. }
  120. }
  121. // 用户取消前往开启定位服务
  122. if (cancel) {
  123. // do sth...
  124. }
  125. }
  126. });
  127. }
  128. });
  129. },
  130. controltap() {
  131. this.getCurrentLocation();
  132. this.timeKey++;
  133. },
  134. addressConversion() {
  135. uni.request({
  136. url: `https://restapi.amap.com/v3/place/around?key=${this.mapKey}&location=${this.longitude},${this.latitude}&radius=10000&types=&city=${this.city}&keywords=${this.keywords}`,
  137. method: "GET",
  138. success: (res) => {
  139. this.addressList = res.data.pois;
  140. this.addressList.map(val => {
  141. val.distance = this.conversion(val.distance);
  142. return val;
  143. })
  144. }
  145. });
  146. },
  147. searchaddress() {
  148. uni.navigateTo({
  149. url: "/pages/caragent/addressSearch?info=" + JSON.stringify(this.cityInfo)
  150. })
  151. },
  152. getCenterLocation() {
  153. this.mapContext.getCenterLocation({
  154. success: (res) => {
  155. this.latitude = res.latitude;
  156. this.longitude = res.longitude;
  157. this.mapshow = true;
  158. this.addressConversion();
  159. }
  160. })
  161. },
  162. addressClick(ads) {
  163. //选中之后返回上一页并修改数据
  164. let pages = getCurrentPages(); //获取所有页面栈实例列表
  165. let nowPage = pages[pages.length - 1]; //当前页页面实例
  166. let prevPage = pages[pages.length - 2]; //上一页页面实例
  167. prevPage.$vm.form.detailAddress = ads; //详细地址
  168. uni.navigateBack({ //uni.navigateTo跳转的返回,默认1为返回上一级
  169. delta: 1
  170. });
  171. },
  172. conversion(data) {
  173. if (data < 1000) {
  174. return data + 'm'
  175. } else {
  176. return data / 1000 + 'km'
  177. }
  178. },
  179. add() {
  180. // this.gomapApp(37.857133, 112.521744, "北京市")
  181. },
  182. gomapApp(latitude, longitude, name) {
  183. let url = "";
  184. if (plus.os.name == "Android") { //判断是安卓端
  185. plus.nativeUI.actionSheet({ //选择菜单
  186. title: "选择地图应用",
  187. cancel: "取消",
  188. buttons: [{
  189. title: "腾讯地图"
  190. }, {
  191. title: "百度地图"
  192. }, {
  193. title: "高德地图"
  194. }]
  195. }, function(e) {
  196. console.log(e);
  197. switch (e.index) {
  198. //下面是拼接url,不同系统以及不同地图都有不同的拼接字段
  199. case 1:
  200. //注意referer=xxx的xxx替换成你在腾讯地图开发平台申请的key
  201. url = `qqmap://map/geocoder?coord=${latitude},${longitude}&referer=xxx`;
  202. break;
  203. case 2:
  204. url =
  205. `baidumap://map/marker?location=${latitude},${longitude}&title=${name}&coord_type=gcj02&src=andr.baidu.openAPIdemo`;
  206. break;
  207. case 3:
  208. url =
  209. `amapuri://route/plan/?sid=&slat=${latitude}&slon=${longitude}&sname=太原市&did=&dlat=${39.990496}&dlon=${116.482086}&dname=${name}&dev=0&t=0`;
  210. break;
  211. default:
  212. break;
  213. }
  214. if (url != "") {
  215. url = encodeURI(url);
  216. //plus.runtime.openURL(url,function(e){})调起手机APP应用
  217. plus.runtime.openURL(url, function(e) {
  218. plus.nativeUI.alert("本机未安装指定的地图应用");
  219. });
  220. }
  221. })
  222. } else {
  223. // iOS上获取本机是否安装了百度高德地图,需要在manifest里配置
  224. // 在manifest.json文件app-plus->distribute->apple->urlschemewhitelist节点下添加
  225. //(如urlschemewhitelist:["iosamap","baidumap"])
  226. plus.nativeUI.actionSheet({
  227. title: "选择地图应用",
  228. cancel: "取消",
  229. buttons: [{
  230. title: "腾讯地图"
  231. }, {
  232. title: "百度地图"
  233. }, {
  234. title: "高德地图"
  235. }]
  236. }, function(e) {
  237. switch (e.index) {
  238. case 1:
  239. url = `qqmap://map/geocoder?coord=${latitude},${longitude}&referer=xxx`;
  240. break;
  241. case 2:
  242. url =
  243. `baidumap://map/marker?location=${latitude},${longitude}&title=${name}&content=${name}&src=ios.baidu.openAPIdemo&coord_type=gcj02`;
  244. break;
  245. case 3:
  246. url =
  247. `iosamap://viewMap?sourceApplication=applicationName&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`;
  248. break;
  249. default:
  250. break;
  251. }
  252. if (url != "") {
  253. url = encodeURI(url);
  254. plus.runtime.openURL(url, function(e) {
  255. plus.nativeUI.alert("本机未安装指定的地图应用");
  256. });
  257. }
  258. })
  259. }
  260. }
  261. },
  262. onUnload() {
  263. this.mapKey = 0;
  264. }
  265. }
  266. </script>
  267. <style>
  268. .mymap__map {
  269. width: 100%;
  270. height: 400px;
  271. }
  272. .search {
  273. padding: 10px;
  274. background-color: #fff;
  275. }
  276. .region {
  277. width: 70px;
  278. height: 40px;
  279. background-color: #f2f2f2;
  280. padding: 5px 10px;
  281. border-bottom-left-radius: 50px;
  282. border-top-left-radius: 50px;
  283. }
  284. .Peripheral {
  285. width: 100%;
  286. height: calc(100vh - 44px - 360px);
  287. overflow-y: auto;
  288. background-color: white;
  289. padding: 15px;
  290. margin-top: 10px;
  291. }
  292. </style>