H5页面
<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>
<!--引入高德地图JSAPI -->
<script src="https://cdn.staticfile.org/jquery/1.10.2/jquery.min.js"></script>
<link rel="stylesheet" href="http://cache.amap.com/lbs/static/main1119.css"/>
<script src="http://cache.amap.com/lbs/static/es5.min.js"></script>
<script src="http://webapi.amap.com/maps?v=1.4.0&key=7c1b6b2cfb952c65ee87707ef162beca&plugin=AMap.Autocomplete,AMap.PlaceSearch"></script>
<script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
<style>
html, body, #container {
height: 100%;
width: 100%;
}
.amap-icon img,
.amap-marker-content img{
width: 25px;
height: 34px;
}
.marker {
position: absolute;
top: -20px;
right: -118px;
color: #fff;
padding: 4px 10px;
box-shadow: 1px 1px 1px rgba(10, 10, 10, .2);
white-space: nowrap;
font-size: 12px;
font-family: "";
background-color: #25A5F7;
border-radius: 3px;
}
.input-card{
width: 32rem;
z-index: 170;
}
.input-card .btn{
margin-right: .8rem;
}
.input-card .btn:last-child{
margin-right: 0;
}
</style>
</head>
<body>
<div id="container"></div>
<div id="myPageTop">
<table>
<tr>
<td>
<label>请输入关键字:</label>
</td>
</tr>
<tr>
<td>
<input id="tipinput" />
</td>
</tr>
</table>
</div>
</body>
</html>
js代码实现
<script>
var map,marker;
var geocoder;
var placeSearch;
$(function(){
// 加入高的地图
map = new AMap.Map('container', {
resizeEnable: true,
zoom: 11,
center: [116.40, 39.91]
});
//为地图注册click事件获取鼠标点击出的经纬度坐标
map.on('click', function(e) {
alert( e.lnglat.getLng() + ',' + e.lnglat.getLat());
clearMarker();
addMarker(e.lnglat.getLng(),e.lnglat.getLat());
});
//输入提示
var autoOptions = {
input: "tipinput"
};
var auto = new AMap.Autocomplete(autoOptions);
var placeSearch = new AMap.PlaceSearch({
map: map
}); //构造地点查询类
AMap.event.addListener(auto, "select", select);//注册监听,当选中某条记录时会触发
function select(e) {
console.log(e);
placeSearch.setCity(e.poi.adcode);
placeSearch.search(e.poi.name); //关键字查询查询
}
AMap.event.addListener(placeSearch, "markerClick", function(e){
console.log(e.data.location);//当前marker的经纬度信息
alert(e.data.location.lng + ',' + e.data.location.lat);
});
});
// 实例化点标记
function addMarker(lng,lat) {
marker = new AMap.Marker({
icon: "poi-marker-default.png",
position: [lng,lat],
offset: new AMap.Pixel(-13, -30)
});
marker.setMap(map);
}
// 清除 marker
function clearMarker() {
if (marker) {
marker.setMap(null);
marker = null;
}
}
</script>
实现效果