device-position.vue
2.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<template>
<view class="map">
<map
id="my-map"
ref="my-map"
class="my-map"
:style="{ height: allScreen - 44 + 'px' }"
:latitude="myLatitude"
:longitude="myLongitude"
:markers="makers"
enable-3D="true"
show-compass="true"
show-location="true"
></map>
</view>
</template>
<script>
const amapFile = require('@/config/amap-wx.js');
import { gdMapRequiredKey } from '@/config/gdMapConfig.js';
export default {
data() {
return {
mapContext: null,
allScreen: '',
myLatitude: 0, //纬度
myLongitude: 0, //经度
makers: [
{
id: 1,
latitude: 0,
longitude: 0,
iconPath: '../../static/location.png',
width: '50px',
height: '50px'
}
]
};
},
onLoad(e) {
let params = null;
if (e.data !== null) {
params = JSON.parse(e.data);
}
let that = this;
var myAmapFun = new amapFile.AMapWX({ key: gdMapRequiredKey });
that.getMyLocation(params.longitude, params.latitude);
uni.getSystemInfo({
success: function(e) {
that.allScreen = e.screenHeight;
}
});
},
onReady: function(res) {
this.mapContext = uni.createMapContext('my-map', this);
},
methods: {
getMyLocation(eLong, eLat) {
let that = this;
const newPos = this.bd_decrypt(eLong, eLat);
that.myLatitude = newPos.lat;
that.myLongitude = newPos.lng;
that.makers[0].longitude = newPos.lng;
that.makers[0].latitude = newPos.lat;
if (newPos.lng == undefined || newPos.lat == undefined) {
//如果没有位置信息则使用当前定位
getLocationFunc();
}
const getLocationFunc = () =>
uni.getLocation({
type: 'gcj02',
success: function(res) {
that.myLatitude = res.latitude;
that.myLongitude = res.longitude;
}
});
},
//服务端返回的是百度地图坐标系,百度地图坐标系转换为高德地图坐标系(重要,不然会造成位置误差)
bd_decrypt(bd_lng, bd_lat) {
var X_PI = (Math.PI * 3000.0) / 180.0;
var x = bd_lng - 0.0065;
var y = bd_lat - 0.006;
var z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * X_PI);
var theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * X_PI);
var gg_lng = z * Math.cos(theta);
var gg_lat = z * Math.sin(theta);
return { lng: gg_lng, lat: gg_lat };
}
}
};
</script>
<style scoped>
.my-map {
width: 750rpx;
}
</style>