index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. <template>
  2. <view>
  3. <view id="container" :style="myStyle"></view>
  4. <button @click="gotoBtn">前往某地</button>
  5. <button @click="searchBtn">查询距离某地</button>
  6. <button @click="getPoint(true)">获取自身定位(需要用微信打开)</button>
  7. <button @click="getPositionByLonLats()">定位转换文字</button>
  8. {{message}}
  9. </view>
  10. </template>
  11. <script>
  12. import * as WxJsApi from '@/utils/wxJsApi.js'
  13. import MapLoader from '@/utils/AMap'
  14. export default {
  15. name:"Carmap",
  16. data() {
  17. return {
  18. myStyle:"width: 100%; height: 340px;",
  19. longitude: 112.276527,
  20. latitude: 30.306427,
  21. longitude2: 112.276527,
  22. latitude2: 30.306427,
  23. message:"",
  24. AMap:null,
  25. marker:null,
  26. }
  27. },
  28. onReady() {
  29. WxJsApi.getWxConfig(['getLocation','addEventListener']).then((res)=>{
  30. // this.message=JSON.stringify(res)
  31. // uni.showToast({
  32. // title:JSON.stringify(res)
  33. // })
  34. //(res)
  35. }).catch(error => {
  36. //(res)
  37. })
  38. },
  39. methods:{
  40. //获取定位地址
  41. getPositionByLonLats() {
  42. var _this = this;
  43. MapLoader().then(AMap => {
  44. var lnglatXY =//[_this.longitude, _this.latitude]
  45. [_this.longitude2, _this.latitude];
  46. AMap.service('AMap.Geocoder', function() {
  47. let geocoder = new AMap.Geocoder({});
  48. geocoder.getAddress(lnglatXY, function(status, result) {
  49. //(lnglatXY);
  50. //(status, result);
  51. if (status === 'complete' && result.info === 'OK') {
  52. var address = result.regeocode.formattedAddress;
  53. //(address);
  54. _this.message= address;
  55. } else {
  56. _this.message= '无法获取定位';
  57. }
  58. });
  59. });
  60. }).catch(error => {
  61. //(error)
  62. })
  63. },
  64. getPoint() {
  65. WxJsApi.getLocation().then((res) => {
  66. var latitude = parseFloat(res.latitude);
  67. var longitude = parseFloat(res.longitude);
  68. this.latitude2 = latitude;
  69. this.longitude2 = longitude;
  70. uni.showToast({
  71. title:JSON.stringify(res)
  72. })
  73. this.message=JSON.stringify(res)
  74. }).catch(error => {
  75. uni.showToast({
  76. title:JSON.stringify(error)
  77. })
  78. })
  79. },
  80. searchBtn(){
  81. var _this = this;
  82. MapLoader().then(AMap1 => {
  83. this.AMap=AMap;
  84. var map=new AMap.Map("container", {
  85. center: [116.397559, 39.89621],
  86. zoom: 14
  87. });
  88. AMap.plugin('AMap.Driving', function() {
  89. var driving = new AMap.Driving({
  90. // 驾车路线规划策略,AMap.DrivingPolicy.LEAST_TIME是最快捷模式
  91. policy: AMap.DrivingPolicy.LEAST_TIME
  92. })
  93. var startLngLat = [116.379028, 39.865042]
  94. var endLngLat = [116.427281, 39.903719]
  95. driving.search(startLngLat, endLngLat, function (status, result) {
  96. //(result)
  97. //(result.routes[0].time)//秒
  98. //(result.routes[0].distance)//米
  99. uni.showToast({
  100. title:result.routes[0].time+"秒,"+result.routes[0].distance+"米"
  101. })
  102. })
  103. var startLngLat2 = [_this.longitude, _this.latitude]
  104. var endLngLat2 = [_this.longitude2, _this.latitude2]
  105. driving.search(startLngLat2, endLngLat2, function (status, result) {
  106. //(result)
  107. //(result.routes[0].time)//秒
  108. //(result.routes[0].distance)//米
  109. uni.showToast({
  110. title:result.routes[0].time+"秒,"+result.routes[0].distance+"米"
  111. })
  112. })
  113. })
  114. }, e => {
  115. //_this.mui.toast('地图加载失败');
  116. //('地图加载失败', e)
  117. })
  118. },
  119. gotoBtn(){
  120. var _this = this;
  121. MapLoader().then(AMap => {
  122. this.AMap=AMap;
  123. var marker=new AMap.Marker({
  124. position: [this.longitude, this.latitude],
  125. });
  126. marker.markOnAMAP({
  127. position: marker.getPosition(),
  128. // name:'首开广场'//name属性在移动端有效
  129. })
  130. }, e => {
  131. //_this.mui.toast('地图加载失败');
  132. //('地图加载失败', e)
  133. })
  134. }
  135. },
  136. }
  137. </script>
  138. <style>
  139. </style>