效果图:
具体代码:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
<title>输入提示</title>
<link rel="stylesheet" href="https://cache.amap.com/lbs/static/main1119.css"/>
<style>
html,
body,
#container {
width: 100%;
height: 100%;
}
</style>
</head>
<body>
<div class="layui-form-item layui-form-text" style="height: 300px">
<div class="layui-input-block">
<div id="container" style="height: 290px"></div>
<div id="myPageTop">
<table>
<tr>
<td>
<label>请输入关键字:</label>
</td>
</tr>
<tr>
<td>
<input id="tipinput"/>
</td>
</tr>
</table>
</div>
<div style="margin: 290px 0 0 0;">
坐标:<input type="text" readonly="true" id="lnglat" name="lnglat" >
</div>
</div>
</div>
<script src="js/jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.14&key=高德地图的key&plugin=AMap.Autocomplete"></script>
<script type="text/javascript">
var address = "";
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();
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;
document.getElementById("lnglat").value = lng+","+lat;
console.log(address);
if (e.poi && e.poi.location) {
map.setZoom(15);
map.setCenter(e.poi.location);
addMarker(lng,lat);
}
}
var marker;
// 实例化点标记
function addMarker(lng,lat) {
if (marker) {
marker.setMap(null);
marker = null;
}
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 address1 = data.regeocode.formattedAddress; //返回地址描述
document.getElementById("tipinput").value=address1;
}
console.log(address);
</script>
</body>
</html>