高德地圖獲取地圖座標

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>火星座標拾取</title>
<script language="javascript" src="http://webapi.amap.com/maps?v=1.2&key=您的key"></script>
<style>
#iMap{height:500px;width:600px;float:left;}
.info{float:left;margin:0 0 0 10px;}
label{width:80px;float:left;}
</style>
</head>
<body onLoad="mapInit()">
    <div id="iMap"></div>
    <div class="info">
        <p><label>火星座標:</label><span id="lnglat">&nbsp;</span></p>
        <p><label>地址:</label><span id="iAddress">&nbsp;</span></p>
        </br>
        <p>說明:</p>
        <p>一、鼠標滾輪能夠縮放地圖,拖動地圖。</p>
        <p>二、點擊地圖,便可得到GCJ-02的經緯度座標,地址。</p>
    </div>
</body>
<script language="javascript">
var mapObj;
var lnglatXY;
//初始化地圖
function mapInit(){
    var opt = {  
        level: 13, //設置地圖縮放級別    
        center: new AMap.LngLat(116.397428, 39.90923) //設置地圖中心點   
    }  
    mapObj = new AMap.Map("iMap", opt);  
    
    AMap.event.addListener(mapObj,'click',getLnglat); //點擊事件
}
function geocoder() {
    var MGeocoder;
    //加載地理編碼插件
    mapObj.plugin(["AMap.Geocoder"], function() {        
        MGeocoder = new AMap.Geocoder({ 
            radius: 1000,
            extensions: "all"
        });
        //返回地理編碼結果 
        AMap.event.addListener(MGeocoder, "complete", geocoder_CallBack); 
        //逆地理編碼
        MGeocoder.getAddress(lnglatXY); 
    });
    //加點
    var marker = new AMap.Marker({
        map:mapObj,
        icon: new AMap.Icon({
            image: "http://api.amap.com/Public/images/js/mark.png",
            size:new AMap.Size(58,30),
            imageOffset: new AMap.Pixel(-32, -0)
        }),
        position: lnglatXY,
        offset: new AMap.Pixel(-5,-30)
    });
    mapObj.setFitView();
}
//回調函數
function geocoder_CallBack(data) {
    var address;
    //返回地址描述
    address = data.regeocode.formattedAddress;
    //返回結果拼接輸出
    document.getElementById("iAddress").innerHTML = address;
}  
//鼠標點擊,獲取經緯度座標  
function getLnglat(e){    
    mapObj.clearMap();
    var x = e.lnglat.getLng();
    var y = e.lnglat.getLat(); 
    document.getElementById("lnglat").innerHTML = x + "," + y;
    
    lnglatXY = new AMap.LngLat(x,y);
    geocoder();
}
</script>
</html>
相關文章
相關標籤/搜索