liu-easy-map.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  1. <template>
  2. <view style="width: 100%; height: 100%">
  3. <map
  4. style="width: 100%; height: 100%"
  5. id="esaymap"
  6. :scale="scale"
  7. :latitude="nowLat ? nowLat : centerLat"
  8. :longitude="nowLng ? nowLng : centerLng"
  9. :markers="markers"
  10. :polygons="polygonsData"
  11. :enable-zoom="isEnableZoom"
  12. :enable-scroll="isEnableScroll"
  13. :enable-satellite="isShowWxMap"
  14. :enable-rotate="isEnableRotate"
  15. @markertap="chooseItem"
  16. @tap="clickMap"
  17. >
  18. </map>
  19. <view class="rightbox">
  20. <view class="boxitem" @click="myArea()">
  21. <image
  22. class="itemimg"
  23. :src="tabIndex ? myaddressOnImg : myaddressImg"
  24. mode=""
  25. ></image>
  26. <view class="itemname" :class="tabIndex ? 'active' : ''">我的位置</view>
  27. </view>
  28. <view class="boxitem" @click="changeTab(2)" v-if="wxMapShow">
  29. <image
  30. class="itemimg"
  31. :src="tabIndex2 ? wxmapOnImg : wxmapImg"
  32. mode=""
  33. ></image>
  34. <view class="itemname" :class="tabIndex2 ? 'active' : ''"
  35. >卫星地图</view
  36. >
  37. </view>
  38. </view>
  39. <cover-view class="detailbox" v-if="isShowDetail">
  40. <cover-image
  41. class="closeicon"
  42. :src="closeImg"
  43. @click="closeDetail"
  44. ></cover-image>
  45. <cover-view class="boxl">
  46. <cover-view
  47. class="boxlhd"
  48. style="border-bottom: 2rpx solid rgba(204, 204, 204, 0.3)"
  49. >{{ detailData.name || "--" }}</cover-view
  50. >
  51. <cover-view class="boxlbd">
  52. 经度位置:{{ detailData.latitude || "--" }}
  53. </cover-view>
  54. <cover-view
  55. class="boxlbd"
  56. style="
  57. border-bottom: 2rpx solid rgba(204, 204, 204, 0.3);
  58. margin-bottom: 20rpx;
  59. "
  60. >
  61. 纬度位置:{{ detailData.longitude || "--" }}
  62. </cover-view>
  63. <cover-view class="boxlbd" @click="goRoute">
  64. <cover-image
  65. style="
  66. background: #3045db;
  67. width: 80rpx;
  68. text-align: center;
  69. border-radius: 1rem;
  70. padding: 10rpx 20rpx;
  71. color: white;
  72. float: right;
  73. "
  74. :src="require('./daohang.png')"
  75. ></cover-image>
  76. </cover-view>
  77. </cover-view>
  78. </cover-view>
  79. </view>
  80. </template>
  81. <script>
  82. export default {
  83. props: {
  84. //标记点数据
  85. markerData: {
  86. type: Array,
  87. default() {
  88. return [];
  89. },
  90. },
  91. //多边形数据
  92. polygons: {
  93. type: Array,
  94. default() {
  95. return [];
  96. },
  97. },
  98. //标记点图标宽度
  99. markerIconWidth: {
  100. type: Number,
  101. default: 22,
  102. },
  103. //标记点图标高度
  104. markerIconHeight: {
  105. type: Number,
  106. default: 32,
  107. },
  108. //标记点图标路径
  109. markerIconUrl: {
  110. type: String,
  111. default: "",
  112. },
  113. //缩放级别 取值范围为3-20
  114. scale: {
  115. type: Number,
  116. default: 16,
  117. },
  118. //是否显示指南针
  119. isShowCompass: {
  120. type: Boolean,
  121. default: false,
  122. },
  123. //是否支持缩放
  124. isEnableZoom: {
  125. type: Boolean,
  126. default: true,
  127. },
  128. //是否支持拖动
  129. isEnableScroll: {
  130. type: Boolean,
  131. default: true,
  132. },
  133. //是否支持旋转
  134. isEnableRotate: {
  135. type: Boolean,
  136. default: false,
  137. },
  138. },
  139. watch: {
  140. markerData: {
  141. immediate: true, //初始化的时候是否调用
  142. deep: true, //是否开启深度监听
  143. handler(newValue, oldValue) {
  144. this.markerDatas = newValue;
  145. this.showMarkers();
  146. },
  147. },
  148. polygons: {
  149. immediate: true, //初始化的时候是否调用
  150. deep: true, //是否开启深度监听
  151. handler(newValue, oldValue) {
  152. this.polygonsData = [...newValue];
  153. },
  154. },
  155. },
  156. data() {
  157. return {
  158. markerImg: require("../../static/marker.png"),
  159. goImg: require("../../static/go.png"),
  160. myaddressImg: require("../../static/myaddress.png"),
  161. wxmapImg: require("../../static/wxmap.png"),
  162. myaddressOnImg: require("../../static/myaddress-on.png"),
  163. wxmapOnImg: require("../../static/wxmap-on.png"),
  164. closeImg: require("../../static/close.png"),
  165. polygonsData: [], //polygons区域数据
  166. markers: [], //markers数据
  167. detailData: {}, //选中展示详情数据
  168. nowLat: "", //我的当前位置
  169. nowLng: "",
  170. tabIndex: false,
  171. centerLat: "",
  172. centerLng: "",
  173. tabIndex2: false,
  174. isShowWxMap: false, //是否展示卫星地图
  175. isShowDetail: false, //是否展示详情弹框
  176. wxMapShow: false, //是否展示卫星地图按钮(小程序展示)
  177. };
  178. },
  179. mounted() {
  180. const type = uni.getSystemInfoSync().uniPlatform;
  181. if (type == "mp-weixin") {
  182. this.wxMapShow = true;
  183. }
  184. this.showMarkers();
  185. if (!this.centerLat || !this.centerLng) this.getLocation();
  186. this.myArea();
  187. },
  188. methods: {
  189. myArea() {
  190. this.tabIndex = !this.tabIndex;
  191. this.getLocation();
  192. },
  193. //右侧类型切换
  194. changeTab(index) {
  195. if (index == 1) {
  196. this.tabIndex = !this.tabIndex;
  197. if (this.tabIndex) this.getLocation();
  198. else this.showMarkers();
  199. } else {
  200. this.tabIndex2 = !this.tabIndex2;
  201. if (this.tabIndex2) this.isShowWxMap = true;
  202. else this.isShowWxMap = false;
  203. }
  204. },
  205. changeCenter(e) {
  206. this.centerLat = e.latitude;
  207. this.centerLng = e.longitude;
  208. let mapObjs = uni.createMapContext("esaymap", this);
  209. mapObjs.moveToLocation(
  210. {
  211. latitude: e.latitude,
  212. longitude: e.longitude,
  213. },
  214. {
  215. complete: (res) => {},
  216. }
  217. );
  218. },
  219. //获取当前的地理位置
  220. getLocation() {
  221. uni.getLocation({
  222. type: "gcj02",
  223. isHighAccuracy: true,
  224. highAccuracyExpireTime: 3500,
  225. success: (res) => {
  226. console.log(res);
  227. this.centerLat = res.latitude;
  228. this.centerLng = res.longitude;
  229. let mapObjs = uni.createMapContext("esaymap", this);
  230. mapObjs.moveToLocation(
  231. {
  232. latitude: res.latitude,
  233. longitude: res.longitude,
  234. },
  235. {
  236. complete: (res) => {},
  237. }
  238. );
  239. },
  240. fail: (res) => {
  241. if (res.errMsg == "getLocation:fail auth deny") {
  242. uni.showModal({
  243. content: "检测到您没打开获取信息功能权限,是否去设置打开?",
  244. confirmText: "确认",
  245. cancelText: "取消",
  246. success: (res) => {
  247. if (res.confirm) {
  248. uni.openSetting({
  249. success: (res) => {},
  250. });
  251. } else {
  252. return false;
  253. }
  254. },
  255. });
  256. }
  257. },
  258. });
  259. },
  260. //到这去
  261. goRoute() {
  262. uni.openLocation({
  263. latitude: +this.detailData.latitude,
  264. longitude: +this.detailData.longitude,
  265. scale: 17,
  266. name: this.detailData.name || "--",
  267. address: this.detailData.address || "--",
  268. });
  269. },
  270. //地图打点展示marker
  271. showMarkers() {
  272. if (this.markerDatas && this.markerDatas.length > 0) {
  273. var arr = [];
  274. for (var i = 0; i < this.markerDatas.length; i++) {
  275. arr.push({
  276. id: Number(this.markerDatas[i].id),
  277. latitude: this.markerDatas[i].latitude || "", //纬度
  278. longitude: this.markerDatas[i].longitude || "", //经度
  279. iconPath: this.markerDatas[i].markerUrl
  280. ? this.markerDatas[i].markerUrl
  281. : this.markerImg, //显示的图标
  282. rotate: 0, // 旋转度数
  283. width: this.markerDatas[i].iconWidth
  284. ? this.markerDatas[i].iconWidth
  285. : this.markerIconWidth, //宽
  286. height: this.markerDatas[i].iconHeight
  287. ? this.markerDatas[i].iconHeight
  288. : this.markerIconHeight, //高
  289. callout: {
  290. //自定义标记点上方的气泡窗口 点击有效
  291. content: this.markerDatas[i].name, //文本
  292. color: this.markerDatas[i].calloutColor || "#ffffff", //文字颜色
  293. fontSize: this.markerDatas[i].calloutFontSize || 14, //文本大小
  294. borderRadius: this.markerDatas[i].calloutBorderRadius || 6, //边框圆角
  295. padding: this.markerDatas[i].calloutPadding || 6,
  296. bgColor: this.markerDatas[i].calloutBgColor || "#0B6CFF", //背景颜色
  297. display: this.markerDatas[i].calloutDisplay || "BYCLICK", //常显
  298. },
  299. });
  300. }
  301. this.markers = arr;
  302. }
  303. },
  304. //点击标记点
  305. chooseItem(e) {
  306. let markerId = e.detail.markerId;
  307. for (var i = 0; i < this.markerDatas.length; i++) {
  308. if (this.markerDatas[i].id == markerId) {
  309. this.isShowDetail = true;
  310. this.detailData = this.markerDatas[i];
  311. this.$emit("clickMarker", this.markerDatas[i]);
  312. break;
  313. }
  314. }
  315. },
  316. //点击地图(仅微信小程序支持)
  317. clickMap(e) {
  318. // #ifdef MP-WEIXIN
  319. let lat = e.detail.latitude.toFixed(5);
  320. let lng = e.detail.longitude.toFixed(5);
  321. this.$emit("clickMap", {
  322. latitude: lat,
  323. longitude: lng,
  324. });
  325. // #endif
  326. },
  327. //关闭详情弹框
  328. closeDetail() {
  329. this.detailData = {};
  330. this.isShowDetail = false;
  331. },
  332. },
  333. };
  334. </script>
  335. <style>
  336. .rightbox {
  337. padding: 0 8rpx;
  338. background: #ffffff;
  339. box-shadow: 0rpx 4rpx 8rpx 0rpx rgba(200, 200, 200, 0.5);
  340. border-radius: 14rpx;
  341. position: fixed;
  342. top: 185rpx;
  343. right: 20rpx;
  344. }
  345. .boxitem {
  346. display: flex;
  347. flex-direction: column;
  348. text-align: center;
  349. padding-bottom: 8rpx;
  350. border-bottom: 2rpx solid #e4e4e4;
  351. }
  352. .itemimg {
  353. width: 40rpx;
  354. height: 40rpx;
  355. margin: 16rpx auto 4rpx;
  356. }
  357. .itemname {
  358. font-size: 22rpx;
  359. font-weight: 400;
  360. color: #333333;
  361. line-height: 42rpx;
  362. }
  363. .active {
  364. color: #2765f1;
  365. }
  366. .detailbox {
  367. display: flex;
  368. align-items: center;
  369. justify-content: space-between;
  370. width: calc(100% - 128rpx);
  371. padding: 24rpx 32rpx;
  372. background: #ffffff;
  373. border-radius: 16rpx;
  374. position: fixed;
  375. bottom: 32rpx;
  376. left: 32rpx;
  377. }
  378. .closeicon {
  379. width: 40rpx;
  380. height: 40rpx;
  381. position: absolute;
  382. right: 16rpx;
  383. top: 12rpx;
  384. }
  385. .boxl {
  386. width: 100%;
  387. }
  388. .boxlhd {
  389. margin-bottom: 16rpx;
  390. white-space: pre-wrap;
  391. font-size: 36rpx;
  392. font-weight: bold;
  393. color: #333333;
  394. line-height: 48rpx;
  395. }
  396. .boxlbd {
  397. font-size: 30rpx;
  398. font-weight: 400;
  399. color: #333333;
  400. line-height: 46rpx;
  401. white-space: pre-wrap;
  402. }
  403. .boxr {
  404. width: 96rpx;
  405. display: flex;
  406. align-items: center;
  407. position: relative;
  408. }
  409. .boxr::before {
  410. width: 2rpx;
  411. height: 96rpx;
  412. background: #e3e3e3;
  413. content: "";
  414. position: relative;
  415. left: 0;
  416. z-index: 99;
  417. }
  418. .boxrimg {
  419. width: 64rpx;
  420. height: 64rpx;
  421. margin: 0 auto;
  422. }
  423. </style>