bing Map 在vue項(xiàng)目中的使用詳解
寫在最前面
擁有全球數(shù)據(jù)庫(kù)國(guó)內(nèi)好像就只有百度地圖有,高德、搜狗、騰訊的都不行,但是由于百度地圖的數(shù)據(jù)更新不及時(shí),所以在做相關(guān)項(xiàng)目要用到國(guó)外數(shù)據(jù)的時(shí)候,最好還是推薦使用bingMap。
bing Map 使用教程(基礎(chǔ))
參考文檔:bing Map 官方教程
bing Map 初始化
引入bing map資源
<script type='text/javascript' src='http://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=[YOUR_BING_MAPS_KEY]' async defer></script>
初始化地圖
<div id="myMap"></div>
<script type='text/javascript'>
function GetMap()
{
var map = new Microsoft.Maps.Map('#myMap');
//Add your post map load code here.
}
</script>
設(shè)置地圖控制參數(shù)
常用控制參數(shù)
branch
加載地圖sdk的哪個(gè)分支:release(默認(rèn))、experimental
callback
地圖控制腳本加載完成后的回調(diào)(默認(rèn):GetMap)
key
用戶使用的userKey(詳情)
setLang
指定用于地圖標(biāo)簽和導(dǎo)航控件的語(yǔ)言
常用:中國(guó)大陸(zh-CN)、中國(guó)香港(zh-HK)、簡(jiǎn)體中文(zh-Hans)、中國(guó)臺(tái)灣(zh-TW)、英文-英國(guó)(en-GB)、英文-美國(guó)(en-US)
setMkt(詳情)
UR(詳情)
給bing map添加地圖事件(參考)
// 核心代碼-demo
Microsoft.Maps.Events.addHandler(你的地圖名稱, 觸發(fā)地圖事件名稱, function() { 觸發(fā)的事件 });
// 常用實(shí)例
//Add view change events to the map.
// 視圖更改事件
Microsoft.Maps.Events.addHandler(map, 'viewchangestart', function () { highlight('mapViewChangeStart'); });
Microsoft.Maps.Events.addHandler(map, 'viewchange', function () { highlight('mapViewChange'); });
Microsoft.Maps.Events.addHandler(map, 'viewchangeend', function () { highlight('mapViewChangEnd'); });
//Add mouse events to the map.
// 鼠標(biāo)事件
Microsoft.Maps.Events.addHandler(map, 'click', function () { highlight('mapClick'); });
Microsoft.Maps.Events.addHandler(map, 'dblclick', function () { highlight('mapDblClick'); });
Microsoft.Maps.Events.addHandler(map, 'rightclick', function () { highlight('mapRightClick'); });
Microsoft.Maps.Events.addHandler(map, 'mousedown', function () { highlight('mapMousedown'); });
Microsoft.Maps.Events.addHandler(map, 'mouseout', function () { highlight('mapMouseout'); });
Microsoft.Maps.Events.addHandler(map, 'mouseover', function () { highlight('mapMouseover'); });
Microsoft.Maps.Events.addHandler(map, 'mouseup', function () { highlight('mapMouseup'); });
Microsoft.Maps.Events.addHandler(map, 'mousewheel', function () { highlight('mapMousewheel'); });
//Add addition map event handlers
Microsoft.Maps.Events.addHandler(map, 'maptypechanged', function () { highlight('maptypechanged'); });
bing Map 添加圖釘(詳情)
基本圖釘示例
function GetMap() {
var map = new Microsoft.Maps.Map('#myMap', {
credentials: 'Your Bing Maps Key',
center: new Microsoft.Maps.Location(47.6149, -122.1941)
});
var center = map.getCenter();
//Create custom Pushpin
// 創(chuàng)建一個(gè)圖釘
var pin = new Microsoft.Maps.Pushpin(center, {
// demo_1
title: 'Microsoft', // 圖釘?shù)臉?biāo)題
subTitle: 'City Center', // 圖釘主體文字
text: '1' // 圖釘內(nèi)的文字
// demo_2
color: 'red', // 純色圖釘
});
//Add the pushpin to the map
map.entities.push(pin);
}
demo_1
demo_2
添加自定義圖片圖釘(詳情)
function GetMap() {
var map = new Microsoft.Maps.Map('#myMap',
{
credentials: 'You Bing Maps Key'
});
var center = map.getCenter();
//Create custom Pushpin
var pin = new Microsoft.Maps.Pushpin(center, {
icon: 'images/poi_custom.png', // 自定義圖片路徑
anchor: new Microsoft.Maps.Point(12, 39)
});
//Add the pushpin to the map
map.entities.push(pin);
}
自定義圖標(biāo)的圖釘
bing Map 給圖釘添加事件
核心代碼
//Create a pushpin.
var pushpin = new Microsoft.Maps.Pushpin(map.getCenter());
map.entities.push(pushpin);
//Add mouse events to the pushpin.
// 將自定義方法及鼠標(biāo)事件添加到圖釘上面
Microsoft.Maps.Events.addHandler(pushpin, 'click', function () { highlight('pushpinClick'); });
Microsoft.Maps.Events.addHandler(pushpin, 'mousedown', function () { highlight('pushpinMousedown'); });
Microsoft.Maps.Events.addHandler(pushpin, 'mouseout', function () { highlight('pushpinMouseout'); });
Microsoft.Maps.Events.addHandler(pushpin, 'mouseover', function () { highlight('pushpinMouseover'); });
Microsoft.Maps.Events.addHandler(pushpin, 'mouseup', function () { highlight('pushpinMouseup'); });
bing Map 給圖釘添加hover樣式
其核心還是給bing Map的圖釘添加事件,通過事件修改圖釘?shù)臉邮?/p>
// demo
var defaultColor = 'blue';
var hoverColor = 'red';
var mouseDownColor = 'purple';
var pin = new Microsoft.Maps.Pushpin(map.getCenter(), {
color: defaultColor
});
map.entities.push(pin);
Microsoft.Maps.Events.addHandler(pin, 'mouseover', function (e) {
e.target.setOptions({ color: hoverColor });
});
Microsoft.Maps.Events.addHandler(pin, 'mousedown', function (e) {
e.target.setOptions({ color: mouseDownColor });
});
Microsoft.Maps.Events.addHandler(pin, 'mouseout', function (e) {
e.target.setOptions({ color: defaultColor });
});
給圖釘添加hover樣式
bing Map 固定錨點(diǎn)
開發(fā)人員在使用自定義圖釘時(shí)遇到的最常見問題之一是,當(dāng)他們縮放地圖時(shí),看起來好像他們的圖釘正在漂移到或離開它所要錨定的位置。這是由于圖釘選項(xiàng)中的錨點(diǎn)值不正確。錨點(diǎn)指定圖像的哪個(gè)像素坐標(biāo)相對(duì)于圖像的左上角應(yīng)與圖釘位置坐標(biāo)重疊。
常見配置參考
bing Map 在vue中使用
vue引入bing Map可能會(huì)遇到的問題
由于vue一般引用第三方插件是用import的方式進(jìn)行的,所以的在html中使用script標(biāo)簽引入bing Map SDK會(huì)出現(xiàn)兩種問題
1.在控制臺(tái)會(huì)報(bào)錯(cuò):Mirosorft is not defined
2.vue-cli會(huì)報(bào)錯(cuò):Mirosorft is not defined
這里的原因是由于異步加載,所以在調(diào)用"Mirosorft"的時(shí)候可能SDK并沒有引用成功
解決“Mirosorft is not defined”的錯(cuò)誤
文檔參考
解決“Mirosorft is not defined”的錯(cuò)誤,只要在項(xiàng)目中保證調(diào)用地圖之前,能夠正確引入相關(guān)工具類就行了。
// bing map init devTools
export default {
init: function (){
console.log("初始化bing地圖腳本...");
// bing map key
const bingUesrKey = '你的bingMap Key';
const BingMap_URL = 'http://www.bing.com/api/maps/mapcontrol?callback=GetMap&key=' + bingUesrKey;
return new Promise((resolve, reject) => {
if(typeof Microsoft !== "undefined") {
resolve(Microsoft);
return true;
}
// 插入script腳本
let scriptNode = document.createElement("script");
scriptNode.setAttribute("type", "text/javascript");
scriptNode.setAttribute("src", BingMap_URL);
document.body.appendChild(scriptNode);
// 等待頁(yè)面加載完畢回調(diào)
let timeout = 0;
let interval = setInterval(() => {
// 超時(shí)10秒加載失敗
if(timeout >= 20) {
reject();
clearInterval(interval);
console.error("bing地圖腳本初始化失敗...");
}
// 加載成功
if(typeof Microsoft !== "undefined") {
resolve(Microsoft);
clearInterval(interval);
console.log("bing地圖腳本初始化成功...");
}
timeout += 1;
}, 500);
});
}
}
// bing map vue
import bingMap from './**/bing-map';
bingMap.init()
.then((Microsoft) => {
console.log(Microsoft)
console.log("加載成功...")
// 開始地圖操作
})
集成bing Map組件到vue中
需要達(dá)到的功能
在vue項(xiàng)目中成功加載bing Map (完成)
當(dāng)點(diǎn)擊bing Map的時(shí)候,返回點(diǎn)擊點(diǎn)的經(jīng)緯度 (完成)
子組件觸發(fā)事件返回參數(shù)到父組件
當(dāng)已有經(jīng)緯度的時(shí)候,加載bingMap自動(dòng)顯示其經(jīng)緯度所在的位置并設(shè)置圖釘 (待完成)
子組件觸發(fā)事件返回參數(shù)到父組件
實(shí)現(xiàn)原理
vue-$meit
核心代碼
// 子組件
<template>
<div @click="iclick"></div>
</template>
methods:{
iclick(){
let data = {
a:'data'
};
this.$emit('ievent', data1, 'data2Str');
}
}
// 父組件
<i-template @ievent = "ievent"></i-template>
methods:{
ievent(...data){
console.log('allData:',data); // data為包含傳過來所有數(shù)據(jù)的數(shù)組,第一個(gè)元素是對(duì)象,第二個(gè)元素是字符串
}
}
封裝bing Map通用組件
// 核心代碼
<template>
<div class="map-container">
<div id="localMap"></div>
</div>
</template>
<script>
import initBingMap from './initMap.js'
export default {
data () {
return {
lngNum: null, // 經(jīng)度
latNum: null, // 緯度
}
},
created: function () {
let _this = this;
initBingMap.init()
.then((Microsoft) => {
console.log(Microsoft)
console.log("加載成功...")
_this.initMap();
})
},
methods: {
initMap () {
let _this = this;
let map = new Microsoft.Maps.Map('#localMap', {
credentials: 'AgzeobkGvmpdZTFuGa7_6gkaHH7CXHKsFiTQlBvi55x-QLZLh1rSjhd1Da9bfPhD'
});
Microsoft.Maps.Events.addHandler(map, 'click', _this.getClickLocation);
},
getClickLocation (e) {
//若點(diǎn)擊到地圖的標(biāo)記上,而非地圖上
let [_this, loc] = [this, null];
if (e.targetType == 'pushpin') {
loc = e.target.getLocation();
}
//若點(diǎn)擊到地圖上
else {
var point = new Microsoft.Maps.Point(e.pageX, e.pageY);
loc = e.target.tryPixelToLocation(point, Microsoft.Maps.PixelReference.page);
}
console.log(loc.latitude+", "+loc.longitude);
console.log(loc);
_this.lngNum = loc.longitude;
_this.latNum = loc.latitude;
let data = {
lngNum: _this.lngNum,
latNum: _this.latNum
}
this.$emit('getLocationNums',data);
},
}
}
</script>
<style scoped>
.map-container {
width: 100%;
height: 400px;
border: 1px solid #000;
}
</style>
在組件中調(diào)用bing Map通用組件
// 引入bingMap
import bingMapsLayer from 'bingMap.vue'
// component中定義
components: {
bingMapsLayer
},
// template中使用
<bing-maps-layer @getLocationNums="getLocationNums"></bing-maps-layer>
// 定義觸發(fā)點(diǎn)擊標(biāo)記返回經(jīng)緯度的事件函數(shù)
getLocationNums (...data) {
let _this = this;
console.log('click');
console.log(data);
// 這里的data中即子組件bingMap返回的點(diǎn)擊獲取的經(jīng)緯度值
},
總結(jié)
以上所述是小編給大家介紹的bing Map 在vue項(xiàng)目中的使用詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
ElementUI中利用table表格自定義表頭Tooltip文字提示
這篇文章主要介紹了ElementUI中利用table表格自定義表頭Tooltip文字提示,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
vue 屬性攔截實(shí)現(xiàn)雙向綁定的實(shí)例代碼
這篇文章主要介紹了vue 屬性攔截實(shí)現(xiàn)雙向綁定的實(shí)例代碼,代碼簡(jiǎn)答易懂,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2018-10-10
關(guān)于vue中@click.native.prevent的說明
這篇文章主要介紹了關(guān)于vue中@click.native.prevent的說明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
vue路由跳轉(zhuǎn)router-link清除歷史記錄的三種方式(總結(jié))
這篇文章主要介紹了vue路由跳轉(zhuǎn)router-link清除歷史記錄的三種方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04
vue封裝el-upload批量上傳只請(qǐng)求一次接口
本文主要介紹了vue封裝el-upload批量上傳只請(qǐng)求一次接口,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02
前端儲(chǔ)存之localStrage、sessionStrage和Vuex使用
localStorage、sessionStorage和Vuex是三種不同的客戶端存儲(chǔ)方式,用于在瀏覽器中保存數(shù)據(jù),localStorage和sessionStorage都是以鍵值對(duì)的形式存儲(chǔ)數(shù)據(jù),但localStorage存儲(chǔ)的數(shù)據(jù)在關(guān)閉瀏覽器后仍然存在2025-01-01

