myMap.js
4.72 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
Component({
/**
* 组件的属性列表
*/
options: {
multipleSlots: true
},
properties: {
markers: Array,
polyline: Array,
isLoading: Boolean,
includePoints: Array, // 当前页面是否有marker或路径的左边点
mapId: String
},
/**
* 组件的初始数据
*/
data: {
currentLo: 116.363874, // 地图中心点坐标
currentLa: 39.738269,
latitude: null, // 定位坐标
longitude: null,
isAuthLocation: false,
enableSatellite: false,
enableTraffic: false,
carInfoWindowObj: {},
isGPS: true,
scale: 16,
isCanClick: true,
inPoints: []
},
ready() {
},
attached() {
},
/**
* 组件的方法列表
*/
methods: {
getMapCtx() {
if (this.mapCtx) {
return this.mapCtx
}
// 创建 map 上下文 MapContext 对象
this.mapCtx = wx.createMapContext(this.data.mapId, this)
return this.mapCtx
},
bindmarkertap(e) {
this.triggerEvent('getCarInfoWindowObj', e)
},
// 刷新
refreshClick() {
this.triggerEvent('refresh')
},
// 点击地图
mapClick(e) {
this.triggerEvent('mapCallback', e)
},
// 移动地图
moveToLocation: function (latitude, longitude ) {
this.getMapCtx().moveToLocation({
longitude: longitude,
latitude: latitude
})
},
// 图层缩放控制
layerClick(e) {
switch(Number(e.currentTarget.dataset.num)) {
case 1:
this.setData({
enableSatellite: !this.data.enableSatellite,
enableTraffic: this.data.enableTraffic
})
break
case 2:
this.setData({
enableSatellite: this.data.enableSatellite,
enableTraffic: !this.data.enableTraffic
})
break
case 3:
this.getMapCtx().getScale({
success: (res) => {
this.getMapCtx().getCenterLocation({
success: (e) => {
this.setData({
currentLo: e.longitude,
currentLa: e.latitude,
scale: res.scale >= 20 ? 20 : res.scale+1
})
}
})
}
})
break
case 4:
this.getMapCtx().getScale({
success: (res) => {
this.getMapCtx().getCenterLocation({
success: (e) => {
this.setData({
currentLo: e.longitude,
currentLa: e.latitude,
scale: res.scale <= 3 ? 3 : res.scale-1
})
}
})
}
})
break
}
},
// 定位
locateClick(e) {
let _this = this
if (!this.data.isCanClick) return
this.setData({
isCanClick: false
})
setTimeout(() => {
this.setData({
isCanClick: true
})
}, 30*1000)
if (_this.data.isAuthLocation) {
_this.getLocation()
} else {
wx.getSetting({
success: (res) => {
let authSetting = res.authSetting
if (authSetting['scope.userLocation']) {
// 已授权
_this.getLocation()
_this.setData({
isAuthLocation: true
})
} else {
if (typeof(authSetting['scope.userLocation']) !== 'undefined') {
wx.showModal({
title: '您未开启地理位置授权',
content: '点击确定去开启地理位置授权',
success: res => {
if (res.confirm) {
wx.openSetting()
}
}
})
} else {
_this.getLocation()
}
}
}
})
}
},
getLocation() {
let _this = this
wx.getLocation({
type: 'gcj02',
success (res) {
_this.setData({
longitude: res.longitude,
latitude: res.latitude
})
if (_this.data.includePoints.length > 0) { // 包含其他坐标点 不需将获取到的坐标移动到地图中心
_this.triggerEvent('locationCallback', {lat: res.latitude, lng: res.longitude})
} else {
_this.moveToLocation(res.latitude, res.longitude)
}
},
fail () {
if (_this.data.includePoints.length > 0) { // 包含其他坐标点 不需将获取到的坐标移动到地图中心
_this.triggerEvent('locationCallback', {lat: _this.data.latitude, lng: _this.data.longitude})
} else {
_this.moveToLocation(_this.data.latitude, _this.data.longitude)
}
}
})
}
},
})