Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. const googleMap = new Vue({
  2. el: '#app',
  3. data: {
  4. map: null,
  5. features: [], // 存入每一個地點
  6. infowindowAll: {} // 存入每一個marker上的info windows
  7. },
  8. methods: {
  9. // init google map
  10. initMap() {
  11.  
  12. // 預設顯示的地點:台北市政府親子劇場
  13. let location = {
  14. lat: 25.0374865, // 經度
  15. lng: 121.5647688 // 緯度
  16. };
  17.  
  18. // 建立地圖
  19. this.map = new google.maps.Map(document.getElementById('map'), {
  20. center: location,
  21. zoom: 16,
  22. mapTypeId: 'terrain'
  23. });
  24.  
  25. // 放置多個marker
  26. fetch('./map.geojson')
  27. .then(results => results.json())
  28. .then(result => {
  29. this.features = result.features;
  30. Array.prototype.forEach.call(this.features, r => {
  31. let latLng = new google.maps.LatLng(r.geometry.coordinates[0], r.geometry.coordinates[1]);
  32. let marker = new google.maps.Marker({
  33. position: latLng,
  34. map: this.map
  35. });
  36.  
  37. // info window
  38. let infowindow = new google.maps.InfoWindow({
  39. content: `<h6>${r.properties.name}</h6>` // 支援html
  40. });
  41.  
  42. // 監聽 marker click 事件
  43. marker.addListener('click', e => {
  44. infowindow.open(this.map, marker);
  45. });
  46.  
  47. // 加一個open的method,就可由外部按鈕開啟
  48. this.infowindowAll[r.properties.id] = {
  49. open: function() {
  50. infowindow.open(this.map, marker);
  51. }
  52. };
  53.  
  54. });
  55.  
  56. });
  57.  
  58. },
  59. // 由外部按鈕開啟info windows
  60. openInfoWindows(id) {
  61. this.infowindowAll[id].open();
  62. }
  63. },
  64. created() {
  65. window.addEventListener('load', () => {
  66. this.initMap();
  67. });
  68. }
  69. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement