forked from spider-hand/GeoguessMaster
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a composable for map utilities
- Loading branch information
1 parent
285ed08
commit d3333ab
Showing
5 changed files
with
149 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { ref } from "vue"; | ||
import { describe, expect, test, vi } from "vitest"; | ||
import { useMap } from "../../../../composables/game/map"; | ||
import * as GoogleMaps from "@googlemaps/js-api-loader"; | ||
|
||
vi.spyOn(GoogleMaps, "Loader").mockImplementation(() => { | ||
return { | ||
importLibrary: () => | ||
vi.fn().mockResolvedValue(() => { | ||
return { | ||
Map: vi.fn(), | ||
}; | ||
}), | ||
} as any; | ||
}); | ||
|
||
describe("map", async () => { | ||
const loader = new GoogleMaps.Loader({ | ||
apiKey: "", | ||
version: "weekly", | ||
}); | ||
const { Map } = await loader.importLibrary("maps"); | ||
const mapRef = ref<HTMLElement>(); | ||
|
||
test("putMarker puts a marker", () => { | ||
const { markers, putMarker } = useMap(Map, mapRef); | ||
const latLng = new google.maps.LatLng(1, 1); | ||
putMarker(latLng); | ||
|
||
expect(markers.value.length).toBe(1); | ||
}); | ||
|
||
test("removeMarkers removes markers", () => { | ||
const { markers, putMarker, removeMarkers } = useMap(Map, mapRef); | ||
const latLng = new google.maps.LatLng(1, 1); | ||
putMarker(latLng); | ||
putMarker(latLng); | ||
|
||
expect(markers.value.length).toBe(2); | ||
|
||
removeMarkers(); | ||
|
||
expect(markers.value.length).toBe(0); | ||
}); | ||
|
||
test("drawPolyline draws a polyline", () => { | ||
const { polylines, drawPolyline } = useMap(Map, mapRef); | ||
const from = new google.maps.LatLng(1, 1); | ||
const to = new google.maps.LatLng(1, 1); | ||
drawPolyline(from, to); | ||
|
||
expect(polylines.value.length).toBe(1); | ||
}); | ||
|
||
test("removePolyline removes polylines", () => { | ||
const { polylines, drawPolyline, removePolyline } = useMap(Map, mapRef); | ||
const from = new google.maps.LatLng(1, 1); | ||
const to = new google.maps.LatLng(1, 1); | ||
drawPolyline(from, to); | ||
drawPolyline(from, to); | ||
|
||
expect(polylines.value.length).toBe(2); | ||
|
||
removePolyline(); | ||
|
||
expect(polylines.value.length).toBe(0); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { Ref, onMounted, shallowRef } from "vue"; | ||
|
||
export const useMap = ( | ||
Map: typeof google.maps.Map, | ||
mapRef: Ref<HTMLElement | undefined> | ||
) => { | ||
const map = shallowRef<google.maps.Map>(); | ||
const markers = shallowRef<google.maps.Marker[]>([]); | ||
const polylines = shallowRef<google.maps.Polyline[]>([]); | ||
|
||
const removeMarkers = (): void => { | ||
markers.value.forEach((marker) => { | ||
marker.setMap(null); | ||
}); | ||
markers.value.splice(0); | ||
}; | ||
|
||
const putMarker = (position: google.maps.LatLng): void => { | ||
const marker = new google.maps.Marker({ | ||
position: position, | ||
map: map.value, | ||
}); | ||
markers.value.push(marker); | ||
}; | ||
|
||
const drawPolyline = ( | ||
from: google.maps.LatLng, | ||
to: google.maps.LatLng | ||
): void => { | ||
const polyline = new google.maps.Polyline({ | ||
path: [from, to], | ||
strokeColor: "hsl(0, 100%, 63%)", | ||
}); | ||
polyline.setMap(map.value as google.maps.Map); | ||
polylines.value.push(polyline); | ||
}; | ||
|
||
const removePolyline = (): void => { | ||
polylines.value.forEach((line) => { | ||
line.setMap(null); | ||
}); | ||
polylines.value = []; | ||
}; | ||
|
||
onMounted(() => { | ||
if (mapRef.value) { | ||
map.value = new Map(mapRef.value as HTMLElement, { | ||
center: { lat: 37.86926, lng: -122.254811 }, | ||
zoom: 1, | ||
fullscreenControl: false, | ||
mapTypeControl: false, | ||
streetViewControl: false, | ||
}); | ||
} | ||
}); | ||
|
||
return { | ||
map, | ||
markers, | ||
polylines, | ||
removeMarkers, | ||
putMarker, | ||
drawPolyline, | ||
removePolyline, | ||
}; | ||
}; |