js高德地圖手機定位

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<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=9f831272996d8fb9c2765393725e3f96"></script>
    <script type="text/javascript" src="http://cache.amap.com/lbs/static/addToolbar.js"></script>
    <body>
        <div id='container'></div>
        <div id="tip"></div>
        <script type="text/javascript">
        /***************************************
        因爲Chrome、IOS10等已再也不支持非安全域的瀏覽器定位請求,爲保證定位成功率和精度,請儘快升級您的站點到HTTPS。
        ***************************************/
        var map, geolocation;
        map = new AMap.Map('container');
        map.plugin('AMap.Geolocation', function() {
            geolocation = new AMap.Geolocation({
                enableHighAccuracy: true, //是否使用高精度定位,默認:true
                timeout: 10000, //超過10秒後中止定位,默認:無窮大
                maximumAge: 0, //定位結果緩存0毫秒,默認:0
                convert: true, //自動偏移座標,偏移後的座標爲高德座標,默認:true
                showButton: true, //顯示定位按鈕,默認:true
                buttonPosition: 'LB', //定位按鈕停靠位置,默認:'LB',左下角
                buttonOffset: new AMap.Pixel(10, 20), //定位按鈕與設置的停靠位置的偏移量,默認:Pixel(10, 20)
                showMarker: true, //定位成功後在定位到的位置顯示點標記,默認:true
                showCircle: true, //定位成功後用圓圈表示定位精度範圍,默認:true
                panToLocation: true, //定位成功後將定位到的位置做爲地圖中心點,默認:true
                zoomToAccuracy: true //定位成功後調整地圖視野範圍使定位位置及精度範圍視野內可見,默認:false
            });
            map.addControl(geolocation);
            geolocation.getCurrentPosition();
            AMap.event.addListener(geolocation, 'complete', onComplete); //返回定位信息
            AMap.event.addListener(geolocation, 'error', onError); //返回定位出錯信息
        });
        var myplace = ''; //個人位置
        var mycity = ''; //我所在的城市
        //解析定位結果
        function onComplete(data) {
            var str = ['定位成功'];
            str.push('經度:' + data.position.getLng());
            str.push('緯度:' + data.position.getLat());
            if (data.accuracy) {
                str.push('精度:' + data.accuracy + '');
            } //如爲IP精肯定位結果則沒有精度信息
            str.push('是否通過偏移:' + (data.isConverted ? '' : ''));
            document.getElementById('tip').innerHTML = str.join('<br>');
            // parent.loca(data);
            myplace = data.formattedAddress; //個人位置
            mycity = data.addressComponent.city; //我所在的城市
            console.log('str',data.position.getLng(),data.position.getLat());
            console.log("我所在的地點爲 " + data.formattedAddress);
            console.log("我所在的城市爲 " + data.addressComponent.city);
        }

        function getSomeOfMyVal() {
            return myplace + "+" + mycity;
        }

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

        function getGeolocation() {
            geolocation.getCurrentPosition();

        }
        </script>
    </body>

</html>
相關文章
相關標籤/搜索