map.html
12.3 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="utf-8" />
<meta content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="广联帐户管理中心-地图选择" />
<meta name="author" content="GLSX Web Team" />
<meta name="keywords" itemprop="keywords" content="广联,帐户管理,商户管理" />
<link rel="stylesheet" type="text/css" href="/css/main.css" />
<style>
.map-box {
padding: 34px 42px 22px;
background-color: #ffffff;
}
#container {
height: 100%;
width: 100%;
text-align: center;
}
.map-box .poi-list {
width: calc(100% - 420px);
height: 300px;
margin: 0;
color: #676a6c;
overflow-y: scroll;
}
.map-box .poi-list .item {
font-size: 14px;
padding: 5px 10px;
cursor: pointer;
}
.map-box .poi-list .item.active,
.map-box .poi-list .item.active .addr {
color: #00a479;
}
.poi-list .item .name {
font-weight: 700;
}
.poi-list .item .addr {
font-size: 12px;
color: #ccc;
margin-top: 2px;
}
</style>
<title>地图选择</title>
</head>
<body>
<div id="app">
<main>
<div class="map-box">
<form class="layui-form">
<div class="layui-form-item">
<div class="layui-input-inline" style="width: calc(100% - 58px); margin: 0;">
<input id="addrEl" name="searchAddr" class="layui-input" placeholder="请输入地点名称">
</div>
<button type="button" class="layui-btn" data-type="handleSearchAddr">
<i class="layui-icon layui-icon-search"></i>
</button>
</div>
<div class="layui-form-item">
<div class="layui-input-inline" style="width: 100%; height: 300px;">
<div id="container"></div>
</div>
<!-- <div class="poi-list layui-input-inline poi-list" id="pois"></div> -->
</div>
<div class="layui-form-item">
<input name="merchantAddress" id="merchantAddress" class="layui-input" placeholder="请输入详细地址,如道路、门牌号等">
</div>
<div class="layui-form-item">
<button id="mapBtn" style="display: none;" lay-submit lay-filter="confirmFilter"></button>
</div>
</form>
</div>
</main>
</div>
<script src="https://img02.glsx.com.cn/layui/layui.js"></script>
<script src="/js/load.js"></script>
<script type="text/javascript"
src="https://webapi.amap.com/maps?v=1.4.15&key=44cc16e43ad7ed3358df507bd0618181&plugin=AMap.Geocoder&plugin=AMap.PlaceSearch&plugin=AMap.Geolocation">
</script>
<script src="/js/glsk-mapapi-sdk.js"></script>
<script>
layui.use(['helper'], function () {
var form = layui.form
var $ = layui.$
var layer = layui.layer
var helper = layui.helper
var mapData = {}
var pois = []
var geocoder //根据当前marker 获取地址
var poiActiveIdx = -1
var map = null, mapapi = null, marker = null
// new GLMapSdk(async res => {
// mapapi = res
// map = await mapapi.invoke('Map', 'container')
// mapapi.invoke('centerAndZoom', mapapi.invoke('Point', 113.736716, 23.069466), 15)
// map.enableScrollWheelZoom(true)
// })
// var map = new AMap.Map("container", {
// resizeEnable: true
// })
// mapapi = new GlMap()
init()
console.log(GlMap, '===')
var btnHandle = {
handleSearchAddr: function () {
var addr = $('#addrEl').val()
getLocation(addr)
return false
}
}
$('.layui-btn').on('click', function () {
var type = $(this).data('type')
if (btnHandle[type]) {
btnHandle[type]()
}
})
form.on('submit(confirmFilter)', function (data) {
if (mapData.lat && data.field.merchantAddress) {
mapData.merchantAddress = data.field.merchantAddress
helper.store.set('merchantMapObj', mapData)
}
return false
})
// map.on('click', setLocation)
function init() {
mapData = helper.store.get('merchantMapObj')
// 初始化地图
new GlMap(async api => {
mapapi = api
map = await mapapi.invoke('Map', 'container')
mapapi.invoke('centerAndZoom', mapapi.invoke('Point', 113.736716, 23.069466), 15)
mapapi.invoke("mapAddEventListener",'click', setLocation)
map.enableScrollWheelZoom(true) // 启用滚轮放大缩小,默认禁用
if (mapData.lat && mapData.lng) {
const {lng, lat} = mapData
console.log(mapData, 'mapData')
const lngLatXY = getLngLat(mapData.lng, mapData.lat)
// __pos = [mapData.lng, mapData.lat]
modelVal('merchantAddress', mapData.merchantAddress)
marker = createMarker(lngLatXY)
mapapi.invoke('addOverlay',marker)
mapapi.invoke('panTo', lngLatXY)
} else {
mapapi.invoke('Geolocation', res => {
console.log(res, 'Geolocation')
const lngLatXY = getLngLat(res.longitude, res.latitude)
if (marker) mapapi.invoke('removeOverlay', marker)
marker = createMarker(lngLatXY)
mapapi.invoke('addOverlay', marker)
mapapi.invoke('panTo', lngLatXY)
})
}
})
}
function getLngLat(lng, lat) {
return mapapi.invoke('Point', lng, lat)
}
function createMarker(lngLatXY) {
const marker = mapapi.invoke('Marker', lngLatXY)
mapapi.invoke('markerEnableDragging')
return marker
}
function modelVal(id, val) {
$('#' + id).attr('value', val)
}
function setFitView() {
map.setFitView(marker)
}
function setLocation(e) {
var lnglat = e.point
mapData.lat = lnglat.lat
mapData.lng = lnglat.lng
if (!marker) {
marker = createMarker(lnglat)
mapapi.invoke('addOverlay',marker)
} else {
marker.setPosition(lnglat)
}
mapapi.invoke('panTo', lnglat)
getAddress()
}
// 添加地点列表
function appendList() {
$('#pois').empty()
$.each(pois, function (index, item) {
// console.log(item)
var el = $('<p></p>')
el.addClass('item')
el.data('targerItem', item)
el.data('idx', index)
el.bind('click', function (e) {
// console.log(e)
var activeEl = $('.poi-list .item.active')
if (activeEl.length) {
activeEl.removeClass('active')
}
$(this).addClass('active')
var targerItem = $(this).data('targerItem')
mapData.lat = targerItem.location.lat
mapData.lng = targerItem.location.lng
setPosition(targerItem)
})
$('#pois').append(el)
var child1 = $('<p class="name"></p>').text(item.name)
var child2 = $('<p class="addr"></p>').text(item.address)
el.append(child1)
el.append(child2)
})
}
// 提供某一特定地区的位置查询服务
function placeSearch(keywords) {
var autoOptions = {
city: "全国",
pageSize: 20,
autoFitView: true,
type: `汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施`
};
var placeSearch = new AMap.PlaceSearch(autoOptions)
pois = []
// var temp = $scope.model.address.addressComponent
// $scope.model.address.name = (temp.city || '') + (temp.district || '') + (temp.street || '') + (temp
// .township || '') + (temp.streetNumber || '')
// $scope.model.address.location = $scope.model.marker.getPosition()
placeSearch.search(
keywords,
function (status, result) {
// console.log(result)
// 搜索成功时,result即是对应的匹配数据
if (
result &&
result.poiList &&
result.poiList.pois &&
result.poiList.pois.length
) {
pois = result.poiList.pois
// var flag = false
} else { }
appendList()
setFitView()
}
);
}
function setPosition(targerItem) {
marker.setPosition(targerItem.location)
mapData.merchantAddress = targerItem.address
modelVal('merchantAddress', mapData.merchantAddress)
setFitView()
}
// 根据给定坐标进行解析 - 地图选点
function getAddress() {
if (!geocoder) {
geocoder = mapapi.invoke('Geocoder')
// geocoder = new AMap.Geocoder({
// // city: "010", //城市设为北京,默认:“全国”
// radius: 1000 //范围,默认:500
// })
}
mapapi.invoke('getLocation', marker.getPosition(), res => {
console.log(res)
if (res) {
mapData.merchantAddress = res.surroundingPois.length ? res.surroundingPois[0].address : res.address
$('#merchantAddress').val(mapData.merchantAddress)
// modelVal('merchantAddress', mapData.merchantAddress)
mapData.geocoderAddress = res
mapData.lat = res.point.lat
mapData.lng = res.point.lng
}
})
// geocoder.getAddress(marker.getPosition(), function (status, result) {
// if (status === "complete" && result.regeocode) {
// mapData.merchantAddress = result.regeocode.formattedAddress
// modelVal('merchantAddress', mapData.merchantAddress)
// placeSearch(mapData.merchantAddress)
// geocoder.getLocation(mapData.merchantAddress, (status, result) => {
// if (status === 'complete' && result.info === 'OK') {
// mapData.geocoderAddress = result.geocodes[0]
// }
// })
// }
// })
}
// 根据给定的地址进行解析 - 搜索地址
function getLocation(searchText) {
console.log(searchText)
if (!geocoder) {
geocoder = mapapi.invoke('Geocoder')
// geocoder = new AMap.Geocoder({
// // city: "010", //城市设为北京,默认:“全国”
// radius: 1000 //范围,默认:500
// })
}
mapapi.invoke('getPoint', searchText, res => {
console.log(res)
const lngLatXY = getLngLat(res.lng, res.lat)
if (!marker) {
marker = createMarker(lngLatXY)
mapapi.invoke('addOverlay', marker)
} else {
marker.setPosition(lngLatXY)
}
mapapi.invoke('panTo', lngLatXY)
getAddress()
}, '全国')
// geocoder.getLocation(searchText, function (status, result) {
// // console.log(result)
// if (status === "complete" && result.geocodes.length) {
// var lnglat = result.geocodes[0].location
// // var _result$geocodes$ = result.geocodes[0]
// // formattedAddress = _result$geocodes$.formattedAddress
// // addressComponent = _result$geocodes$.addressComponent
// mapData.lat = lnglat.lat
// mapData.lng = lnglat.lng
// mapData.merchantAddress = result.geocodes[0].formattedAddress
// // mapData.province = addressComponent.province || ""
// // mapData.city = addressComponent.city || ""
// // mapData.district = addressComponent.district || ""
// modelVal('merchantAddress', mapData.merchantAddress)
// marker.setPosition(lnglat)
// // address = _result$geocodes$
// setFitView()
// placeSearch(searchText)
// } else {
// layer.msg('未查询到当前地址,请尝试更换搜索关键字', {
// icon: 0
// })
// }
// })
}
})
</script>
</body>
</html>