xnzt/xnzt-h5/static/html/location.html
2024-11-11 10:27:27 +08:00

116 lines
3.5 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!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://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css"/>
<style>
html,body,#container{
height:100%;
width:100%;
}
.btn{
width:10rem;
margin-left:6.8rem;
}
</style>
</head>
<body>
<div id="container"></div>
<!--传递经纬度参数-->
<input type="hidden" id="lng">
<input type="hidden" id="lat">
<div class="input-card" style='width:28rem;'>
<label style='color:grey'>逆地理编码,根据经纬度获取地址信息</label>
<div class="input-item">
<div class="input-item-prepend"><span class="input-item-text">经纬度</span></div>
<input id='lnglat' type="text" value='116.39,39.9'>
</div>
<div class="input-item">
<div class="input-item-prepend"><span class="input-item-text" >地址</span></div>
<input id='address' type="text" disabled>
</div>
<input id="regeo" type="button" class="btn" value="经纬度 -> 地址" >
</div>
<script type="text/javascript">
// 安全秘钥
window._AMapSecurityConfig = {
securityJsCode:'5aec449447e2bbaa184385edd59a8f25',
}
</script>
<script src="https://a.amap.com/jsapi_demos/static/demo-center/js/demoutils.js"></script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=2.0&key=86093f0d8733199ee3e5fd8b1494945f&plugin=AMap.Geocoder"></script>
<script type="text/javascript">
var map = new AMap.Map("container", {
resizeEnable: true
});
var geocoder = new AMap.Geocoder({
city: "420500", //城市设为宜昌市,默认:“全国”
radius: 1000 //范围默认500
});
// 标点
var marker = new AMap.Marker();;
// 根据标点查询获取地址
function regeoCode() {
var lnglat = document.getElementById('lnglat').value.split(',');
map.add(marker);
marker.setPosition(lnglat);
geocoder.getAddress(lnglat, function(status, result) {
console.log(result);
var lng = document.getElementById('lng').value;
var lat = document.getElementById('lat').value;
// 经度
result.lng = lng;
// 纬度
result.lat = lat;
// 向父页面传递参数
window.parent.postMessage(result);
if (status === 'complete'&&result.regeocode) {
var address = result.regeocode.formattedAddress;
document.getElementById('address').value = address;
// 构建自定义信息窗体
var infoWindow = new AMap.InfoWindow({
anchor: 'bottom-center',
content: '经纬度:'+lnglat+'<br>地址:'+address,
offset: new AMap.Pixel(0, -30)
});
infoWindow.open(map, lnglat);
}else{
log.error('根据经纬度查询地址失败')
}
});
}
// 地图定位点击事件
map.on('click',function(e){
console.log(e);
console.log("e.lgn=="+e.lnglat.lng+"e.lat=="+e.lnglat.lat);
document.getElementById('lnglat').value = e.lnglat;
document.getElementById('lng').value = e.lnglat.lng;
document.getElementById('lat').value = e.lnglat.lat;
regeoCode();
map.setFitView();//地图自适应
})
// document.getElementById("regeo").onclick = regeoCode;
// document.getElementById('lnglat').onkeydown = function(e) {
// if (e.keyCode === 13) {
// regeoCode();
// return false;
// }
// return true;
// };
</script>
</body>
</html>