123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326 |
- <template>
- <view>
- <map
- style="width: 100%; height:250px;"
- :latitude="latitude"
- :longitude="longitude"
- :markers="marker"
- :scale="scale"
- :circles="circles"
- >
-
- </map>
- </view>
- </template>
- <script>
- import { geoDistance } from '@/common/util/util.js'
- import amap from "@/common/js-sdk/js-amap/amap-wx.js";
-
- import AMap from "@/common/js-sdk/js-amap/amap-h5.js";
-
-
- export default {
- props:{
- compLatitude:{
- type:Number,
- default:40.009390,
- required:false
- },
- compLongitude:{
- type:Number,
- default:116.374322,
- required:false
- }
- },
- data() {
- return {
- amapPlugin:null,
- wxMapKey:"53324ee357405c4a65f35a1aa05ffaf2",
- id:0,
- title: 'map',
- distance:0,
- address:"",
- latitude: this.compLatitude,
- longitude: this.compLongitude,
- scale:16,
- tipText:'打卡范围',
- bgColor:'#00c16f',
- marker: [],
- circles:[{
- latitude: this.compLatitude,
- longitude: this.compLongitude,
- radius:80,
- fillColor:"#ffffffAA",
- color:"#55aaffAA",
- strokeWidth:1
- }],
- resAmap:null
- }
- },
- created() {
-
- this.amapPlugin = new amap.AMapWX({
- key: this.wxMapKey
- });
-
-
-
- this.initAMap()
-
- },
- mounted() {
-
- this.getAuthorizeInfo();
-
-
- this.getLocationInfoWx();
-
-
-
-
- },
- computed:{
- inCircle(){
- return this.address && this.distance <= 80
- }
- },
- methods: {
- allowed(){
- return this.inCircle
- },
- getMyAddress(){
- return this.address
- },
- refreshLocation(){
-
- this.getAuthorizeInfo();
-
-
- this.getLocationInfoWx();
-
-
- this.initAMap()
-
- },
- getAuthorizeInfo(){
-
- var _this=this;
- uni.authorize({
- scope: "scope.userLocation",
- success() {
- _this.getLocationInfoWx();
- },
- fail(){
- console.log("你拒绝了授权,无法获得周边信息")
- _this.openConfirm();
- }
- })
- },
- getLocationInfoWx(){
- var that=this;
- this.amapPlugin.getRegeo({
- type: 'gcj02',
- success: function(res) {
- console.log("success",res);
- that.latitude = res[0].latitude;
- that.longitude = res[0].longitude;
- that.address = res[0].name + res[0].desc;
- that.distance=geoDistance(that.longitude, that.latitude,that.compLongitude,that.compLatitude)
- console.log("that.distance",that.distance);
- let tipText=(that.distance>80?"未在":"已在")+"打卡范围内";
- let bgColor=that.distance>80?"#ff0000":"#00c16f";
- let marker={
- id:0,
- latitude:that.latitude,
- longitude:that.longitude,
- iconPath: '/static/location.png',
- width:35,
- height:35,
-
- label:{
- content:tipText,
- color:'#ffffff',
- fontSize:14,
- borderWidth:2,
- borderColor:bgColor,
- bgColor:bgColor,
- borderRadius:2,
- padding:5,
- textAlign:'center',
- x:0,
- y:0,
- },
-
-
- callout:{
- content:tipText,
- color:'#ffffff',
- fontSize:14,
-
- bgColor:bgColor,
- display:'ALWAYS',
- textAlign:'center'
- },
-
- }
- that.marker=[marker];
- },
- fail: (res) => {
- console.log(JSON.stringify(res));
- }
- });
- },
- getLocationInfo() {
- var _this=this;
- uni.showLoading({
- title: '获取信息中',
- mask:true
- });
- uni.getLocation({
-
- type:'gcj02',
- success: function (res) {
- console.log('当前位置的经度:' + res.longitude);
- console.log('当前位置的纬度:' + res.latitude);
- _this.distance=geoDistance(res.longitude, res.latitude,_this.compLongitude,_this.compLatitude)
- let tipText=(_this.distance>80?"未在":"已在")+"打卡范围内";
- let bgColor=_this.distance>80?"#ff0000":"#00c16f";
- _this.longitude=res.longitude
- _this.latitude=res.latitude
- let marker={
- latitude: res.latitude,
- longitude:res.longitude,
- callout:{
- content:tipText,
- color:'#ffffff',
- fontSize:14,
- borderRadius:2,
- bgColor:bgColor,
- display:'ALWAYS'
- }
- }
- _this.marker=[marker];
- },
- fail: function (res){
- console.log('getLocation==> fail:' + res);
- console.log(res);
- }
- });
- uni.hideLoading();
- },
-
- openConfirm(){
- uni.showModal({
- title: '请求授权当前位置',
- content: '需要获取您的地理位置,请确认授权',
- success: (res)=> {
- if (res.confirm) {
- uni.openSetting();
- } else if (res.cancel) {
- uni.showToast({
- title: '你拒绝了授权,无法获得位置信息',
- icon: 'none',
- duration: 1000
- })
- }
- }
- });
- },
-
-
-
- async initAMap() {
- try {
- uni.showLoading({
- title: '定位中...',
- mask:true
- });
- this.resAmap = await AMap();
- this.$nextTick(function() {
- this.resAmap.plugin('AMap.Geolocation', () => {
- var geolocation = new this.resAmap.Geolocation({
- enableHighAccuracy: true,
- timeout: 10000,
- buttonPosition: 'RB',
-
- zoomToAccuracy: true,
-
- });
- geolocation.getCurrentPosition(function(status, result) {
- if (status == 'complete') {
- onComplete(result)
- } else {
- onError(result)
- }
- });
-
- });
-
-
- var _this = this;
-
- function onComplete(data) {
- console.log("H5高德定位",data)
- console.log('当前位置的经度:' + data.position.lat);
- console.log('当前位置的纬度:' + data.position.lng);
- _this.distance=geoDistance(data.position.lng, data.position.lat,_this.compLongitude,_this.compLatitude)
- let tipText=(_this.distance>80?"未在":"已在")+"打卡范围内";
- let bgColor=_this.distance>80?"#ff0000":"#00c16f";
- _this.longitude=data.position.lng
- _this.latitude=data.position.lat
- _this.address=data.formattedAddress
- let marker={
- latitude: _this.latitude,
- longitude:_this.longitude,
- callout:{
- content:tipText,
- color:'#ffffff',
- fontSize:14,
- borderRadius:2,
- bgColor:bgColor,
- display:'ALWAYS'
- }
- }
- _this.marker=[marker];
- uni.hideLoading();
- _this.$tip.success("定位成功")
- }
-
- function onError(data) {
- console.log(data)
- }
-
- })
- } catch (e) {
- console.log(e)
- _this.$tip.alert("定位失败")
- }
- },
-
-
- }
- }
- </script>
- <style>
- </style>
|