This time I will bring you a detailed explanation of the steps for using bing Map. What are the precautions when using bing Map? Here are practical cases, let’s take a look.
Write it at the front
It seems that only Baidu Maps has a global database in China, not Amap, Sogou, or Tencent, but Since the data of Baidu Map is not updated in a timely manner, it is best to use bingMap when doing related projects that require the use of foreign data.
bing Map usage tutorial (basic)
Reference document: bing Map official tutorial
## Bing Map initialization
Introduce bing map resources
1 | <script type='text/javascript' src='http:
|
Copy after login
Initialize the map
1 2 3 4 5 6 7 8 | <p id= "myMap" ></p>
<script type='text/javascript'>
function GetMap()
{
var map = new Microsoft.Maps.Map( '#myMap' );
}
</script>
|
Copy after login
Set map control parameters
Commonly used control parametersbranch
Which branch of the map sdk is loaded: release (default), experimental
callback
Callback after the map control script is loaded (default: GetMap)
key
userKey used by the user (details)
setLang
Specify the language used for map labels and navigation controls
Commonly used : Mainland China (zh-CN), Hong Kong (zh-HK), Simplified Chinese (zh-Hans), Taiwan (zh-TW), English-UK (en-GB), English-United States (en-US)
setMkt (details)
UR (details)
Add map events to bing map (reference)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | Microsoft.Maps.Events.addHandler(你的地图名称, 触发地图事件名称, function () { 触发的事件 });
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' ); });
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' ); });
Microsoft.Maps.Events.addHandler(map, 'maptypechanged' , function () { highlight( 'maptypechanged' ); });
|
Copy after login
bing Map Add pin (details)
Basic Pin Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | 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();
var pin = new Microsoft.Maps.Pushpin(center, {
title: 'Microsoft' ,
subTitle: 'City Center' ,
text: '1'
color: 'red' ,
});
map.entities.push(pin);
}
|
Copy after login
demo_1

demo_2

Add a custom image pin (details)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | function GetMap() {
var map = new Microsoft.Maps.Map( '#myMap' ,
{
credentials: 'You Bing Maps Key'
});
var center = map.getCenter();
var pin = new Microsoft.Maps.Pushpin(center, {
icon: 'images/poi_custom.png' ,
anchor: new Microsoft.Maps.Point(12, 39)
});
map.entities.push(pin);
}
|
Copy after login
Custom icon pin

bing Map Add events to push pins
Core code
1 2 3 4 5 6 7 8 9 10 | var pushpin = new Microsoft.Maps.Pushpin(map.getCenter());
map.entities.push(pushpin);
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' ); });
|
Copy after login
bing Map Add hover style to push pins
The core is still for bing Map Add events to push pins and modify the style of push pins through events
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | 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 });
});
|
Copy after login
Add hover style to push pins

bing Map fixed anchor point
One of the most common issues developers run into when using custom pins is that when they zoom the map, it looks like their pins are drifting to or from Move away from where it was anchored. This is due to incorrect anchor point values in the pin options. The anchor point specifies which pixel coordinate of the image, relative to the upper left corner of the image, should overlap the pin position coordinate.
CommonConfiguration Reference
bing Map Using
vue may introduce bing Map in vue Problems you will encounter
Since Vue generally refers to third-party plug-ins through import, there will be two problems when using script tags to introduce bing Map SDK in HTML
1 . An error will be reported in the console: Mirosorft is not defined
2.vue-cli will report an error: Mirosorft is not defined
The reason here is due to asynchronous loading, so when calling "Mirosorft" Sometimes the SDK may not be referenced successfully
Solving the error "Mirosoft is not defined"
Document reference
To solve the "Mirosoft is not defined" error, just ensure that the relevant tool classes can be correctly introduced in the project before calling the map.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | export default {
init: function (){
console.log( "初始化bing地图脚本..." );
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;
}
let scriptNode = document.createElement( "script" );
scriptNode.setAttribute( "type" , "text/javascript" );
scriptNode.setAttribute( "src" , BingMap_URL);
document.body.appendChild(scriptNode);
let timeout = 0;
let interval = setInterval(() => {
if (timeout >= 20) {
reject();
clearInterval(interval);
console.error( "bing地图脚本初始化失败..." );
}
if (typeof Microsoft !== "undefined" ) {
resolve(Microsoft);
clearInterval(interval);
console.log( "bing地图脚本初始化成功..." );
}
timeout += 1;
}, 500);
});
}
}
import bingMap from './**/bing-map' ;
bingMap.init()
.then((Microsoft) => {
console.log(Microsoft)
console.log( "加载成功..." )
})
|
Copy after login
集成bing Map组件到vue中
需要达到的功能
在vue项目中成功加载bing Map (完成)
当点击bing Map的时候,返回点击点的经纬度 (完成)
子组件触发事件返回参数到父组件
当已有经纬度的时候,加载bingMap自动显示其经纬度所在的位置并设置图钉 (待完成)
子组件触发事件返回参数到父组件
实现原理
vue-$meit
核心代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <template>
<p @click= "iclick" ></p>
</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);
}
}
|
Copy after login
封装bing Map通用组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 | <template>
<p class = "map-container" >
<p id= "localMap" ></p>
</p>
</template>
<script>
import initBingMap from './initMap.js'
export default {
data () {
return {
lngNum: null,
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) {
let [_this, loc] = [this, null];
if (e.targetType == 'pushpin' ) {
loc = e.target.getLocation();
}
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>
在组件中调用bing Map通用组件
import bingMapsLayer from 'bingMap.vue'
components: {
bingMapsLayer
},
<bing-maps-layer @getLocationNums= "getLocationNums" ></bing-maps-layer>
getLocationNums (...data) {
let _this = this;
console.log( 'click' );
console.log(data);
},
|
Copy after login
相信看了本文案例你已经掌握了方法,更多精彩请关注php中文网其它相关文章!
推荐阅读:
JS对图片进行黑白化设置
vue select组件开启与禁用方法详解
The above is the detailed content of Detailed explanation of the steps to use Bing Map. For more information, please follow other related articles on the PHP Chinese website!