騰訊地圖應(yīng)用例子之定位打卡

本文教給大家如何使用JavaScript API進(jìn)行基于LBS的基礎(chǔ)開發(fā):在地圖上標(biāo)記位置。提供地圖服務(wù)的有高德地圖、百度地圖和騰訊地圖,騰訊地圖官網(wǎng)上的JavaScript API已升級(jí)到了JavaScript API GL,Javascript API GL是基于WebGL技術(shù)打造的3D版地圖API,3D化的視野更為自由,交互更加流暢,故本文此次選取的地圖是騰訊地圖。

1. 注冊(cè)賬號(hào)

a. 注冊(cè)成為開發(fā)者
b. 申請(qǐng)密鑰,在如圖位置創(chuàng)建新密鑰

c. 進(jìn)行配置
推薦使用授權(quán)IP的方式進(jìn)行配置,將你個(gè)人的IP寫進(jìn)輸入框就OK啦!

2.使用官方Api進(jìn)行地圖標(biāo)記(JavaScript)

2.1 通過(guò)搜索接口找到個(gè)人公司位置:

https://apis.map.qq.com/ws/place/v1/search?keyword=嘉譽(yù)國(guó)際&boundary=region(上海,0)&key=HLSBZ-T6JR5-BIEIW-QLJSF-ACEA3-*****

key換成你的

查詢到公司位置為(31.329716,121.508386)

2.2 初始化地圖,中心設(shè)為公司位置

<!DOCTYPE html>
<html>
<head>
    
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>Hello world!</title>
    <style type="text/css">
        #container{
            /*地圖(容器)顯示大小*/
            width:1200px;
            height:400px;
        }
    </style>
    <!--引入Javascript API GL,參數(shù)說(shuō)明參見下文-->
    <script src="https://map.qq.com/api/gljs?v=1.exp&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"></script>
    <script>
        //地圖初始化函數(shù),本例取名為init,開發(fā)者可根據(jù)實(shí)際情況定義
        function initMap() {
            //定義地圖中心點(diǎn)坐標(biāo)
            var center = new TMap.LatLng(31.329716, 121.508386);
            //定義map變量,調(diào)用 TMap.Map() 構(gòu)造函數(shù)創(chuàng)建地圖
            var map = new TMap.Map(document.getElementById('container'), {
                center: center,//設(shè)置地圖中心點(diǎn)坐標(biāo)
                viewMode:'2D',//設(shè)置顯示模式 2D 3D可以自己修改
                zoom: 17.2,   //設(shè)置地圖縮放級(jí)別
                pitch: 43.5,  //設(shè)置俯仰角
                rotation: 45    //設(shè)置地圖旋轉(zhuǎn)角度
            });
        }
    </script>
</head>
<!-- 頁(yè)面載入后,調(diào)用init函數(shù) -->
<body onload="initMap()">
<!-- 定義地圖顯示容器 -->
<div id="container"></div>
</body>
</html>

效果展示:

標(biāo)注的位置就是設(shè)置的中心啦。

2.3 為公司位置位置打上標(biāo)記

效果示例:



公司的位置加上了style中寫的圖片,代碼如下:

<script src="https://map.qq.com/api/gljs?v=1.exp&key=OB4BZ-D4W3U-B7VVO-4PJWW-6TKDJ-WPB77"></script>
<script>
    //地圖初始化函數(shù),本例取名為init,開發(fā)者可根據(jù)實(shí)際情況定義
    function initMap() {
        //創(chuàng)建map對(duì)象,初始化地圖
        var center = new TMap.LatLng(31.329716, 121.508386);
        var map = new TMap.Map(document.getElementById('container'), {
            center: center,//設(shè)置地圖中心點(diǎn)坐標(biāo)
            zoom: 17.2,   //設(shè)置地圖縮放級(jí)別
            pitch: 43.5,  //設(shè)置俯仰角
            rotation: 45    //設(shè)置地圖旋轉(zhuǎn)角度
        });
        //創(chuàng)建并初始化MultiMarker
        var markerLayer = new TMap.MultiMarker({
            map: map,  //指定地圖容器
            //樣式定義
            styles: {
                //創(chuàng)建一個(gè)styleId為"myStyle"的樣式(styles的子屬性名即為styleId)
                "myStyle": new TMap.MarkerStyle({
                    "width": 25,  // 點(diǎn)標(biāo)記樣式寬度(像素)
                    "height": 35, // 點(diǎn)標(biāo)記樣式高度(像素)
                    "src": '微信圖片_20210107230437.jpg',  //圖片路徑
                    //焦點(diǎn)在圖片中的像素位置,一般大頭針類似形式的圖片以針尖位置做為焦點(diǎn),圓形點(diǎn)以圓心位置為焦點(diǎn)
                    "anchor": { x: 16, y: 32 }
                })
            },
            //點(diǎn)標(biāo)記數(shù)據(jù)數(shù)組
            geometries: [{
                "id": "1",   //點(diǎn)標(biāo)記唯一標(biāo)識(shí),后續(xù)如果有刪除、修改位置等操作,都需要此id
                "styleId": 'myStyle',  //指定樣式id
                "position": new TMap.LatLng(31.329716, 121.508386),  //點(diǎn)標(biāo)記坐標(biāo)位置
                "properties": {//自定義屬性
                    "title": "我的公司"
                }
            }
            ]
        });
    }

</script>

2.4 讓標(biāo)記的點(diǎn)動(dòng)起來(lái)

