geolocation.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <view class="amap-container">
  3. <view :change:prop="amap.updateEcharts" id="amap"></view>
  4. <view style="margin: 30rpx;">
  5. <button type="primary" @click="amap.onClick">定位当前位置</button>
  6. </view>
  7. <view style="margin: 15rpx;">当前位置信息:lng:{{ currentPosition.lng }} , lat:{{currentPosition.lat}}</view>
  8. </view>
  9. </template>
  10. <script>
  11. export default {
  12. data() {
  13. return {
  14. markerList: [],
  15. currentPosition: {}
  16. }
  17. },
  18. mounted() {
  19. },
  20. methods: {
  21. //地图点击回调事件
  22. onViewClick(params) {
  23. this.currentPosition = params.currentPosition
  24. }
  25. }
  26. }
  27. </script>
  28. <script module="amap" lang="renderjs">
  29. import config from './config.js'
  30. export default {
  31. data() {
  32. return {
  33. map: null,
  34. ownerInstanceObj: null //service层对象
  35. }
  36. },
  37. mounted() {
  38. if (typeof window.AMap === 'function') {
  39. this.initAmap()
  40. } else {
  41. // 动态引入较大类库避免影响页面展示
  42. const script = document.createElement('script')
  43. script.src = 'https://webapi.amap.com/maps?v=1.4.15&key=' + config.WEBAK
  44. script.onload = this.initAmap.bind(this)
  45. document.head.appendChild(script)
  46. }
  47. },
  48. methods: {
  49. initAmap() {
  50. this.map = new AMap.Map('amap', {
  51. resizeEnable: true
  52. })
  53. },
  54. updateEcharts(newValue, oldValue, ownerInstance, instance) {
  55. // 监听 service 层数据变更
  56. this.ownerInstanceObj = ownerInstance
  57. },
  58. onClick(event, ownerInstance) {
  59. // 创建一个 Marker 实例:
  60. let marker = new AMap.Marker({
  61. position: this.map.getCenter() // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
  62. })
  63. // 将创建的点标记添加到已有的地图实例:
  64. this.map.add(marker)
  65. // 调用 service 层的方法
  66. ownerInstance.callMethod('onViewClick', {
  67. currentPosition: this.map.getCenter()
  68. })
  69. }
  70. }
  71. }
  72. </script>
  73. <style lang="scss" scoped>
  74. #amap {
  75. width: 100%;
  76. height: 600rpx;
  77. }
  78. </style>