-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
336 lines (301 loc) · 13.4 KB
/
index.html
File metadata and controls
336 lines (301 loc) · 13.4 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
<html lang="cn">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, minimum-scale=1, user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>附近</title>
<script src="https://cdn.bootcss.com/zepto/1.2.0/zepto.min.js"></script>
<script src="https://webapi.amap.com/maps?v=1.4.12&key=f6e50ee40dee1818965b7007eb8d2303"></script>
<style>
* {
box-sizing: border-box;
}
html,body {
width: 100%;
height: 100%;
margin: 0;
padding: 0;
}
.main {
width: 100%;
height: 100%;
display: flex;
position: relative;
flex-direction: column;
}
.main .search-bar {
width: 100%;
height: 56px;
background: #ededed;
}
.main .search-bar .input-wrapper {
width: 100%;
padding: 10px 8px;
}
.main .search-bar .input-wrapper input {
width: 100%;
height: 36px;
border: 1px solid #e5e5e5;
border-radius: 5px;
font-size: 16px;
text-align: center;
outline: none;
-webkit-appearance: none;
appearance: none;
}
.main .map-wrapper {
width: 100%;
height: 369px;
transition: height .3s ease-in-out;
}
.main .map-wrapper.mini {
width: 100%;
height: 122px;
}
.main .map-wrapper #map {
width: 100%;
height: 100%;
}
.main .poi-nearby {
width: 100%;
flex: 1;
padding: 4px 16px;
/* 这样滑动会更流畅,但也会带来滚动事件的延迟 */
overflow-y: scroll;
-webkit-overflow-scrolling: touch;
}
.main .poi-nearby .info {
width: 100%;
height: 48px;
padding: 4px 0;
border-bottom: 1px solid #f2f2f2;
}
.main .poi-nearby .info .name,
.main .poi-nearby .info .address {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.main .poi-nearby .info .name {
color: #000000;
font-size: 18px;
}
.main .poi-nearby .info .address {
color: #b2b2b2;
font-size: 12px;
}
.main .mask {
display: none;
position: absolute;
top: 56px;
left: 0;
width: 100%;
height: 100%;
opacity: 0.6;
background: #000000;
}
.main.mask .mask {
display: block;
}
.amap-logo {
display: none !important;
}
.amap-copyright {
display: none !important;
}
#submap {
position: relative;
width: 100%;
height: 35vh;
}
.amap-sug-result {
right: 0px;
height: 100%;
left: 0px !important;
padding: 10px 0;
}
</style>
</head>
<body>
<div id="mainWrapper" class="main">
<div class="search-bar">
<div class="input-wrapper">
<input id="searchNearby" type="text" name="searchFor" placeholder="搜索地点">
</div>
</div>
<div id="mapWrapper" class="map-wrapper">
<div id="map"></div>
</div>
<div id="poiWrapper" class="poi-nearby">
<div id="poiInfos"></div>
</div>
<div class="mask"></div>
</div>
<script>
(function () {
var $mainWrapper = $('#mainWrapper');
var $mapWrapper = $('#mapWrapper');
var $poiInfos = $('#poiInfos');
var $poiWrapper = $('#poiWrapper');
var poiWrapperLastTop = 0;
var currentCity;
var placeSearch;
var currentPage, totalPage, nextPageLoading = false;
// 遮罩
$('#searchNearby').on('focus', function () {
$mainWrapper.addClass('mask');
});
$('#searchNearby').on('blur', function () {
$mainWrapper.removeClass('mask');
});
$poiWrapper.on('scroll', function (e) {
if (e.target.scrollTop <= 0) {
$mapWrapper.removeClass('mini');
} else if (e.target.scrollTop > 5 && poiWrapperLastTop <= 5) {
$mapWrapper.addClass('mini');
}
if (!nextPageLoading && currentPage < totalPage && e.target.scrollTop + e.target.clientHeight >= $poiInfos[0].clientHeight - 20) {
nextPageLoading = true;
amapSearchNearBy(map.getCenter(), currentPage + 1);
}
poiWrapperLastTop = e.target.scrollTop;
})
// 地图加载
var map = new AMap.Map('map', {
resizeEnable: true, //是否监控地图容器尺寸变化
zoom: 11, //初始化地图层级
// center: [116.397428, 39.90923] //初始化地图中心点
});
map.on('mapmove', function (e) {
marker.setPosition(map.getCenter());
})
map.on('moveend', function () {
// 只是为了好看= =
marker.setAnimation('AMAP_ANIMATION_DROP');
marker.setPosition(map.getCenter());
amapSearchNearBy(map.getCenter());
});
map.on('complete', function () {
amapSearchNearBy(map.getCenter());
initAutocomplete();
});
map.plugin('AMap.Geolocation', function () {
var geolocation = new AMap.Geolocation({
enableHighAccuracy: true,//是否使用高精度定位,默认:true
timeout: 5000, //超过10秒后停止定位,默认:无穷大
maximumAge: 0, //定位结果缓存0毫秒,默认:0
convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
showButton: true, //显示定位按钮,默认:true
buttonPosition: 'RB', //定位按钮停靠位置,默认:'LB',左下角
buttonOffset: new AMap.Pixel(10, 20),//定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
showCircle: true, //定位成功后用圆圈表示定位精度范围,默认:true
panToLocation: true, //定位成功后将定位到的位置作为地图中心点,默认:true
zoomToAccuracy:true //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
});
map.addControl(geolocation);
geolocation.getCurrentPosition()
AMap.event.addListener(geolocation, 'complete', onComplete)
AMap.event.addListener(geolocation, 'error', onError)
function onComplete(data) {
// data是具体的定位信息
refreshCenter(data.position);
map.setZoom(14);
}
function onError(data) {
// 定位出错
console.error(data.message);
}
});
function refreshCenter(loc) {
map.setCenter(loc);
marker.setPosition(loc);
map.getCity(function (data) {
currentCity = data;
placeSearch && placeSearch.setCity(currentCity.citycode);
});
}
// 更新周边列表
function refreshNearBy(poiList, firstPage) {
var tmpl =
'<div class="info"><div class="name">{{name}}</div><div class="address">{{address}}</div></div>';
// 首页清空
if (firstPage) {
$poiInfos.empty();
}
for (var i = 0; i < poiList.length; i++) {
var poi = poiList[i];
var html = tmpl.replace(/{{([\w]+)}}/g, function ($0, $1) {
return poi[$1] || '';
});
var $dom = $(html);
$dom.data('poi', JSON.stringify(poi));
$dom.on('click', function (e) {
var $this = $(this);
var loc = $this.data('poi').location;
refreshCenter(loc);
});
$poiInfos.append($dom);
}
}
// 查询周边
function amapSearchNearBy(centerPoint, pageIndex) {
pageIndex = pageIndex || 1;
AMap.service(["AMap.PlaceSearch"], function () {
var placeSearch = new AMap.PlaceSearch({
pageSize: 25, // 每页10条
pageIndex: pageIndex, // 获取第一页
// 汽车服务|汽车销售|汽车维修|摩托车服务|餐饮服务|购物服务|生活服务|体育休闲服务|医疗保健服务|住宿服务|风景名胜|商务住宅|政府机构及社会团体|科教文化服务|交通设施服务|金融保险服务|公司企业|道路附属设施|地名地址信息|公共设施
// type: '酒店|电影院|医院|学校'
city: currentCity ? currentCity.citycode : '' // 指定城市名(如果你获取不到城市名称,这个参数也可以不传,注释掉)
});
placeSearch.searchNearBy('', centerPoint, 1500, function (status, result) {
nextPageLoading = false;
if (result.info === 'OK') {
if (pageIndex === 1) {
totalPage = Math.ceil(result.poiList.count / result.poiList.pageSize);
}
currentPage = result.poiList.pageIndex;
console.log(result.poiList.pois);
refreshNearBy(result.poiList.pois, pageIndex === 1);
} else {
console.log('获取位置信息失败!');
}
});
});
}
// 地图上的点
var marker = new AMap.Marker({
position: map.getCenter(),
// 自己画的图标哦
icon: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAABS2lUWHRYTUw6Y29tLmFkb2JlLnhtcAAAAAAAPD94cGFja2V0IGJlZ2luPSLvu78iIGlkPSJXNU0wTXBDZWhpSHpyZVN6TlRjemtjOWQiPz4KPHg6eG1wbWV0YSB4bWxuczp4PSJhZG9iZTpuczptZXRhLyIgeDp4bXB0az0iQWRvYmUgWE1QIENvcmUgNS42LWMxNDAgNzkuMTYwNDUxLCAyMDE3LzA1LzA2LTAxOjA4OjIxICAgICAgICAiPgogPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4KICA8cmRmOkRlc2NyaXB0aW9uIHJkZjphYm91dD0iIi8+CiA8L3JkZjpSREY+CjwveDp4bXBtZXRhPgo8P3hwYWNrZXQgZW5kPSJyIj8+LUNEtwAAAgtJREFUWIXtlj9v00AYxn+xk8gkoYZChE27IBB4QFlgYWBmQQzAHokRUSH4BPkIqCAGJJbOaQYoA9+BKSwZIiEGkiiF1M2fi7FjH0Ma1EohuYBLlzzT+b1Hz/10enWv4YSVUDH1L9q/17VW8zzwHLgLXJ2UgXfApmPZ3yfeXKMZL0Ct1XwAvDVPZUwzm8HQkwB4oY8rBF3hdYFHjmVvxw5QazXva1qivHZmNZFNp6f7fJ+G25FRJB86ll2JDeCTRh6or6+eXcmljZnenufxzd3rAlduRuzOy9ZUAICNnJGeezjAacMgZ6RXgGcqwaoA98xsVtEKppGFcZPGBuBktJQyQCaVArgWJwBaUlcG0HUdwI8ToBaNQmWAMAwBvsQJsCOiQBlABAHATpwAm/sDT6gCdIeeAF7EBuBYdrvvDYt935vr7QUevZ/DomPZ7dgADiDKu71ecTDy/9gMA98ftd39omPZZdVcZQCAS+fyW40f7lpHiA/DIBCRlERSMgyCQUeI9409d/1y/sLWIpkLT8PD+vjylQS4s/Fkao7KLEiqANRas4Pm7f8zwI1QTq1/rVRm7pOYf8EL9cBxaAmwBFgCLAGUZoGU45euVCodqRcKBQlQrVaP5Ex8iWN8CW8DlUPf2we1haU0C6boDWAKIUI5vp5bjP+Cr/8vgKfA43q9PjnwM/D6L7NOVr8ANGi9B/+lsSUAAAAASUVORK5CYII=',
draggable: false, // 不允许拖拽图标
cursor: 'move',
zIndex: 110,
// 设置拖拽效果
// raiseOnDrag: true
});
map.add(marker);
function initAutocomplete() {
if (!AMap.Autocomplete || !AMap.PlaceSearch) return;
var autoOptions = {
city: currentCity ? currentCity.citycode : '', //城市,默认全国
input: "searchNearby", //使用联想输入的input的id
children: "1"
};
var autocomplete = new AMap.Autocomplete(autoOptions);
placeSearch = new AMap.PlaceSearch({
map: map
}); //构造地点查询类
AMap.event.addListener(autocomplete, "select", function (e) {
refreshCenter(e.poi.location);
//TODO 选择后的处理程序,data的格式见 附录
placeSearch.setCity(e.poi.adcode);
// placeSearch.search(e.poi.name); //关键字查询查询
});
}
AMap.plugin(['AMap.Autocomplete', 'AMap.PlaceSearch'], initAutocomplete);
})();
</script>
</body>
</html>