<%--引入高德地图--%>
<link rel="stylesheet" href="https://cache.amap.com/lbs/static/main1119.css"/>
<script type="text/javascript"
src="https://webapi.amap.com/maps?v=1.4.9&key=相应的key值&plugin=AMap.Autocomplete&plugin=AMap.Geocoder"></script>
<script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
<div class="layui-form-item layui-form-text" style="height: 300px">
<label class="layui-form-label">高德地图</label>
<div class="layui-input-block">
<%--地图大小--%>
<div id="container" style="height: 290px"></div>
<div id="myPageTop">
<table>
<tr>
<td>
<label>按关键字搜索:</label>
</td>
<td class="column2">
<label>左击获取经纬度:</label>
</td>
</tr>
<tr>
<td>
<input type="text" id="tipinput" autocomplete="off" onkeydown="if(event.keyCode==13)return false;" placeholder="请输入关键字进行搜索" >
</td>
<td class="column2">
<input type="text" readonly="true" id="lnglat" name="lnglat" >
</td>
</tr>
</table>
</div>
</div>
</div>
<%--
高德地图
点击获取的时候,填充搜索栏的地址,并记录mark点
--%>
<script type="text/javascript">
var map = new AMap.Map("container", {
resizeEnable: true,
zoom: 18
})
//为地图注册click事件获取鼠标点击出的经纬度坐标
var clickEventListener = map.on(\'click\', function(e) {
document.getElementById("lnglat").value = e.lnglat.getLng() + \',\' + e.lnglat.getLat();
if (marker) {
marker.setMap(null);
marker = null;
}
addMarker(e.lnglat.getLng(),e.lnglat.getLat());
//这边是数组
var lnglatXY=[e.lnglat.getLng(),e.lnglat.getLat()];
regeocoder(lnglatXY);
});
var auto = new AMap.Autocomplete({
input: "tipinput"
});
//注册监听,当选中某条记录时会触发
AMap.event.addListener(auto, "select", select);
function select(e) {
var lng = e.poi.location.lng;
var lat = e.poi.location.lat;
console.log(e.poi.location.lng);
if (e.poi && e.poi.location) {
map.setZoom(15);
map.setCenter(e.poi.location);
addMarker(lng,lat);
}
}
var marker;
// 实例化点标记
function addMarker(lng,lat) {
marker = new AMap.Marker({
icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
position: [lng, lat]
});
marker.setMap(map);
}
//坐标-地址
function regeocoder(lnglatXY) { //逆地理编码
var geocoder = new AMap.Geocoder({
radius: 1000,
extensions: "all"
});
geocoder.getAddress(lnglatXY, function(status, result) {
if (status === \'complete\' && result.info === \'OK\') {
geocoder_CallBack(result);
}
});
}
function geocoder_CallBack(data) {
var address = data.regeocode.formattedAddress; //返回地址描述
document.getElementById("tipinput").value=address;
}
</script>