vector-maps.mdx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. ---
  2. title: Vector Maps
  3. description: Interactive guide to creating vector maps with jsVectorMap.
  4. summary: Vector maps are a great way to display geographical data in an interactive and visually appealing way. Learn how to create vector maps with jsVectorMap.
  5. ---
  6. ## Installation
  7. To use vector maps in your project, you need to include the jsVectorMap library in your project. You can install it via npm or include it directly from a CDN. The following example demonstrates how to include the jsVectorMap library from a CDN:
  8. ```html
  9. <script src="$TABLER_CDN/dist/libs/jsvectormap/dist/js/jsvectormap.min.js"></script>
  10. <script src="$TABLER_CDN/dist/libs/jsvectormap/dist/maps/js/jsvectormap-world.js"></script>
  11. ```
  12. ## Sample demo
  13. Integrating the vector map into your website is straightforward. Below is a sample implementation for a world map:
  14. ```html
  15. <div id="map-world" class="w-100 h-100"></div>
  16. <script>
  17. document.addEventListener("DOMContentLoaded", function() {
  18. const map = new jsVectorMap({
  19. selector: '#map-world',
  20. map: 'world',
  21. });
  22. });
  23. </script>
  24. ```
  25. Look at the example below to see how the vector map works with a world map.
  26. ```html example vendors height="30rem" libs="jsvectormap,jsvectormap-world" columns={2} centered
  27. <div class="card">
  28. <div class="card-body">
  29. <div class="ratio ratio-16x9">
  30. <div>
  31. <div id="map-world" class="w-100 h-100"></div>
  32. </div>
  33. </div>
  34. </div>
  35. </div>
  36. <script>
  37. document.addEventListener("DOMContentLoaded", function() {
  38. const map = new jsVectorMap({
  39. selector: '#map-world',
  40. map: 'world',
  41. backgroundColor: 'transparent',
  42. regionStyle: {
  43. initial: {
  44. fill: tabler.getColor('body-bg'),
  45. stroke: tabler.getColor('border-color'),
  46. strokeWidth: 2,
  47. }
  48. },
  49. zoomOnScroll: false,
  50. zoomButtons: false,
  51. });
  52. window.addEventListener("resize", () => {
  53. map.updateSize();
  54. });
  55. });
  56. </script>
  57. ```
  58. ## Markers
  59. You can add markers to the map to highlight specific locations. Below is a sample implementation for a world map with markers:
  60. ```html example vendors height="30rem" libs="jsvectormap,jsvectormap-world-merc" columns={2} centered
  61. <div class="card">
  62. <div class="card-body">
  63. <div class="ratio ratio-16x9">
  64. <div>
  65. <div id="map-world-markers" class="w-100 h-100"></div>
  66. </div>
  67. </div>
  68. </div>
  69. </div>
  70. <script>
  71. // @formatter:on
  72. document.addEventListener("DOMContentLoaded", function() {
  73. const map = new jsVectorMap({
  74. selector: '#map-world-markers',
  75. map: 'world_merc',
  76. backgroundColor: 'transparent',
  77. regionStyle: {
  78. initial: {
  79. fill: tabler.getColor('body-bg'),
  80. stroke: tabler.getColor('border-color'),
  81. strokeWidth: 2,
  82. }
  83. },
  84. zoomOnScroll: false,
  85. zoomButtons: false,
  86. markers: [
  87. {
  88. coords: [61.524, 105.3188],
  89. name: "Russia",
  90. },
  91. {
  92. coords: [56.1304, -106.3468],
  93. name: "Canada",
  94. },
  95. {
  96. coords: [71.7069, -42.6043],
  97. name: "Greenland",
  98. },
  99. {
  100. coords: [26.8206, 30.8025],
  101. name: "Egypt",
  102. },
  103. {
  104. coords: [-14.235, -51.9253],
  105. name: "Brazil",
  106. },
  107. {
  108. coords: [35.8617, 104.1954],
  109. name: "China",
  110. },
  111. {
  112. coords: [37.0902, -95.7129],
  113. name: "United States",
  114. },
  115. {
  116. coords: [60.472024, 8.468946],
  117. name: "Norway",
  118. },
  119. {
  120. coords: [48.379433, 31.16558],
  121. name: "Ukraine",
  122. },
  123. ],
  124. markerStyle: {
  125. initial: {
  126. r: 4,
  127. stroke: '#fff',
  128. opacity: 1,
  129. strokeWidth: 3,
  130. stokeOpacity: .5,
  131. fill: tabler.getColor('blue')
  132. },
  133. hover: {
  134. fill: tabler.getColor('blue'),
  135. stroke: tabler.getColor('blue')
  136. }
  137. },
  138. markerLabelStyle: {
  139. initial: {
  140. fontSize: 10
  141. },
  142. },
  143. labels: {
  144. markers: {
  145. render: function(marker) {
  146. return marker.name
  147. },
  148. },
  149. },
  150. });
  151. window.addEventListener("resize", () => {
  152. map.updateSize();
  153. });
  154. });
  155. // @formatter:off
  156. </script>
  157. ```