Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the value selected using react-native-google-places-autocomplete ?

I am using react-native-google-places-autocomplete to select a location. I want to extract the location selected and use it as a props to be passed to another function. The value should be of type String. I do not how to extract the selected description value. Here is what I have so far:

<GooglePlacesAutocomplete
                    placeholder='Event Location'
                    minLength={2} // minimum length of text to search
                    autoFocus={false}
                    // Can be left out for default return key https://facebook.github.io/react-native/docs/textinput.html#returnkeytype
                    listViewDisplayed='auto'    // true/false/undefined
                    fetchDetails={true}
                    renderDescription={row => row.description} // custom description render
                    onPress={(data, details = null) => { // 'details' is provided when fetchDetails = true
                      //console.warn(data, details);
                       props.location = data.description;
                    }}
                    textInputProps={{
                      onChangeText: (text) => { console.warn(text) }
                    }}
                    getDefaultValue={() => ''}

                    query={{
                      // available options: https://developers.google.com/places/web-service/autocomplete
                      key: 'AIzaSyCKwbZNUyUIx2X0XBBlWbhPu_unz5_1o3E',
                      language: 'en', // language of the results
                      types: 'geocode' // default: 'geocode'
                    }}

                    styles={{
                      textInputContainer: {
                        backgroundColor: 'rgba(0,0,0,0)',
                        borderTopWidth: 0,
                        borderBottomWidth:0,
                      },
                      description: {
                        fontWeight: 'bold',
                      },
                      textInput: {
                      marginLeft: 22,
                      marginRight: 0,
                      height: 38,
                      color: '#5d5d5d',
                      fontSize: 16,
                    },
                      predefinedPlacesDescription: {
                        color: '#1faadb'
                      }
                    }}
                    value={props.location}
                    onChangeText={props.onLocationChange}
                    renderLeftButton={()  => <Text style={{ marginTop: 12, marginLeft:16, fontSize: 18 }}> Location </Text>}
                    nearbyPlacesAPI='GooglePlacesSearch' // Which API to use: GoogleReverseGeocoding or GooglePlacesSearch
                    GoogleReverseGeocodingQuery={{
                      // available options for GoogleReverseGeocoding API : https://developers.google.com/maps/documentation/geocoding/intro
                    }}
                    GooglePlacesSearchQuery={{
                      // available options for GooglePlacesSearch API : https://developers.google.com/places/web-service/search
                      rankby: 'distance',
                      types: 'food'
                    }}

                    filterReverseGeocodingByTypes={['locality', 'administrative_area_level_3']} // filter the reverse geocoding results by types - ['locality', 'administrative_area_level_3'] if you want to display only cities
                    debounce={200} // debounce the requests in ms. Set to 0 to remove debounce. By default 0ms.

                  />
like image 244
jungleMan Avatar asked Jan 27 '26 10:01

jungleMan


1 Answers

You will get the selected location in onPress callback of the component.

   onPress={(data, details = null) => {
                // 'details' is provided when fetchDetails = true
                this.setState(
                  {
                    address: data.description, // selected address
                    coordinates: `${details.geometry.location.lat},${details.geometry.location.lng}` // selected coordinates
                  }
                );
              }}
like image 178
Suyog K.C Avatar answered Jan 31 '26 13:01

Suyog K.C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!