H5調用騰訊地圖


獲取當前定位的經緯度並在容器內顯示當前位置 (安卓上的位置有點偏差)

在vue的index.html中需要引用

template

<div id="container" style="width:600px;height:500px;"></div>

javaScript

<script>
export defalut {
    data(){
        return {
            longitude:0,//經度
            latitude:0,//緯度
            city:''
        }
    },
    mounted(){
        this.getMyLocation(); //騰訊地圖定位當前的位置
    },
    methods: {
        //第一部分
        //定位獲得當前位置信息
        getMyLocation() {
            var geolocation = new qq.maps.Geolocation("you key", "youkeyname");
            //geolocation.getIpLocation(this.showPosition, this.showErr);
            geolocation.getLocation(this.showPosition, this.showErr);//或者用getLocation精確度比較高
        },
        showPosition(position) {
            console.log(position);
            this.latitude = position.lat;
            this.longitude = position.lng;
            this.city = position.city;
            console.log(this.latitude,'this.latitude',this.longitude,'this.longitude',this.city,'this.city')
            this.setMap();
        },
        showErr() {
            console.log("定位失敗");
            this.getMyLocation();//定位失敗再請求定位,測試使用
        },
        //第二部分
        //位置信息在地圖上展示
        setMap() {
            //步驟:定義map變量 調用 qq.maps.Map() 構造函數   獲取地圖顯示容器
            //設置地圖中心點
            var myLatlng = new qq.maps.LatLng(this.latitude,this.longitude);
            //定義工廠模式函數
            var myOptions = {
              zoom: 100,               //設置地圖縮放級別
              center: myLatlng,    //設置中心點樣式
              mapTypeId: qq.maps.MapTypeId.ROADMAP  //設置地圖樣式詳情參見MapType
            }
             //獲取dom元素添加地圖信息
            var map = new qq.maps.Map(document.getElementById("container"), myOptions);
              //第三部分
              //給定位的位置添加圖片標注
            var marker = new qq.maps.Marker({
                position: myLatlng,
                map: map
            });
            //給定位的位置添加文本標注
            var marker = new qq.maps.Label({
                position: myLatlng,
                map: map,
                content:'這是我當前的位置,偏差有點大,哈哈'
            });
        }
    }
    
}
</script>

相關文檔:

1. 路線規划:https://lbs.qq.com/guides/direction.html

2. 簡單地圖示例:https://lbs.qq.com/javascript_v2/case-run.html#sample-map

3. 配置密碼:https://lbs.qq.com/dev/console/key/manage

4. java根據經緯度查詢門店地理位置-完美解決附近門店問題: https://www.cnblogs.com/fuzongle/p/11327688.html

5. 騰訊地圖前端定位組件: https://lbs.qq.com/tool/component-geolocation.html


免責聲明!

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



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