Vue中騰訊地圖-自動獲取當前城市位置+搜索功能


<script>
export default {
    data() {
        return {
   //搜索關鍵字
            adressKeywords:'',
            coordinatesValue: {
                longitude: '', //經度
                latitude: '', //緯度
            }
        };
    },
 

 

    methods: {
 
  getCoordinates() {
 
          // 獲取當前地理位置信息
          let data = {
            key: "XXX" //密鑰,需要在騰訊位置服務官網(https://lbs.qq.com/)申請自己的密鑰。
          };
 
          let url = "https://apis.map.qq.com/ws/location/v1/ip"; //地理位置信息接口(根據IP自動獲取當前城市位置信息)
 
          let secondUrl = "https://apis.map.qq.com/ws/geocoder/v1/?address=" + this.adressKeywords; //根據輸入地址獲取對應的經緯度信息 (根據搜索關鍵字獲取當前城市位置信息)
 
          data.output = "jsonp";
 
   //判斷用戶是否輸入了搜索關鍵字,若沒有輸入搜索關鍵字則調用根據IP自動獲取當前城市位置信息的接口。
          this.$jsonp(this.adressKeywords == '' ? url : secondUrl, data)
 
            .then(res => {
              this.coordinatesValue.longitude = res.result.location.lng // 經度
              this.coordinatesValue.latitude = res.result.location.lat // 緯度
            })
            .catch(error => {
              console.log(error);
            });
        },
};
</script>

 


免責聲明!

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



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