成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

如何在vue項(xiàng)目中使用bingMap

本篇文章給大家分享的是有關(guān)如何在vue項(xiàng)目中使用bing Map ,小編覺得挺實(shí)用的,因此分享給大家學(xué)習(xí),希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

崇明ssl適用于網(wǎng)站、小程序/APP、API接口等需要進(jìn)行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!

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的哪個分支:release(默認(rèn))、experimental
callback
地圖控制腳本加載完成后的回調(diào)(默認(rèn):GetMap)
key
用戶使用的userKey(詳情)
setLang
指定用于地圖標(biāo)簽和導(dǎo)航控件的語言
常用:中國大陸(zh-CN)、中國香港(zh-HK)、簡體中文(zh-Hans)、中國臺灣(zh-TW)、英文-英國(en-GB)、英文-美國(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)建一個圖釘
 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

如何在vue項(xiàng)目中使用bing Map 

demo_2

如何在vue項(xiàng)目中使用bing Map 

添加自定義圖片圖釘(詳情)

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)的圖釘

如何在vue項(xiàng)目中使用bing Map 

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

如何在vue項(xiàng)目中使用bing Map 

bing Map 固定錨點(diǎn)

開發(fā)人員在使用自定義圖釘時遇到的最常見問題之一是,當(dāng)他們縮放地圖時,看起來好像他們的圖釘正在漂移到或離開它所要錨定的位置。這是由于圖釘選項(xiàng)中的錨點(diǎn)值不正確。錨點(diǎn)指定圖像的哪個像素坐標(biāo)相對于圖像的左上角應(yīng)與圖釘位置坐標(biāo)重疊。

常見配置參考

bing Map 在vue中使用

vue引入bing Map可能會遇到的問題

由于vue一般引用第三方插件是用import的方式進(jìn)行的,所以的在html中使用script標(biāo)簽引入bing Map SDK會出現(xiàn)兩種問題

1.在控制臺會報錯:Mirosorft is not defined

2.vue-cli會報錯:Mirosorft is not defined

這里的原因是由于異步加載,所以在調(diào)用"Mirosorft"的時候可能SDK并沒有引用成功

解決“Mirosorft is not defined”的錯誤

文檔參考

解決“Mirosorft is not defined”的錯誤,只要在項(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);
   // 等待頁面加載完畢回調(diào)
   let timeout = 0;
   let interval = setInterval(() => {
    // 超時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的時候,返回點(diǎn)擊點(diǎn)的經(jīng)緯度 (完成)
子組件觸發(fā)事件返回參數(shù)到父組件
當(dāng)已有經(jīng)緯度的時候,加載bingMap自動顯示其經(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ù)組,第一個元素是對象,第二個元素是字符串
 }
}

封裝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-QLZLh2rSjhd1Da9bfPhD'
   });
   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)緯度值
},

以上就是如何在vue項(xiàng)目中使用bing Map ,小編相信有部分知識點(diǎn)可能是我們?nèi)粘9ぷ鲿姷交蛴玫降?。希望你能通過這篇文章學(xué)到更多知識。更多詳情敬請關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。

本文題目:如何在vue項(xiàng)目中使用bingMap
分享網(wǎng)址:http://jinyejixie.com/article38/psphsp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、外貿(mào)建站、靜態(tài)網(wǎng)站、云服務(wù)器、手機(jī)網(wǎng)站建設(shè)、企業(yè)建站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作
临武县| 罗平县| 阳曲县| 凤山市| 迁西县| 石家庄市| 财经| 石嘴山市| 永善县| 合水县| 合川市| 江陵县| 大城县| 怀集县| 昌图县| 蒙山县| 方正县| 东安县| 新巴尔虎右旗| 嫩江县| 嵩明县| 碌曲县| 翁牛特旗| 资兴市| 兴文县| 台湾省| 门源| 临安市| 枣庄市| 宜春市| 驻马店市| 临沂市| 新泰市| 洞头县| 丹阳市| 榆树市| 长白| 汝城县| 宿松县| 通州市| 昌江|