高德地圖定位,獲取當前位置坐標


<!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="https://cache.amap.com/lbs/static/main1119.css" />
        <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.4&key=ad26780bfaabf0ca27ae6078e2e81682"></script>
        <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>

        <body>
            
            <div id='container' style=''></div>

            <div id="tip"></div>
            <script type="text/javascript">
                /***************************************
                    由於Chrome、IOS10等已不再支持非安全域的瀏覽器定位請求,為保證定位成功率和精度,請盡快升級您的站點到HTTPS。
                    ***************************************/
                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: true, //定位成功后調整地圖視野范圍使定位位置及精度范圍視野內可見,默認:false
                        buttonPosition: 'RB'
                    });
document.getElementById('tip').innerHTML="定位中,請勿操作,否則會定位失敗";
document.getElementById("tip").style.color="red";
                    map.addControl(geolocation);
                    geolocation.getCurrentPosition();
                    AMap.event.addListener(geolocation, 'complete', onComplete); //返回定位信息
                    AMap.event.addListener(geolocation, 'error', onError); //返回定位出錯信息
                });
                //解析定位結果
                function onComplete(data) {

                    var str = [];
console.log(data.position.getLat());
console.log(data.position.getLng());
                    str.push(data.position.getLat());
                    str.push(data.position.getLng());
                    //document.getElementById('tip').innerHTML = str;
document.getElementById('tip').innerHTML="定位成功";
document.getElementById("tip").style.color="#15A3FA";
setTimeout(tishi,3000);


                }

                //解析定位錯誤信息
                function onError(data) {
                    document.getElementById('tip').innerHTML = '定位失敗請刷新再試';

                }

function tishi(){ 
  document.getElementById("tip").style.visibility="hidden";
} 
            </script>
        </body>

</html>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM