map.vue 8.3 KB

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