javaweb使用高德地圖進行網頁定位

渴望飛的魚發表於2018-04-02

參考高德地圖api:http://lbs.amap.com/api/javascript-api/guide/map-data/cministrative_division

兩點之間根據經緯度判斷:http://lbs.amap.com/api/webservice/guide/api/georegeo/?


根據網頁定位:

<!DOCTYPE html>
 
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="baidu-site-verification" content="zyu2elKF1h">
    <meta name="robots" content="noodp">
    <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.3&key=8cc355b1d46538c64a75d9a0b57ea744"></script>   
    <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
    <style type="text/css">
        /*刪除高德地圖的logo*/
          .amap-logo {
            right: 0 !important;
            left: auto !important;
            display: none;
        }
         
        .amap-copyright {
            right: 70px !important;
            left: auto !important;
        } 
    </style>
    </head>
     
<body>
<div id='container' style="height: 600px;width: 500px"></div>
<div id="tip"></div>
<script type="text/javascript">
    var map, geolocation;
    //載入地圖,呼叫瀏覽器定位服務
    map = new AMap.Map('container', {
        resizeEnable: true
    });
    map.plugin('AMap.Geolocation', function () {
        geolocation = new AMap.Geolocation({
            enableHighAccuracy: true,//是否使用高精度定位,預設:true
            timeout: 10000,          //超過10秒後停止定位,預設:無窮大
            buttonOffset: new AMap.Pixel(10, 20),//定位按鈕與設定的停靠位置的偏移量,預設:Pixel(10, 20)
            zoomToAccuracy: false,      //定位區域
            buttonPosition: 'RB'
        });
        map.addControl(geolocation);
        geolocation.getCurrentPosition();
        AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位資訊
        AMap.event.addListener(geolocation, 'error', onError);      //返回定位出錯資訊
    });
 
    
    
    //解析定位結果
    function onComplete(data) {
        var str = ['定位成功'];
        str.push('經度:' + data.position.getLng());      
        str.push('緯度:' + data.position.getLat());
        str.push('精度:' + data.accuracy + ' 米');
        str.push('是否經過偏移:' + (data.isConverted ? '是' : '否'));       
        document.getElementById('tip').innerHTML = str.join('<br>');
    }
    //解析定位錯誤資訊
    function onError(data) {
        document.getElementById('tip').innerHTML = '定位失敗';
    }
</script>
</body>
</html>

相關文章