05ToolLocation.html 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>坐标定位、坐标拾取</title>
  7. <link rel="stylesheet" href="../../dist/ol/ol.css" />
  8. <script src="../assets/vue@2.7.14.js"></script>
  9. <script src="../../dist/index.js"></script>
  10. <style>
  11. body {
  12. margin: 0
  13. }
  14. #map {
  15. height: 100vh;
  16. }
  17. .control {
  18. position: absolute;
  19. top: 20px;
  20. left: 40px;
  21. padding: 10px 10px 0;
  22. background: #fff;
  23. }
  24. </style>
  25. </head>
  26. <body>
  27. <div id="app">
  28. <div id="map"></div>
  29. <div class="control">
  30. <label>坐标</label>
  31. <input type="text" v-model="longitude" placeholder="经度">
  32. <input type="text" v-model="latitude" placeholder="纬度">
  33. <br>
  34. <template v-if="ready">
  35. <button @click="locate">定位</button>
  36. <button @click="pick">拾取</button>
  37. <button @click="_ => location.stopPick()">停止拾取</button>
  38. <button @click="destroy">销毁</button>
  39. </template>
  40. <template v-else>
  41. <button @click="init">初始化</button>
  42. </template>
  43. <p>注意:Vue项目中<b>不要</b>将地图相关实例对象赋值给响应式变量</p>
  44. </div>
  45. </div>
  46. </body>
  47. <script>
  48. new Vue({
  49. el: '#app',
  50. data () {
  51. return {
  52. longitude: '',
  53. latitude: '',
  54. ready: false
  55. }
  56. },
  57. mounted () {
  58. this.map = new CTMapOl.extend.InitMap({
  59. domId: 'map'
  60. })
  61. this.init()
  62. },
  63. methods: {
  64. init () {
  65. // 不要在 data 中定义 location 变量,无需响应式
  66. this.location = new CTMapOl.extend.ToolLocation(this.map.map, {
  67. onPick: coords => {
  68. this.longitude = coords[0]
  69. this.latitude = coords[1]
  70. }
  71. })
  72. this.ready = true
  73. },
  74. locate () {
  75. this.location.locate([+this.longitude, +this.latitude])
  76. },
  77. pick () {
  78. this.location.pick()
  79. },
  80. destroy () {
  81. this.location.destroy()
  82. this.location = null
  83. this.ready = false
  84. }
  85. }
  86. })
  87. </script>
  88. </html>