map.vue 9.2 KB

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