//初始化地图对象,加载地图
var map = new AMap.Map('mapContainer', {
resizeEnable: true,
zoom:11
});
//为地图注册click事件获取鼠标点击出的经纬度坐标
var clickEventListener = map.on('click', function(e) {
document.getElementById("longitude").value = e.lnglat.getLng();
document.getElementById("latitude").value = e.lnglat.getLat();
var lnglatXY = [e.lnglat.getLng(), e.lnglat.getLat()];
AMap.service('AMap.Geocoder',function(){//回调函数
//实例化Geocoder
geocoder = new AMap.Geocoder({
city: "010",//城市,默认:“全国”
});
//逆地理编码
geocoder.getAddress(lnglatXY, function(status, result) {
if (status === 'complete' && result.info === 'OK') {
//获得了有效的地址信息:
document.getElementById("addressStr").value = result.regeocode.formattedAddress;
} else {
//获取地址失败
alertBox("暂无地址信息!");
}
});
});
});
document.getElementById("query").onclick = function(e){
var inputAddress = document.getElementById("inputAddress").value;
placeSearch(inputAddress);
};
var MSearch;
function placeSearch(inputAddress) { //POI搜索,关键字查询
map.plugin(["AMap.PlaceSearch"], function() { //构造地点查询类
MSearch = new AMap.PlaceSearch({
pageSize:10,
pageIndex:1,
zoom:11,
city:"0755" //城市
});
AMap.event.addListener(MSearch, "complete", function(data){
var poiArr = data.poiList.pois;
var lngX = poiArr[0].location.getLng();
var latY = poiArr[0].location.getLat();
map.setCenter(new AMap.LngLat(lngX, latY));
});//返回地点查询结果
MSearch.search(inputAddress); //关键字查询
});
}
点击地图获取经纬度
js点击地图获取经纬度