Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 476 | Rev 481 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 476 Rev 480
Línea 3... Línea 3...
3
const useLocationAutocomplete = (inputRef) => {
3
const useLocationAutocomplete = (inputRef) => {
4
    const [addresObject, setAddressObject] = useState({})
4
    const [addresObject, setAddressObject] = useState({})
5
    const [error, setError] = useState(null)
5
    const [error, setError] = useState(null)
6
    let autoComplete
6
    let autoComplete
Línea 7... Línea 7...
7
 
7
 
8
    function handleScriptLoad(input) {
8
    function handleScriptLoad(autoCompleteRef) {
-
 
9
        autoComplete = new window.google.maps.places.Autocomplete(
-
 
10
            autoCompleteRef,
-
 
11
            { types: ['(cities)'] }
9
        autoComplete = new window.google.maps.places.Autocomplete(input)
12
        )
10
        autoComplete.setFields([
13
        autoComplete.setFields([
11
            'address_components',
14
            'address_components',
12
            'formatted_address',
15
            'formatted_address',
13
            'geometry',
16
            'geometry',
14
        ])
17
        ])
15
        autoComplete.addListener('place_changed', handlePlaceSelect)
18
        autoComplete.addListener('place_changed', () => handlePlaceSelect())
Línea 16... Línea 19...
16
    }
19
    }
-
 
20
 
17
 
21
    function handlePlaceSelect() {
18
    function handlePlaceSelect() {
22
        setAddressObject({})
-
 
23
        const addressPlace = autoComplete.getPlace()
-
 
24
        const formattedAddress = addressPlace.formatted_address
-
 
25
        const addressComponents = [
-
 
26
            ...addressPlace.address_components,
-
 
27
            {
-
 
28
                latitude: addressPlace.geometry.location.lat(),
-
 
29
                longitude: addressPlace.geometry.location.lng(),
-
 
30
                types: ['geometry'],
19
        const addressObject = autoComplete.getPlace()
31
            },
Línea 20... Línea 32...
20
        const query = addressObject.formatted_address
32
        ]
21
        const address = {}
33
        const address = {}
22
 
34
 
23
        if (query) {
35
        if (formattedAddress) {
24
            setError(null)
36
            setError(null)
Línea 25... Línea 37...
25
            addresObject.address_components?.map((address_component) => {
37
            addressComponents?.map((address_component) => {
26
                const address_component_name = address_component.long_name
38
                const address_component_name = address_component.long_name
Línea 55... Línea 67...
55
                    default:
67
                    default:
56
                        break
68
                        break
57
                }
69
                }
58
            })
70
            })
Línea 59... Línea 71...
59
 
71
 
60
            setAddressObject({ ...address, formatted_address: query })
72
            setAddressObject({ ...address, formatted_address: formattedAddress })
61
        } else {
73
        } else {
62
            setError('Ha ocurrido un error')
74
            setError('Ha ocurrido un error')
63
        }
75
        }
Línea 67... Línea 79...
67
        if (inputRef) {
79
        if (inputRef) {
68
            handleScriptLoad(inputRef)
80
            handleScriptLoad(inputRef)
69
        }
81
        }
70
    }, [inputRef])
82
    }, [inputRef])
Línea 71... Línea 83...
71
 
83
 
72
    return { ...addresObject, error }
84
    return { address: addresObject, error }
Línea 73... Línea 85...
73
}
85
}