map.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  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)">
  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: '197d1a12301a306f82533a23651807b8',
  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. type: '',
  68. keywords: "",
  69. }
  70. },
  71. onShow() {
  72. if (this.latitude == 0) {
  73. this.getCurrentLocation();
  74. }
  75. },
  76. onLoad(params) {
  77. this.type = params.type;
  78. },
  79. onReady() {
  80. this.mapContext = uni.createMapContext("map1", this);
  81. },
  82. methods: {
  83. btn_click() {
  84. uni.navigateTo({
  85. url: '/pages/chat/chat'
  86. })
  87. },
  88. getCurrentLocation() {
  89. //获取当前的地理位置
  90. let vthis = this;
  91. uni.getLocation({
  92. type: 'gcj02',
  93. geocode: true,
  94. success: function(res) {
  95. vthis.latitude = res.latitude;
  96. vthis.longitude = res.longitude;
  97. vthis.city = res.address.city;
  98. vthis.cityInfo = res;
  99. vthis.mapshow = true;
  100. vthis.addressConversion();
  101. },
  102. fail: function(err) {
  103. uni.showModal({
  104. title: '提示',
  105. content: '请打开定位服务',
  106. success: ({
  107. confirm,
  108. cancel
  109. }) => {
  110. if (confirm) {
  111. // android平台
  112. if (uni.getSystemInfoSync().platform == 'android') {
  113. var Intent = plus.android.importClass(
  114. 'android.content.Intent');
  115. var Settings = plus.android.importClass(
  116. 'android.provider.Settings');
  117. var intent = new Intent(Settings
  118. .ACTION_LOCATION_SOURCE_SETTINGS);
  119. var main = plus.android.runtimeMainActivity();
  120. main.startActivity(intent); // 打开系统设置GPS服务页面
  121. }
  122. }
  123. // 用户取消前往开启定位服务
  124. if (cancel) {
  125. // do sth...
  126. }
  127. }
  128. });
  129. }
  130. });
  131. },
  132. controltap() {
  133. this.getCurrentLocation();
  134. this.timeKey++;
  135. },
  136. addressConversion() {
  137. uni.request({
  138. url: `https://restapi.amap.com/v3/place/around?key=${this.mapKey}&location=${this.longitude},${this.latitude}&radius=5000&types=&city=${this.city}&keywords=${this.keywords}`,
  139. method: "GET",
  140. success: (res) => {
  141. this.addressList = res.data.pois;
  142. this.addressList.map(val => {
  143. val.distance = this.conversion(val.distance);
  144. return val;
  145. })
  146. }
  147. });
  148. },
  149. searchaddress() {
  150. uni.navigateTo({
  151. url: "/pages/caragent/addressSearch?info=" + JSON.stringify(this.cityInfo)
  152. })
  153. },
  154. getCenterLocation() {
  155. this.mapContext.getCenterLocation({
  156. success: (res) => {
  157. this.latitude = res.latitude;
  158. this.longitude = res.longitude;
  159. this.mapshow = true;
  160. this.addressConversion();
  161. }
  162. })
  163. },
  164. addressClick(item) {
  165. //选中之后返回上一页并修改数据
  166. let pages = getCurrentPages(); //获取所有页面栈实例列表
  167. let nowPage = pages[pages.length - 1]; //当前页页面实例
  168. let prevPage = pages[pages.length - 2]; //上一页页面实例
  169. let data = item.location.split(',')
  170. switch (prevPage.route) {
  171. case 'pages/roadRescue/Grounding':
  172. prevPage.$vm.dataInfo.rescuePosition = item.address; //详细地址
  173. break;
  174. case 'pages/roadRescue/trailer':
  175. if (this.type == '1') {
  176. prevPage.$vm.dataInfo.rescueStarting = item.address; //详细地址
  177. prevPage.$vm.dataInfo.startingLongitude = data[0]; //详细地址
  178. prevPage.$vm.dataInfo.startingLatitude = data[1]; //详细地址
  179. } else {
  180. prevPage.$vm.dataInfo.rescuePosition = item.address; //详细地址
  181. prevPage.$vm.dataInfo.positionLongitude = data[0]; //详细地址
  182. prevPage.$vm.dataInfo.positionLatitude = data[1]; //详细地址
  183. }
  184. break;
  185. case 'pages/roadRescue/replacement':
  186. prevPage.$vm.dataInfo.rescuePosition = item.address; //详细地址
  187. break;
  188. default:
  189. }
  190. uni.navigateBack({ //uni.navigateTo跳转的返回,默认1为返回上一级
  191. delta: 1
  192. });
  193. },
  194. conversion(data) {
  195. if (data < 1000) {
  196. return data + 'm'
  197. } else {
  198. return data / 1000 + 'km'
  199. }
  200. },
  201. add() {
  202. // this.gomapApp(37.857133, 112.521744, "北京市")
  203. },
  204. gomapApp(latitude, longitude, name) {
  205. let url = "";
  206. if (plus.os.name == "Android") { //判断是安卓端
  207. plus.nativeUI.actionSheet({ //选择菜单
  208. title: "选择地图应用",
  209. cancel: "取消",
  210. buttons: [{
  211. title: "腾讯地图"
  212. }, {
  213. title: "百度地图"
  214. }, {
  215. title: "高德地图"
  216. }]
  217. }, function(e) {
  218. console.log(e);
  219. switch (e.index) {
  220. //下面是拼接url,不同系统以及不同地图都有不同的拼接字段
  221. case 1:
  222. //注意referer=xxx的xxx替换成你在腾讯地图开发平台申请的key
  223. url = `qqmap://map/geocoder?coord=${latitude},${longitude}&referer=xxx`;
  224. break;
  225. case 2:
  226. url =
  227. `baidumap://map/marker?location=${latitude},${longitude}&title=${name}&coord_type=gcj02&src=andr.baidu.openAPIdemo`;
  228. break;
  229. case 3:
  230. url =
  231. `amapuri://route/plan/?sid=&slat=${latitude}&slon=${longitude}&sname=太原市&did=&dlat=${39.990496}&dlon=${116.482086}&dname=${name}&dev=0&t=0`;
  232. break;
  233. default:
  234. break;
  235. }
  236. if (url != "") {
  237. url = encodeURI(url);
  238. //plus.runtime.openURL(url,function(e){})调起手机APP应用
  239. plus.runtime.openURL(url, function(e) {
  240. plus.nativeUI.alert("本机未安装指定的地图应用");
  241. });
  242. }
  243. })
  244. } else {
  245. // iOS上获取本机是否安装了百度高德地图,需要在manifest里配置
  246. // 在manifest.json文件app-plus->distribute->apple->urlschemewhitelist节点下添加
  247. //(如urlschemewhitelist:["iosamap","baidumap"])
  248. plus.nativeUI.actionSheet({
  249. title: "选择地图应用",
  250. cancel: "取消",
  251. buttons: [{
  252. title: "腾讯地图"
  253. }, {
  254. title: "百度地图"
  255. }, {
  256. title: "高德地图"
  257. }]
  258. }, function(e) {
  259. switch (e.index) {
  260. case 1:
  261. url = `qqmap://map/geocoder?coord=${latitude},${longitude}&referer=xxx`;
  262. break;
  263. case 2:
  264. url =
  265. `baidumap://map/marker?location=${latitude},${longitude}&title=${name}&content=${name}&src=ios.baidu.openAPIdemo&coord_type=gcj02`;
  266. break;
  267. case 3:
  268. url =
  269. `iosamap://viewMap?sourceApplication=applicationName&poiname=${name}&lat=${latitude}&lon=${longitude}&dev=0`;
  270. break;
  271. default:
  272. break;
  273. }
  274. if (url != "") {
  275. url = encodeURI(url);
  276. plus.runtime.openURL(url, function(e) {
  277. plus.nativeUI.alert("本机未安装指定的地图应用");
  278. });
  279. }
  280. })
  281. }
  282. }
  283. },
  284. onUnload() {
  285. this.mapKey = 0;
  286. }
  287. }
  288. </script>
  289. <style>
  290. .mymap__map {
  291. width: 100%;
  292. height: 400px;
  293. }
  294. .search {
  295. padding: 10px;
  296. background-color: #fff;
  297. }
  298. .region {
  299. width: 70px;
  300. height: 40px;
  301. background-color: #f2f2f2;
  302. padding: 5px 10px;
  303. border-bottom-left-radius: 50px;
  304. border-top-left-radius: 50px;
  305. }
  306. .Peripheral {
  307. width: 100%;
  308. height: calc(100vh - 44px - 360px);
  309. overflow-y: auto;
  310. background-color: white;
  311. padding: 15px;
  312. margin-top: 10px;
  313. }
  314. </style>