schangxiang@126.com
2025-09-19 9be9c3784b2881a3fa25e93ae2033dc2803c0ed0
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
80
81
82
83
84
85
86
87
88
89
<template>
  <div class="map-container">
    <div id="localMap"></div>
  </div>
</template>
 
<script>
import initBingMap from "./initMap.js";
export default {
  data() {
    return {
      lngNum: null, // 经度
      latNum: null // 纬度
    };
  },
  created: function() {
    // eslint-disable-next-line
    initBingMap.init().then(Microsoft => {
      // eslint-disable-next-line
      // console.log(Microsoft);
      // console.log("加载成功...");
      setTimeout(res => {
        this.initMap();
      }, 1000);
    });
  },
  methods: {
    initMap() {
      const _this = this;
      // eslint-disable-next-line
      let map = new Microsoft.Maps.Map("#localMap", {
        credentials:
          "AgzeobkGvmpdZTFuGa7_6gkaHH7CXHKsFiTQlBvi55x-QLZLh1rSjhd1Da9bfPhD"
      });
      // eslint-disable-next-line
      Microsoft.Maps.Events.addHandler(map, "click", _this.getClickLocation);
      var obj = {
        lng: -33.89546535000133,
        lat: 151.1866805450932
      };
      _this.getcenter(obj);
    },
    getClickLocation(e) {
      // 若点击到地图的标记上,而非地图上
      // eslint-disable-next-line
      let [_this, loc] = [this, null];
      if (e.targetType === "pushpin") {
        loc = e.target.getLocation();
      } else {
        // 若点击到地图上
        // eslint-disable-next-line
        var point = new Microsoft.Maps.Point(e.pageX, e.pageY);
        loc = e.target.tryPixelToLocation(
          point,
          // eslint-disable-next-line
          Microsoft.Maps.PixelReference.page
        );
      }
      // console.log(loc.latitude + ", " + loc.longitude);
      // console.log(loc);
      _this.lngNum = loc.longitude;
      _this.latNum = loc.latitude;
      const data = {
        lngNum: _this.lngNum,
        latNum: _this.latNum
      };
      this.$emit("getLocationNums", data);
    },
    getcenter(point) {
      // eslint-disable-next-line
      let map = new Microsoft.Maps.Map("#localMap", {
        credentials:
          "AgzeobkGvmpdZTFuGa7_6gkaHH7CXHKsFiTQlBvi55x-QLZLh1rSjhd1Da9bfPhD",
        // eslint-disable-next-line
        center: new Microsoft.Maps.Location(point.lng, point.lat)
      });
      map.getCenter();
    }
  }
};
</script>
 
<style scoped>
.map-container {
  width: 100%;
  height: 400px;
  border: 1px solid #000;
}
</style>