高德地圖獲取經緯度

<!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>
    <script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=0250860ccb5953fa5d655e8acf40ebb7&plugin=AMap.Geocoder"></script>
    <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
    <style>
        #addressBox{height:20px;width:600px;}
        #mapBox{height:400px;width:600px}
        #pointBox{height:20px;width:600px;}
    </style>
<body>
<div>
    <input id="addressBox" type="text" value="請輸入結構化地址,例如北京市朝陽區望京街道首開廣場" />
    <input id="" type="button" onclick="geocoder();" value="搜索" />
</div>
<div id='pointBox'>&nbsp;</div>
<div id='mapBox'></div>
<div>
使用方法:<br />
1.輸入地址,點擊搜索<br />
2.若是不夠準確,能夠拖動地圖改變經緯度
</div>
<script type="text/javascript">
var $addressBox = document.getElementById('addressBox');
var $pointBox = document.getElementById("pointBox");

//建立地圖
var map = new AMap.Map("mapBox", {
    resizeEnable: true,
    center: [116.397428, 39.90923],
    zoom:14
});

//添加標註
function addMarker(point) {
    var marker = new AMap.Marker({
        map: map,
        position: [ point.getLng(),  point.getLat()]
    });
}

//地圖中心點添加標註
function addCenterPoint(){
    map.clearMap();
    var centerPoint = map.getCenter();
    addMarker(centerPoint);
    $pointBox.innerHTML = "當前經緯度爲:" + centerPoint.getLng() + ',' + centerPoint.getLat();
}
addCenterPoint();

function geocoder() {
    map.clearMap();
    var myGeo = new AMap.Geocoder();
    //地理編碼,返回地理編碼結果
    myGeo.getLocation($addressBox.value, function(status, result) {
        if (status === 'complete' && result.info === 'OK') {
            //地址解析成功
            geocoder_CallBack(result);
        }
        else{
            //地址解析失敗
            $pointBox.innerHTML = "查無此地址";
        }
    });
}

//地理編碼返回結果展現
function geocoder_CallBack(data) {
    var resultStr = "";
    var geocode = data.geocodes;
    addMarker(geocode[0].location);
    resultStr += "當前座標</b>:" + geocode[0].location.getLng() + ", " + geocode[0].location.getLat();
    map.setFitView();
    $pointBox.innerHTML = resultStr;
}

map.on('moveend', function() {
    addCenterPoint();
});
</script>
</body>
</html>
相關文章
相關標籤/搜索