|
| 1 | +<script setup lang="ts"> |
| 2 | +import type { MapOptions } from 'ts-maps' |
| 3 | +import { computed, reactive, ref } from 'vue' |
| 4 | +
|
| 5 | +type MapName = 'world' | 'world-merc' | 'us-merc' | 'us-mill' | 'us-lcc' | 'us-aea' | 'spain' | 'italy' | 'canada' |
| 6 | +
|
| 7 | +interface EventData { |
| 8 | + type: string |
| 9 | + code?: string |
| 10 | + marker?: any |
| 11 | + time: string |
| 12 | +} |
| 13 | +
|
| 14 | +const isDarkTheme = ref(false) |
| 15 | +const lastEvent = ref<EventData | null>(null) |
| 16 | +const currentMap = ref<MapName>('world') |
| 17 | +
|
| 18 | +const mapOptions = [ |
| 19 | + { value: 'world', label: 'World Map', projection: 'miller' }, |
| 20 | + { value: 'world-merc', label: 'World (Mercator)', projection: 'mercator' }, |
| 21 | + { value: 'us-merc', label: 'USA (Mercator)', projection: 'mercator' }, |
| 22 | + { value: 'us-mill', label: 'USA (Miller)', projection: 'miller' }, |
| 23 | + { value: 'us-lcc', label: 'USA (Lambert)', projection: 'lambert' }, |
| 24 | + { value: 'us-aea', label: 'USA (Albers)', projection: 'albers' }, |
| 25 | + { value: 'spain', label: 'Spain', projection: 'mercator' }, |
| 26 | + { value: 'italy', label: 'Italy', projection: 'mercator' }, |
| 27 | + { value: 'canada', label: 'Canada', projection: 'mercator' }, |
| 28 | +] |
| 29 | +
|
| 30 | +const options = reactive<Omit<MapOptions, 'selector'>>({ |
| 31 | + backgroundColor: '#ffffff', |
| 32 | + zoomOnScroll: true, |
| 33 | + zoomButtons: true, |
| 34 | + regionsSelectable: true, |
| 35 | + markersSelectable: true, |
| 36 | +
|
| 37 | + regionStyle: { |
| 38 | + initial: { |
| 39 | + fill: '#e4e4e4', |
| 40 | + stroke: '#ffffff', |
| 41 | + strokeWidth: 0.5, |
| 42 | + }, |
| 43 | + hover: { |
| 44 | + fill: '#2ca25f', |
| 45 | + }, |
| 46 | + selected: { |
| 47 | + fill: '#1a9850', |
| 48 | + }, |
| 49 | + }, |
| 50 | + visualizeData: { |
| 51 | + scale: ['#C8EEFF', '#0071A4'], |
| 52 | + values: { |
| 53 | + 'US-CA': 100, // California |
| 54 | + 'US-TX': 85, // Texas |
| 55 | + 'US-FL': 80, // Florida |
| 56 | + 'US-NY': 75, // New York |
| 57 | + 'US-IL': 70, // Illinois |
| 58 | + 'US-PA': 65, // Pennsylvania |
| 59 | + }, |
| 60 | + }, |
| 61 | + markers: [ |
| 62 | + { |
| 63 | + name: 'Sample Marker', |
| 64 | + coords: [40.7128, -74.0060], |
| 65 | + style: { |
| 66 | + fill: '#ff0000', |
| 67 | + stroke: '#ffffff', |
| 68 | + r: 5, |
| 69 | + }, |
| 70 | + }, |
| 71 | + ], |
| 72 | +}) |
| 73 | +
|
| 74 | +const currentProjection = computed(() => { |
| 75 | + const selectedMap = mapOptions.find(map => map.value === currentMap.value) |
| 76 | +
|
| 77 | + if (!selectedMap) |
| 78 | + return 'unknown' |
| 79 | +
|
| 80 | + return selectedMap.projection |
| 81 | +}) |
| 82 | +
|
| 83 | +function toggleTheme() { |
| 84 | + isDarkTheme.value = !isDarkTheme.value |
| 85 | + options.backgroundColor = isDarkTheme.value ? '#2c3e50' : '#ffffff' |
| 86 | + if (options.regionStyle) { |
| 87 | + options.regionStyle = { |
| 88 | + ...options.regionStyle, |
| 89 | + initial: { |
| 90 | + ...options.regionStyle.initial, |
| 91 | + fill: isDarkTheme.value ? '#34495e' : '#e4e4e4', |
| 92 | + stroke: isDarkTheme.value ? '#1a1a1a' : '#ffffff', |
| 93 | + }, |
| 94 | + } |
| 95 | + } |
| 96 | +} |
| 97 | +
|
| 98 | +function addRandomMarker() { |
| 99 | + const lat = Math.random() * 180 - 90 |
| 100 | + const lng = Math.random() * 360 - 180 |
| 101 | + const marker = { |
| 102 | + name: `Marker ${options.markers?.length ?? 0 + 1}`, |
| 103 | + coords: [lat, lng] as [number, number], |
| 104 | + style: { |
| 105 | + fill: `#${Math.floor(Math.random() * 16777215).toString(16)}`, |
| 106 | + stroke: '#ffffff', |
| 107 | + r: 5, |
| 108 | + }, |
| 109 | + } |
| 110 | + options.markers = [...(options.markers || []), marker] |
| 111 | +} |
| 112 | +
|
| 113 | +function toggleZoom() { |
| 114 | + options.zoomOnScroll = !options.zoomOnScroll |
| 115 | +} |
| 116 | +
|
| 117 | +function handleRegionClick(_event: MouseEvent, code: string) { |
| 118 | + lastEvent.value = { |
| 119 | + type: 'Region Click', |
| 120 | + code, |
| 121 | + time: new Date().toLocaleTimeString(), |
| 122 | + } |
| 123 | +} |
| 124 | +
|
| 125 | +function handleMarkerClick(_event: MouseEvent, index: string) { |
| 126 | + lastEvent.value = { |
| 127 | + type: 'Marker Click', |
| 128 | + marker: options.markers?.[Number.parseInt(index)], |
| 129 | + time: new Date().toLocaleTimeString(), |
| 130 | + } |
| 131 | +} |
| 132 | +
|
| 133 | +function handleLoaded() { |
| 134 | + options.projection = 'albers' |
| 135 | + lastEvent.value = { |
| 136 | + type: 'Map Loaded', |
| 137 | + time: new Date().toLocaleTimeString(), |
| 138 | + } |
| 139 | +} |
| 140 | +</script> |
| 141 | + |
1 | 142 | <template>
|
2 |
| - <TestComponent /> |
| 143 | + <div class="container"> |
| 144 | + <h1>ts-maps Vue Example</h1> |
| 145 | + |
| 146 | + <div class="controls"> |
| 147 | + <div class="control-group"> |
| 148 | + <label for="map-select">Select Map:</label> |
| 149 | + <select id="map-select" v-model="currentMap"> |
| 150 | + <option v-for="map in mapOptions" :key="map.value" :value="map.value"> |
| 151 | + {{ map.label }} ({{ map.projection }}) |
| 152 | + </option> |
| 153 | + </select> |
| 154 | + </div> |
| 155 | + |
| 156 | + <div class="control-info"> |
| 157 | + Current Projection: <strong>{{ currentProjection }}</strong> |
| 158 | + </div> |
| 159 | + |
| 160 | + <button @click="toggleTheme"> |
| 161 | + Toggle Theme |
| 162 | + </button> |
| 163 | + <button @click="addRandomMarker"> |
| 164 | + Add Random Marker |
| 165 | + </button> |
| 166 | + <button @click="toggleZoom"> |
| 167 | + {{ options.zoomOnScroll ? 'Disable' : 'Enable' }} Zoom |
| 168 | + </button> |
| 169 | + </div> |
| 170 | + |
| 171 | + <div class="map-container"> |
| 172 | + <UnitedStates |
| 173 | + :key="currentMap" |
| 174 | + :options="options" |
| 175 | + height="500px" |
| 176 | + @region-click="handleRegionClick" |
| 177 | + @marker-click="handleMarkerClick" |
| 178 | + @loaded="handleLoaded" |
| 179 | + > |
| 180 | + <template #loading> |
| 181 | + <div class="custom-loading"> |
| 182 | + Loading your beautiful map... |
| 183 | + </div> |
| 184 | + </template> |
| 185 | + </UnitedStates> |
| 186 | + </div> |
| 187 | + |
| 188 | + <div v-if="lastEvent" class="info-panel"> |
| 189 | + <h3>Last Event</h3> |
| 190 | + <pre>{{ lastEvent }}</pre> |
| 191 | + </div> |
| 192 | + </div> |
3 | 193 | </template>
|
| 194 | + |
| 195 | +<style scoped> |
| 196 | +.container { |
| 197 | + max-width: 1200px; |
| 198 | + margin: 0 auto; |
| 199 | + padding: 20px; |
| 200 | + font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; |
| 201 | +} |
| 202 | +
|
| 203 | +.controls { |
| 204 | + margin: 20px 0; |
| 205 | + padding: 20px; |
| 206 | + background: #f5f5f5; |
| 207 | + border-radius: 8px; |
| 208 | + display: flex; |
| 209 | + flex-wrap: wrap; |
| 210 | + gap: 15px; |
| 211 | + align-items: center; |
| 212 | +} |
| 213 | +
|
| 214 | +.control-group { |
| 215 | + display: flex; |
| 216 | + align-items: center; |
| 217 | + gap: 10px; |
| 218 | +} |
| 219 | +
|
| 220 | +.control-group label { |
| 221 | + font-weight: 500; |
| 222 | +} |
| 223 | +
|
| 224 | +.control-group select { |
| 225 | + padding: 8px; |
| 226 | + border-radius: 4px; |
| 227 | + border: 1px solid #ddd; |
| 228 | + background: white; |
| 229 | + min-width: 200px; |
| 230 | +} |
| 231 | +
|
| 232 | +.controls button { |
| 233 | + padding: 8px 16px; |
| 234 | + border: none; |
| 235 | + border-radius: 4px; |
| 236 | + background: #2ca25f; |
| 237 | + color: white; |
| 238 | + cursor: pointer; |
| 239 | +} |
| 240 | +
|
| 241 | +.controls button:hover { |
| 242 | + background: #1a9850; |
| 243 | +} |
| 244 | +
|
| 245 | +.map-container { |
| 246 | + border: 1px solid #ddd; |
| 247 | + border-radius: 8px; |
| 248 | + overflow: hidden; |
| 249 | +} |
| 250 | +
|
| 251 | +.custom-loading { |
| 252 | + display: flex; |
| 253 | + align-items: center; |
| 254 | + justify-content: center; |
| 255 | + height: 100%; |
| 256 | + background: rgba(255, 255, 255, 0.9); |
| 257 | + font-size: 1.2em; |
| 258 | + color: #666; |
| 259 | +} |
| 260 | +
|
| 261 | +.info-panel { |
| 262 | + margin-top: 20px; |
| 263 | + padding: 15px; |
| 264 | + background: #f9f9f9; |
| 265 | + border-radius: 8px; |
| 266 | +} |
| 267 | +
|
| 268 | +.control-info { |
| 269 | + padding: 8px 16px; |
| 270 | + background: #fff; |
| 271 | + border-radius: 4px; |
| 272 | + border: 1px solid #ddd; |
| 273 | +} |
| 274 | +
|
| 275 | +.control-info strong { |
| 276 | + color: #2ca25f; |
| 277 | +} |
| 278 | +</style> |
0 commit comments