map.html
10 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
<!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>
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 = new AMap.Map("container", {
resizeEnable: true
})
init()
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() {
var __pos = null
mapData = helper.store.get('merchantMapObj')
if (mapData.lat && mapData.lng) {
__pos = [mapData.lng, mapData.lat]
modelVal('merchantAddress', mapData.merchantAddress)
} else {
var geolocation = new AMap.Geolocation({})
geolocation.getCurrentPosition()
AMap.event.addListener(geolocation, 'complete', (e) => {
// console.log(e) // 定位成功之后做的事
__pos = [e.position.lng, e.position.lat]
mapData.lat = e.position.lat
mapData.lng = e.position.lng
mapData.merchantAddress = e.formattedAddress
modelVal('merchantAddress', mapData.merchantAddress)
})
}
marker = new AMap.Marker({
position: __pos,
// 设置是否可以拖拽
draggable: true,
cursor: "move" // 设置拖拽效果
})
map.add(marker)
setFitView()
}
function modelVal(id, val) {
$('#' + id).attr('value', val)
}
function setFitView() {
map.setFitView(marker)
}
function setLocation(e) {
// console.log(e)
var lnglat = e.lnglat
mapData.lat = e.lnglat.lat
mapData.lng = e.lnglat.lng
marker.setPosition(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 = new AMap.Geocoder({
// city: "010", //城市设为北京,默认:“全国”
radius: 1000 //范围,默认:500
})
}
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) {
if (!geocoder) {
geocoder = new AMap.Geocoder({
// city: "010", //城市设为北京,默认:“全国”
radius: 1000 //范围,默认:500
})
}
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>