-
Notifications
You must be signed in to change notification settings - Fork 13
/
street-view-panorama.ts
75 lines (60 loc) · 1.83 KB
/
street-view-panorama.ts
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
/* eslint-disable complexity */
import {useContext, useEffect, useState} from 'react';
import {GoogleMapsContext} from '../google-maps-provider';
export interface StreetViewPanoramaProps {
divElement?: HTMLElement | null;
position?: google.maps.LatLng | google.maps.LatLngLiteral;
pov?: google.maps.StreetViewPov;
zoom?: number;
}
/**
* Hook to get Street View Panorama
*/
export const useStreetViewPanorama = (
props: StreetViewPanoramaProps
): google.maps.StreetViewPanorama | null => {
const {divElement, position, pov, zoom} = props;
const {googleMapsAPIIsLoaded, map} = useContext(GoogleMapsContext);
const [streetViewPanorama, setStreetViewPanorama] =
useState<google.maps.StreetViewPanorama | null>(null);
// Creates a Street View instance
useEffect(() => {
// If no div element is passed, initialize a map with Street View Panorama
if (!divElement) {
// Wait for Google Maps map instance
if (!map) {
return (): void => {};
}
const newPanorama = map.getStreetView();
if (pov) {
newPanorama.setPov(pov);
}
if (position) {
newPanorama.setPosition(position);
}
// eslint-disable-next-line no-eq-null
if (zoom != null) {
newPanorama.setZoom(zoom);
}
setStreetViewPanorama(newPanorama);
} else {
// Wait for Google Maps API
if (!googleMapsAPIIsLoaded) {
return (): void => {};
}
// If a div element is passed, initialize street view in the element
const newPanorama = new google.maps.StreetViewPanorama(divElement, {
position,
pov,
zoom
});
setStreetViewPanorama(newPanorama);
}
return (): void => {
if (!divElement && map) {
map.setStreetView(null);
}
};
}, [map, divElement]);
return streetViewPanorama;
};