<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>