1. 程式人生 > >高德地圖獲取經緯度和獲取詳細地址

高德地圖獲取經緯度和獲取詳細地址

<!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="http://cache.amap.com/lbs/static/main1119.css"/>
    <script type="text/javascript"
            src="http://webapi.amap.com/maps?v=1.4.3&key=申請的key值=AMap.Autocomplete"></script>
    <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
</head>
<body>
<div id="container" style="width: 100%;height: 500px"></div>
<div id="myPageTop">
    <table>
        <tr>
            <td class="column2">
                <label>按關鍵字搜尋:</label>
            </td>
        </tr>
        <tr>
            <td class="column2">
                <input type="text" placeholder="請輸入關鍵字進行搜尋" id="tipinput" style="width: 250px">
            </td>
        </tr>
    </table>
</div>
<script type="text/javascript">
    var map = new AMap.Map("container", {
            resizeEnable: true,
            zoom:12,
            center: [116.397428, 39.90923]
        });
        //為地圖註冊click事件獲取滑鼠點擊出的經緯度座標
        var clickEventListener = map.on('click', function(e) {
            var lng = e.lnglat.getLng();
            var lat = e.lnglat.getLat();
            console.log("經度:"+lng+"緯度"+lat);
            var lnglatXY = [lng, lat];//地圖上所標點的座標
            AMap.service('AMap.Geocoder',function() {//回撥函式
                geocoder = new AMap.Geocoder({
                });
                geocoder.getAddress(lnglatXY, function (status, result) {
                    if (status === 'complete' && result.info === 'OK') {
                        //獲得了有效的地址資訊:
                        //即,result.regeocode.formattedAddress
                        console.log(result.regeocode.formattedAddress);
                        var address = result.regeocode.formattedAddress;
                    } else {
                        //獲取地址失敗
                    }
                });
            })
        });
        auto = new AMap.Autocomplete({
            input: "tipinput"
        });
        console.log(auto);
        AMap.event.addListener(auto, "select", select);
        function select(e) {
            if (e.poi && e.poi.location) {
                map.setZoom(15);
                map.setCenter(e.poi.location);
            }
        }
</script>
</body>
</html>