map.html
5.0 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
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
<style type="text/css">
body, html, #allmap {
width: 100%;
height: 100%;
overflow: hidden;
margin: 0;
}
#suggestId {
position: absolute;
z-index: 9999;
right: 20px;
top: 20px;
line-height: 32px;
padding: 0px 10px;
width: 300px;
border: 1px solid #e4e6e7;
}
#suggestId:focus {
outline: none;
}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=KxkuAcGBup6sD1XxaDW85KBG"></script>
<title>点击地图获取当前经纬度</title>
</head>
<body>
<input id="lng_input" type="hidden" value="{$lng}"/>
<input id="lat_input" type="hidden" value="{$lat}"/>
<input id="address_input" type="hidden"/>
<input id="city_input" type="hidden"/>
<input id="province_input" type="hidden"/>
<input id="district_input" type="hidden"/>
<input id="suggestId" type="text" placeholder="请输入地址"/>
<div id="allmap"></div>
<script type="text/javascript">
// 百度地图API功能
var center = new BMap.Point({$lng}, {$lat});
var map = new BMap.Map("allmap");
map.centerAndZoom(new BMap.Point(116.404, 39.915), 8);
map.enableScrollWheelZoom(); //启用滚轮放大缩小,默认禁用
map.enableContinuousZoom();
var map_ac = new BMap.Autocomplete( //建立一个自动完成的对象
{
"input": "suggestId",
"location": map
});
var marker = new BMap.Marker(center); // 创建标注
map.addOverlay(marker); // 将标注添加到地图中
marker.enableDragging(); //可拖拽
//marker.setAnimation(BMAP_ANIMATION_BOUNCE);
map_ac.addEventListener("onconfirm", function (e) { //鼠标点击下拉列表后的事件
var _value = e.item.value;
function myFun() {
var pp = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
map.centerAndZoom(pp, 8);
marker.setPosition(pp);
mgetLocation(pp);
}
myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
var local = new BMap.LocalSearch(map, { //智能搜索
onSearchComplete: myFun
});
local.search(myValue);
});
var opts = {
width: 200, // 信息窗口宽度
height: 30, // 信息窗口高度
title: "", // 信息窗口标题
enableMessage: false,//设置允许信息窗发送短息
}
var infoWindow = new BMap.InfoWindow("拖动我设置你的位置", opts); // 创建信息窗口对象
marker.openInfoWindow(infoWindow);
var gc = new BMap.Geocoder();
function msetpoint(e) {
document.getElementById("lng_input").value = e.point.lng;
document.getElementById("lat_input").value = e.point.lat;
marker.setPosition(new BMap.Point(e.point.lng, e.point.lat));
mgetLocation(e.point);
}
function mgetLocation(point) {
document.getElementById("lng_input").value = point.lng;
document.getElementById("lat_input").value = point.lat;
gc.getLocation(point, function (rs) {
var addComp = rs.addressComponents;
marker.openInfoWindow(new BMap.InfoWindow(rs.address, opts));
document.getElementById("address_input").value = rs.address;
document.getElementById("city_input").value = addComp.city;
document.getElementById("province_input").value = addComp.province;
document.getElementById("district_input").value = addComp.district;
//alert(addComp.province + ", " + addComp.city + ", " + addComp.district + ", " + addComp.street + ", " + addComp.streetNumber);
});
}
marker.addEventListener("dragend", msetpoint)
map.addEventListener("click", msetpoint);
function tilesloaded() {
map.setCenter(center); //设置地图中心点。center除了可以为坐标点以外,还支持城市名
mgetLocation(center);
// map.setZoom(8); //将视图切换到指定的缩放等级,中心点坐标不变
map.removeEventListener("tilesloaded", tilesloaded);
}
map.addEventListener("tilesloaded", tilesloaded);
function confirm() {
var data = {};
data.lng = document.getElementById("lng_input").value;
data.lat = document.getElementById("lat_input").value;
data.address = document.getElementById("address_input").value;
data.city = document.getElementById("city_input").value;
data.province = document.getElementById("province_input").value;
data.district = document.getElementById("district_input").value;
return data;
}
</script>
</body>
</html>