6
6
import "./GeoAddressSearch.scss" ;
7
7
import * as React from "react" ;
8
8
import { useActiveViewport } from "@itwin/appui-react" ;
9
- import { BaseMapLayerSettings } from "@itwin/core-common" ;
10
9
import { SvgCloseSmall , SvgSearch } from "@itwin/itwinui-icons-react" ;
11
10
import { ComboBox , IconButton } from "@itwin/itwinui-react" ;
12
- import { BingAddressProvider } from "../BingAddressProvider" ;
13
11
import { GeoTools } from "../GeoTools" ;
14
- import { GoogleAddressProvider } from "../GoogleAddressProvider" ;
15
12
import { IModelGeoView } from "../IModelGeoView" ;
16
13
17
14
import type { Viewport } from "@itwin/core-frontend" ;
@@ -22,52 +19,37 @@ import type { AddressData, AddressProvider } from "../AddressProvider";
22
19
*/
23
20
export interface GeoAddressSearchProps {
24
21
/** Address provider object */
25
- provider ?: AddressProvider ;
22
+ provider : AddressProvider ;
23
+
26
24
/** Indicates whether to set focus to the input element (default to true)*/
27
25
setFocus ?: boolean ;
28
26
}
29
-
30
- const isGoogleBaseMap = ( vp ?: Viewport ) : boolean => {
31
- return (
32
- vp ?. viewFlags . backgroundMap === true
33
- && vp ?. displayStyle . backgroundMapBase instanceof BaseMapLayerSettings
34
- && vp . displayStyle . backgroundMapBase . formatId === "GoogleMaps"
35
- ) ;
36
- } ;
37
- const isValidBaseMap = ( provider : AddressProvider , vp ?: Viewport ) => {
38
- const isGoogleAddressProvider = provider instanceof GoogleAddressProvider ;
39
- return ! isGoogleAddressProvider || ( isGoogleAddressProvider && isGoogleBaseMap ( vp ) ) ;
40
- }
41
-
42
27
/**
43
- * <GeoAddressSearch> react component
44
28
*/
45
29
export function GeoAddressSearch ( props : GeoAddressSearchProps ) {
30
+ const { provider} = props ;
31
+
46
32
const [ inputValue , setInputValue ] = React . useState ( "" ) ;
47
33
const [ options , setOptions ] = React . useState < SelectOption < string > [ ] > ( [ ] ) ;
48
34
const [ addressCache , setAddressCache ] = React . useState < AddressData [ ] > ( [ ] ) ;
49
35
const activeViewport = useActiveViewport ( ) ; // Hook to ensure the component re-renders when the active viewport changes
50
- const [ disabled , setDisabled ] = React . useState < boolean > ( ( ) => isGoogleBaseMap ( activeViewport ) ) ;
51
-
52
- // `React.useMemo' is used avoid creating new object on each render cycle
53
- // Default is Bing provider, but we might want to default to Google in the future
54
- const addressProvider = React . useMemo ( ( ) => props . provider ?? new BingAddressProvider ( ) , [ props . provider ] ) ;
36
+ const [ disabled , setDisabled ] = React . useState < boolean > ( ( ) => provider . isDisabled ( { viewport : activeViewport } ) ) ;
55
37
56
38
React . useEffect ( ( ) => {
57
- setDisabled ( ! isValidBaseMap ( addressProvider , activeViewport ) ) ;
39
+
58
40
return activeViewport ?. onDisplayStyleChanged . addListener (
59
- ( vp ) => { setDisabled ( ! isValidBaseMap ( addressProvider , vp ) ) ; }
41
+ ( vp ) => { setDisabled ( provider ?. isDisabled ( { viewport : vp } ) ) ; }
60
42
) ;
61
43
} , [ activeViewport ] ) ;
62
44
63
45
const onAddressSelected = async ( selected : string ) => {
64
46
setInputValue ( selected ) ;
65
47
let locatedByPosition = false
66
- if ( addressProvider . supportsAddressLocation ( ) ) {
48
+ if ( provider . supportsAddressLocation ( ) ) {
67
49
const address = addressCache . find ( ( addr ) => addr . formattedAddress === selected ) ;
68
50
if ( address !== undefined ) {
69
51
try {
70
- const location = await addressProvider . getLocation ( address ) ;
52
+ const location = await provider . getLocation ( address ) ;
71
53
if ( location ) {
72
54
locatedByPosition = await IModelGeoView . locatePosition ( location ) ;
73
55
}
@@ -83,7 +65,7 @@ export function GeoAddressSearch(props: GeoAddressSearchProps) {
83
65
const getAddressesFunc = async ( value : string ) : Promise < AddressData [ ] > => {
84
66
const viewBBox = IModelGeoView . getFrustumLonLatBBox ( ) ;
85
67
if ( viewBBox && value ) {
86
- const addr = await addressProvider . getSuggestions ( value , viewBBox ) ;
68
+ const addr = await provider . getSuggestions ( value , viewBBox ) ;
87
69
setAddressCache ( addr ) ;
88
70
return addr ;
89
71
}
0 commit comments