my-map.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  1. <template>
  2. <view>
  3. <map
  4. style="width: 100%; height:250px;"
  5. :latitude="latitude"
  6. :longitude="longitude"
  7. :markers="marker"
  8. :scale="scale"
  9. :circles="circles"
  10. >
  11. <!-- :circles="circles" -->
  12. </map>
  13. </view>
  14. </template>
  15. <script>
  16. import { geoDistance } from '@/common/util/util.js'
  17. import amap from "@/common/js-sdk/js-amap/amap-wx.js";
  18. // #ifdef H5
  19. import AMap from "@/common/js-sdk/js-amap/amap-h5.js";
  20. // #endif
  21. export default {
  22. props:{
  23. compLatitude:{
  24. type:Number,
  25. default:40.009390,
  26. required:false
  27. },
  28. compLongitude:{
  29. type:Number,
  30. default:116.374322,
  31. required:false
  32. }
  33. },
  34. data() {
  35. return {
  36. amapPlugin:null,
  37. wxMapKey:"53324ee357405c4a65f35a1aa05ffaf2",
  38. id:0,
  39. title: 'map',
  40. distance:0,
  41. address:"",
  42.   latitude: this.compLatitude, //纬度
  43. longitude: this.compLongitude, //经度
  44. scale:16,//地图缩放程度
  45. tipText:'打卡范围',
  46. bgColor:'#00c16f',
  47. marker: [],
  48.   circles:[{//在地图上显示圆
  49.   latitude: this.compLatitude,
  50.   longitude: this.compLongitude,
  51.   radius:80,//半径
  52. fillColor:"#ffffffAA",//填充颜色
  53.   color:"#55aaffAA",//描边的颜色
  54.    strokeWidth:1//描边的宽度
  55. }],
  56. resAmap:null
  57. }
  58. },
  59. created() {
  60. // #ifdef MP-WEIXIN || APP-PLUS
  61. this.amapPlugin = new amap.AMapWX({
  62. key: this.wxMapKey
  63. });
  64. // #endif
  65. // #ifdef H5
  66. this.initAMap()
  67. // #endif
  68. },
  69. mounted() {
  70. // #ifdef MP-WEIXIN
  71. this.getAuthorizeInfo();
  72. // #endif
  73. // #ifdef APP-PLUS
  74. this.getLocationInfoWx();
  75. // #endif
  76. // #ifdef H5
  77. //this.getLocationInfo()
  78. // #endif
  79. },
  80. computed:{
  81. inCircle(){
  82. return this.address && this.distance <= 80
  83. }
  84. },
  85. methods: {
  86. allowed(){
  87. return this.inCircle
  88. },
  89. getMyAddress(){
  90. return this.address
  91. },
  92. refreshLocation(){
  93. // #ifdef MP-WEIXIN
  94. this.getAuthorizeInfo();
  95. // #endif
  96. // #ifdef APP-PLUS
  97. this.getLocationInfoWx();
  98. // #endif
  99. // #ifdef H5
  100. this.initAMap()
  101. // #endif
  102. },
  103. getAuthorizeInfo(){
  104. //1. uniapp弹窗弹出获取授权(地理,个人微信信息等授权信息)弹窗
  105. var _this=this;
  106. uni.authorize({
  107. scope: "scope.userLocation",
  108. success() { //1.1 允许授权
  109. _this.getLocationInfoWx();
  110. },
  111. fail(){ //1.2 拒绝授权
  112. console.log("你拒绝了授权,无法获得周边信息")
  113. _this.openConfirm();
  114. }
  115. })
  116. },
  117. getLocationInfoWx(){
  118. var that=this;
  119. this.amapPlugin.getRegeo({
  120. type: 'gcj02', //map 组件使用的经纬度是国测局坐标, type 为 gcj02
  121. success: function(res) {
  122. console.log("success",res);
  123. that.latitude = res[0].latitude;
  124. that.longitude = res[0].longitude;
  125. that.address = res[0].name + res[0].desc;
  126. that.distance=geoDistance(that.longitude, that.latitude,that.compLongitude,that.compLatitude)
  127. console.log("that.distance",that.distance);
  128. let tipText=(that.distance>80?"未在":"已在")+"打卡范围内";
  129. let bgColor=that.distance>80?"#ff0000":"#00c16f";
  130. let marker={
  131. id:0,
  132.   latitude:that.latitude,//纬度
  133.   longitude:that.longitude,//经度
  134. iconPath: '/static/location.png',
  135. width:35,
  136. height:35,
  137. // #ifdef MP-WEIXIN
  138. label:{//为标记点旁边增加标签
  139.    content:tipText,//文本
  140.     color:'#ffffff',//文本颜色
  141.   fontSize:14,//文字大小
  142. borderWidth:2,//边框宽度
  143. borderColor:bgColor,//边框颜色
  144. bgColor:bgColor,//背景颜色
  145.   borderRadius:2,//边框圆角
  146.   padding:5,//文本边缘留白
  147. textAlign:'center',//文本对齐方式
  148. x:0,//label的坐标,原点是 marker 对应的经纬度
  149. y:0,//label的坐标,原点是 marker 对应的经纬度
  150. },
  151. // #endif
  152. // #ifdef APP-PLUS
  153. callout:{//自定义标记点上方的气泡窗口 点击有效
  154.   content:tipText,//文本
  155.   color:'#ffffff',//文字颜色
  156.   fontSize:14,//文本大小
  157.   //borderRadius:2,//边框圆角
  158.    bgColor:bgColor,//背景颜色
  159.   display:'ALWAYS',//常显
  160. textAlign:'center'
  161. },
  162. // #endif
  163. }
  164. that.marker=[marker];
  165. },
  166. fail: (res) => {
  167. console.log(JSON.stringify(res));
  168. }
  169. });
  170. },
  171. getLocationInfo() {
  172. var _this=this;
  173. uni.showLoading({
  174. title: '获取信息中',
  175. mask:true
  176. });
  177. uni.getLocation({
  178. //type: 'wgs84',
  179. type:'gcj02',
  180. success: function (res) {
  181. console.log('当前位置的经度:' + res.longitude);
  182. console.log('当前位置的纬度:' + res.latitude);
  183. _this.distance=geoDistance(res.longitude, res.latitude,_this.compLongitude,_this.compLatitude)
  184. let tipText=(_this.distance>80?"未在":"已在")+"打卡范围内";
  185. let bgColor=_this.distance>80?"#ff0000":"#00c16f";
  186. _this.longitude=res.longitude
  187. _this.latitude=res.latitude
  188. let marker={
  189.   latitude: res.latitude,//纬度
  190.   longitude:res.longitude,//经度
  191.   callout:{//自定义标记点上方的气泡窗口 点击有效
  192.   content:tipText,//文本
  193.   color:'#ffffff',//文字颜色
  194.   fontSize:14,//文本大小
  195.   borderRadius:2,//边框圆角
  196.    bgColor:bgColor,//背景颜色
  197.   display:'ALWAYS'//常显
  198. }
  199. }
  200. _this.marker=[marker];
  201. },
  202. fail: function (res){
  203. console.log('getLocation==> fail:' + res);
  204. console.log(res);
  205. }
  206. });
  207. uni.hideLoading();
  208. },
  209. // 当用户第一次拒绝后再次请求授权
  210. openConfirm(){
  211. uni.showModal({
  212. title: '请求授权当前位置',
  213. content: '需要获取您的地理位置,请确认授权',
  214. success: (res)=> {
  215. if (res.confirm) {
  216. uni.openSetting();// 打开地图权限设置
  217. } else if (res.cancel) {
  218. uni.showToast({
  219. title: '你拒绝了授权,无法获得位置信息',
  220. icon: 'none',
  221. duration: 1000
  222. })
  223. }
  224. }
  225. });
  226. },
  227. // 根据坐标返回地址(逆地理编码)
  228. /* async getAddress (points) {
  229. try {
  230. this.resAmap = await AMap();
  231. this.$nextTick(function() {
  232. this.resAmap.plugin('AMap.Geocoder', () => {
  233. var geocoder = new this.resAmap.Geocoder({
  234. radius: 1000,
  235. });
  236. geocoder.getAddress(points, (status, result) => {
  237. if (status === 'complete' && result.regeocode) {
  238. this.address = result.regeocode.formattedAddress
  239. }
  240. })
  241. });
  242. })
  243. } catch (e) {
  244. console.log(e)
  245. }
  246. }, */
  247. // #ifdef H5
  248. async initAMap() {
  249. try {
  250. uni.showLoading({
  251. title: '定位中...',
  252. mask:true
  253. });
  254. this.resAmap = await AMap();
  255. this.$nextTick(function() {
  256. this.resAmap.plugin('AMap.Geolocation', () => {
  257. var geolocation = new this.resAmap.Geolocation({
  258. enableHighAccuracy: true, //是否使用高精度定位,默认:true
  259. timeout: 10000, //超过10秒后停止定位,默认:5s
  260. buttonPosition: 'RB', //定位按钮的停靠位置
  261. // buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
  262. zoomToAccuracy: true, //定位成功后是否自动调整地图视野到定位点
  263. });
  264. geolocation.getCurrentPosition(function(status, result) {
  265. if (status == 'complete') {
  266. onComplete(result)
  267. } else {
  268. onError(result)
  269. }
  270. });
  271. });
  272. //解析定位结果
  273. var _this = this;
  274. function onComplete(data) {
  275. console.log("H5高德定位",data)
  276. console.log('当前位置的经度:' + data.position.lat);
  277. console.log('当前位置的纬度:' + data.position.lng);
  278. _this.distance=geoDistance(data.position.lng, data.position.lat,_this.compLongitude,_this.compLatitude)
  279. let tipText=(_this.distance>80?"未在":"已在")+"打卡范围内";
  280. let bgColor=_this.distance>80?"#ff0000":"#00c16f";
  281. _this.longitude=data.position.lng
  282. _this.latitude=data.position.lat
  283. _this.address=data.formattedAddress
  284. let marker={
  285.   latitude: _this.latitude,//纬度
  286.   longitude:_this.longitude,//经度
  287.   callout:{//自定义标记点上方的气泡窗口 点击有效
  288.   content:tipText,//文本
  289.   color:'#ffffff',//文字颜色
  290.   fontSize:14,//文本大小
  291.   borderRadius:2,//边框圆角
  292.    bgColor:bgColor,//背景颜色
  293.   display:'ALWAYS'//常显
  294. }
  295. }
  296. _this.marker=[marker];
  297. uni.hideLoading();
  298. _this.$tip.success("定位成功")
  299. }
  300. function onError(data) {
  301. console.log(data) // 定位失败的信息
  302. }
  303. })
  304. } catch (e) {
  305. console.log(e)
  306. _this.$tip.alert("定位失败")
  307. }
  308. },
  309. // #endif
  310. }
  311. }
  312. </script>
  313. <style>
  314. </style>