主要通過(guò)使用moveAlong方法,定義移動(dòng)的軌跡path,在moveAlong中傳入移動(dòng)的路線和坐標(biāo)點(diǎn),坐標(biāo)點(diǎn)是geometries中的標(biāo)記。

    <script>
    //地圖初始化函數(shù),本例取名為init,開發(fā)者可根據(jù)實(shí)際情況定義
    function initMap() {
        //創(chuàng)建map對(duì)象,初始化地圖
        var center = new TMap.LatLng(31.329716, 121.508386);
        var map = new TMap.Map(document.getElementById('container'), {
            center: center,//設(shè)置地圖中心點(diǎn)坐標(biāo)
            zoom: 17.2,   //設(shè)置地圖縮放級(jí)別
            pitch: 43.5,  //設(shè)置俯仰角
            rotation: 45    //設(shè)置地圖旋轉(zhuǎn)角度
        });
        //創(chuàng)建并初始化MultiMarker
        var marker = new TMap.MultiMarker({
            map: map,  //指定地圖容器
            //樣式定義
            styles: {
                //創(chuàng)建一個(gè)styleId為"myStyle"的樣式(styles的子屬性名即為styleId)
                "myStyle": new TMap.MarkerStyle({
                    "width": 25,  // 點(diǎn)標(biāo)記樣式寬度(像素)
                    "height": 35, // 點(diǎn)標(biāo)記樣式高度(像素)
                    "src": '微信圖片_20210107230437.jpg',  //圖片路徑
                    //焦點(diǎn)在圖片中的像素位置,一般大頭針類似形式的圖片以針尖位置做為焦點(diǎn),圓形點(diǎn)以圓心位置為焦點(diǎn)
                    "anchor": { x: 16, y: 32 }
                })
            },
            //點(diǎn)標(biāo)記數(shù)據(jù)數(shù)組
            geometries: [{
                "id": "person",   //點(diǎn)標(biāo)記唯一標(biāo)識(shí),后續(xù)如果有刪除、修改位置等操作,都需要此id
                "styleId": 'myStyle',  //指定樣式id
                "position": new TMap.LatLng(31.329716, 121.508386),  //點(diǎn)標(biāo)記坐標(biāo)位置
                "properties": {//自定義屬性
                    "title": "我的公司"
                }}
            ]
        });

        //定義的移動(dòng)路線
        var path = [
            new TMap.LatLng(31.329716, 121.508386),
            new TMap.LatLng(31.330720, 121.508388),
            new TMap.LatLng(31.349725, 121.508389),
            new TMap.LatLng(31.359730, 121.508389),
            new TMap.LatLng(31.369718, 121.518400)
        ];

        marker.moveAlong({
                //讓坐標(biāo)動(dòng)起來(lái)
                "person": {
                    path,
                    //速度為100
                    speed: 100
                }
            }, {
                autoRotation:true   //車頭始終向前(沿路線自動(dòng)旋轉(zhuǎn))
            }
        )
    }

</script>

3. 案例拓展:使用騰訊位置服務(wù)API實(shí)現(xiàn)打卡功能

3.1 實(shí)現(xiàn)思路

3.1.1 位置搜索

接口: https://apis.map.qq.com/ws/place/v1/search
搜索上海 上海五角場(chǎng)地鐵站的位置
示例:https://apis.map.qq.com/ws/place/v1/search?keyword=五角場(chǎng)地鐵站&boundary=region(上海,0)
&key=HLSBZ--

搜索結(jié)果截?。?br> {
“id”: “10804093066539767491”,
“title”: “五角場(chǎng)[地鐵站]”,
“address”: “地鐵10號(hào)線”,
“tel”: “”,
“category”: “基礎(chǔ)設(shè)施:交通設(shè)施:地鐵站”,
“type”: 2,
“l(fā)ocation”: {
“l(fā)at”: 31.298109,
“l(fā)ng”: 121.514651
},
“ad_info”: {
“adcode”: 310110,
“province”: “上海市”,
“city”: “上海市”,
“district”: “楊浦區(qū)”
}
}

現(xiàn)在知道五角場(chǎng)地鐵站的經(jīng)緯度了,就是Location那兩個(gè)值。
注意key是創(chuàng)建好的這一串ID


通過(guò)搜索出來(lái)的是一個(gè)List,上述只拿出了List的第一位,通常第一位也是嘴和搜索關(guān)鍵字相符合的。通過(guò)搜索取出我們固定的經(jīng)緯度的地址。

3.1.2 定位(Android定位)

單次定位,獲取當(dāng)前設(shè)備位置

mLocationManager.requestSingleFreshLocation(null, mLocationListener, Looper.getMainLooper());

其他IOS等設(shè)備可以通過(guò)騰訊位置服務(wù)官方文檔查看。
獲取到位置后將該位置傳到后臺(tái)接口。

3.1.3 后臺(tái)接口

求距離:

// 寫死的公司位置,實(shí)際項(xiàng)目中應(yīng)該是數(shù)據(jù)庫(kù)配置
private static final double COMPANY_LAT = 31.298109;
private static final double COMPANY_LNG = 121.514651;

@GetMapping("distance")
public Map<String,Double> queryDistance(Double lat,Double lng){
    HashMap<String, Double> result = new HashMap<>(4);
    double distance = distance(COMPANY_LAT, lat, COMPANY_LNG, lng);
    result.put("distance",distance);
    return result;
}

/**
 * 求兩個(gè)經(jīng)緯度之間的距離
 */
public static double distance(double lat1, double lat2, double lng1, double lng2) {
    final int r = 6371;
    double latDistance = Math.toRadians(lat2 - lat1);
    double lonDistance = Math.toRadians(lng2 - lng1);
    double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
            + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
            * Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double distance = r * c * 1000;
    distance = Math.pow(distance, 2);
    return Math.sqrt(distance);
}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容