map.nvue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. <template>
  2. <view class="content">
  3. <map class="map" id="map1" ref="map1" :controls="controls" :scale="scale" :longitude="location.longitude"
  4. :latitude="location.latitude" :show-location="showLocation" :enable-3D="enable3D" :rotate="rotate" :skew="skew"
  5. :show-compass="showCompass" :enable-overlooking="enableOverlooking" :enable-zoom="enableZoom"
  6. :enable-scroll="enableScroll" :enable-rotate="enableRotate" :enable-satellite="enableSatellite"
  7. :enable-traffic="enableTraffic" :markers="markers" :polyline="polyline" :circles="circles" :polygons="polygons"
  8. :include-points="includePoints" @tap="maptap" @controltap="oncontroltap" @markertap="onmarkertap"
  9. @callouttap="oncallouttap" @poitap="onpoitap" @updated="onupdated" @regionchange="onregionchange"></map>
  10. <scroll-view class="scrollview" scroll-y="true">
  11. <!-- <view class="list-item">
  12. <text class="list-text">显示3D楼块</text>
  13. <switch :checked="enable3D" @change="enableThreeD" />
  14. </view>
  15. <view class="list-item">
  16. <text class="list-text">显示指南针</text>
  17. <switch :checked="showCompass" @change="changeShowCompass" />
  18. </view>
  19. <view class="list-item">
  20. <text class="list-text">开启俯视</text>
  21. <switch :checked="enableOverlooking" @change="changeEnableOverlooking" />
  22. </view>
  23. <view class="list-item">
  24. <text class="list-text">是否支持缩放</text>
  25. <switch :checked="enableZoom" @change="changeEnableZoom" />
  26. </view>
  27. <view class="list-item">
  28. <text class="list-text">是否支持拖动</text>
  29. <switch :checked="enableScroll" @change="changeEnableScroll" />
  30. </view>
  31. <view class="list-item">
  32. <text class="list-text">是否支持旋转</text>
  33. <switch :checked="enableRotate" @change="changeEnableRotate" />
  34. </view>
  35. <view class="list-item">
  36. <text class="list-text">是否开启卫星图</text>
  37. <switch :checked="enableSatellite" @change="changeEnableSatellite" />
  38. </view>
  39. <view class="list-item">
  40. <text class="list-text">是否开启实时路况</text>
  41. <switch :checked="enableTraffic" @change="changeEnableTraffic" />
  42. </view> -->
  43. <!-- #ifndef MP-JD -->
  44. <button class="button" @click="changeScale">changeScale</button>
  45. <button class="button" @click="changeRotate">changeRotate</button>
  46. <button class="button" @click="changeSkew">skew</button>
  47. <!-- #endif -->
  48. <button class="button" @click="addMarkers">addMarkers</button>
  49. <button class="button" @click="addPolyline">addPolyline</button>
  50. <!-- #ifndef MP-JD -->
  51. <button class="button" @click="addPolygons">addPolygons</button>
  52. <!-- #endif -->
  53. <button class="button" @click="addCircles">addCircles</button>
  54. <button class="button" @click="includePoint">includePoints</button>
  55. <button class="button" @click="handleGetCenterLocation">getCenterLocation</button>
  56. <button class="button" @click="handleGetRegion">getRegion</button>
  57. <!-- #ifndef MP-JD -->
  58. <button class="button" @click="handleTranslateMarker">translateMarker</button>
  59. <!-- #endif -->
  60. </scroll-view>
  61. </view>
  62. </template>
  63. <script>
  64. const testMarkers = [{
  65. id: 0,
  66. latitude: 39.989631,
  67. longitude: 116.481018,
  68. title: '方恒国际 阜通东大街6号',
  69. zIndex: '1',
  70. rotate: 0,
  71. width: 20,
  72. height: 20,
  73. anchor: {
  74. x: 0.5,
  75. y: 1
  76. },
  77. callout: {
  78. content: '方恒国际 阜通东大街6号',
  79. color: '#00BFFF',
  80. fontSize: 10,
  81. borderRadius: 4,
  82. borderWidth: 1,
  83. borderColor: '#333300',
  84. bgColor: '#CCFF99',
  85. padding: '5',
  86. display: 'ALWAYS'
  87. }
  88. },
  89. {
  90. id: 1,
  91. latitude: 39.9086920000,
  92. longitude: 116.3974770000,
  93. title: '天安门',
  94. zIndex: '1',
  95. iconPath: '/static/location.png',
  96. width: 40,
  97. height: 40,
  98. anchor: {
  99. x: 0.5,
  100. y: 1
  101. },
  102. callout: {
  103. content: '首都北京\n天安门',
  104. color: '#00BFFF',
  105. fontSize: 12,
  106. borderRadius: 2,
  107. borderWidth: 0,
  108. borderColor: '#333300',
  109. bgColor: '#CCFF11',
  110. padding: '1',
  111. display: 'ALWAYS'
  112. }
  113. }
  114. ];
  115. const testPolyline = [{
  116. points: [{
  117. latitude: 39.925539,
  118. longitude: 116.279037
  119. },
  120. {
  121. latitude: 39.925539,
  122. longitude: 116.520285
  123. }
  124. ],
  125. color: '#FFCCFF',
  126. width: 7,
  127. dottedLine: true,
  128. arrowLine: true,
  129. borderColor: '#000000',
  130. borderWidth: 2
  131. },
  132. {
  133. points: [{
  134. latitude: 39.938698,
  135. longitude: 116.275177
  136. },
  137. {
  138. latitude: 39.966069,
  139. longitude: 116.289253
  140. },
  141. {
  142. latitude: 39.944226,
  143. longitude: 116.306076
  144. },
  145. {
  146. latitude: 39.966069,
  147. longitude: 116.322899
  148. },
  149. {
  150. latitude: 39.938698,
  151. longitude: 116.336975
  152. }
  153. ],
  154. color: '#CCFFFF',
  155. width: 5,
  156. dottedLine: true,
  157. arrowLine: true,
  158. borderColor: '#CC0000',
  159. borderWidth: 3
  160. }
  161. ];
  162. const testPolygons = [{
  163. points: [{
  164. latitude: 39.781892,
  165. longitude: 116.293413
  166. },
  167. {
  168. latitude: 39.787600,
  169. longitude: 116.391842
  170. },
  171. {
  172. latitude: 39.733187,
  173. longitude: 116.417932
  174. },
  175. {
  176. latitude: 39.704653,
  177. longitude: 116.338255
  178. }
  179. ],
  180. fillColor: '#FFCCFF',
  181. strokeWidth: 3,
  182. strokeColor: '#CC99CC',
  183. zIndex: 11
  184. },
  185. {
  186. points: [{
  187. latitude: 39.887600,
  188. longitude: 116.518932
  189. },
  190. {
  191. latitude: 39.781892,
  192. longitude: 116.518932
  193. },
  194. {
  195. latitude: 39.781892,
  196. longitude: 116.428932
  197. },
  198. {
  199. latitude: 39.887600,
  200. longitude: 116.428932
  201. }
  202. ],
  203. fillColor: '#CCFFFF',
  204. strokeWidth: 5,
  205. strokeColor: '#CC0000',
  206. zIndex: 3
  207. }
  208. ];
  209. const testCircles = [{
  210. latitude: 39.996441,
  211. longitude: 116.411146,
  212. radius: 15000,
  213. strokeWidth: 5,
  214. color: '#CCFFFF',
  215. fillColor: '#CC0000'
  216. },
  217. {
  218. latitude: 40.096441,
  219. longitude: 116.511146,
  220. radius: 12000,
  221. strokeWidth: 3,
  222. color: '#CCFFFF',
  223. fillColor: '#FFCCFF'
  224. },
  225. {
  226. latitude: 39.896441,
  227. longitude: 116.311146,
  228. radius: 9000,
  229. strokeWidth: 1,
  230. color: '#CCFFFF',
  231. fillColor: '#CC0000'
  232. }
  233. ];
  234. const testIncludePoints = [{
  235. latitude: 39.989631,
  236. longitude: 116.481018,
  237. },
  238. {
  239. latitude: 39.9086920000,
  240. longitude: 116.3974770000,
  241. }
  242. ];
  243. export default {
  244. data() {
  245. return {
  246. location: {
  247. longitude: 116.3974770000,
  248. latitude: 39.9086920000
  249. },
  250. controls: [{
  251. id: 1,
  252. position: {
  253. left: 5,
  254. top: 180,
  255. width: 30,
  256. height: 30
  257. },
  258. iconPath: '/static/logo.png',
  259. clickable: true
  260. }],
  261. showLocation: false,
  262. scale: 13,
  263. showCompass: true,
  264. enable3D: true,
  265. enableOverlooking: true,
  266. enableOverlooking: true,
  267. enableZoom: true,
  268. enableScroll: true,
  269. enableRotate: true,
  270. enableSatellite: false,
  271. enableTraffic: false,
  272. polyline: [],
  273. markers: [],
  274. polygons: [],
  275. circles: [],
  276. includePoints: [],
  277. rotate: 0,
  278. skew: 0
  279. }
  280. },
  281. onLoad() {},
  282. onReady() {
  283. this.map = uni.createMapContext("map1", this);
  284. },
  285. methods: {
  286. // #ifndef MP-JD
  287. changeScale() {
  288. this.scale = this.scale == 9 ? 15 : 9;
  289. },
  290. changeRotate() {
  291. this.rotate = this.rotate == 90 ? 0 : 90;
  292. },
  293. changeSkew() {
  294. this.skew = this.skew == 30 ? 0 : 30;
  295. },
  296. // #endif
  297. enableThreeD(e) {
  298. this.enable3D = e.detail.value;
  299. },
  300. changeShowCompass(e) {
  301. this.showCompass = e.detail.value;
  302. },
  303. changeEnableOverlooking(e) {
  304. this.enableOverlooking = e.detail.value;
  305. },
  306. changeEnableZoom(e) {
  307. this.enableZoom = e.detail.value;
  308. },
  309. changeEnableScroll(e) {
  310. this.enableScroll = e.detail.value;
  311. },
  312. changeEnableRotate(e) {
  313. this.enableRotate = e.detail.value;
  314. },
  315. changeEnableSatellite(e) {
  316. this.enableSatellite = e.detail.value;
  317. },
  318. changeEnableTraffic(e) {
  319. this.enableTraffic = e.detail.value;
  320. },
  321. addMarkers() {
  322. this.markers = testMarkers;
  323. },
  324. addPolyline() {
  325. this.polyline = testPolyline;
  326. },
  327. // #ifndef MP-JD
  328. addPolygons() {
  329. this.polygons = testPolygons;
  330. },
  331. // #endif
  332. addCircles() {
  333. this.circles = testCircles;
  334. },
  335. includePoint() {
  336. this.includePoints = testIncludePoints;
  337. },
  338. handleGetCenterLocation() {
  339. this.map.getCenterLocation({
  340. success: ret => {
  341. console.log(JSON.stringify(ret));
  342. uni.showModal({
  343. content: JSON.stringify(ret)
  344. })
  345. }
  346. })
  347. },
  348. handleGetRegion() {
  349. this.map.getRegion({
  350. success: ret => {
  351. console.log(JSON.stringify(ret));
  352. uni.showModal({
  353. content: JSON.stringify(ret)
  354. })
  355. }
  356. })
  357. },
  358. // #ifndef MP-JD
  359. handleTranslateMarker() {
  360. this.map.translateMarker({
  361. markerId: 1,
  362. destination: {
  363. latitude: 39.989631,
  364. longitude: 116.481018
  365. },
  366. duration: 2000
  367. }, ret => {
  368. console.log(JSON.stringify(ret));
  369. uni.showModal({
  370. content: JSON.stringify(ret)
  371. })
  372. });
  373. },
  374. // #endif
  375. maptap(e) {
  376. uni.showModal({
  377. content: JSON.stringify(e)
  378. })
  379. },
  380. onmarkertap(e) {
  381. uni.showModal({
  382. content: JSON.stringify(e)
  383. })
  384. },
  385. oncontroltap(e) {
  386. uni.showModal({
  387. content: JSON.stringify(e)
  388. })
  389. },
  390. oncallouttap(e) {
  391. uni.showModal({
  392. content: JSON.stringify(e)
  393. })
  394. },
  395. onupdated(e) {
  396. console.log(JSON.stringify(e))
  397. },
  398. onregionchange(e) {
  399. console.log(JSON.stringify(e));
  400. },
  401. onpoitap(e) {
  402. uni.showModal({
  403. content: JSON.stringify(e)
  404. })
  405. }
  406. }
  407. }
  408. </script>
  409. <style>
  410. .content {
  411. flex: 1;
  412. }
  413. .map {
  414. width: 750rpx;
  415. /* #ifdef H5 */
  416. width: 100%;
  417. /* #endif */
  418. height: 250px;
  419. background-color: #f0f0f0;
  420. }
  421. .scrollview {
  422. /* #ifdef H5 */
  423. margin-top: 240px;
  424. /* #endif */
  425. flex: 1;
  426. padding: 10px;
  427. }
  428. .list-item {
  429. flex-direction: row;
  430. flex-wrap: nowrap;
  431. align-items: center;
  432. padding: 5px 0px;
  433. }
  434. .list-text {
  435. flex: 1;
  436. }
  437. .button {
  438. margin-top: 5px;
  439. margin-bottom: 5px;
  440. }
  441. </style>