在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):FaridSafi/react-native-google-places-autocomplete开源软件地址(OpenSource Url):https://github.com/FaridSafi/react-native-google-places-autocomplete开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):Google Maps Search Component for React NativeCustomizable Google Places autocomplete component for iOS and Android React-Native apps Version 2 of this library is now available. See more in the releases section.
|
Prop Name | type | description | default value | Options |
---|---|---|---|---|
autoFillOnNotFound | boolean | displays the result from autocomplete if the place details api return not found | false | true | false |
currentLocation | boolean | Will add a 'Current location' button at the top of the predefined places list | false | true | false |
currentLocationLabel | string | change the display label for the current location button | Current Location | Any string |
debounce | number | debounce the requests (in ms) | 0 | |
disableScroll | boolean | disable scroll on the results list | ||
enableHighAccuracyLocation | boolean | use GPS or not. If set to true, a GPS position will be requested. If set to false, a WIFI location will be requested. use GPS or not. If set to true, a GPS position will be requested. If set to false, a WIFI location will be requested. | true | |
enablePoweredByContainer | boolean | show "powered by Google" at the bottom of the search results list | true | |
fetchDetails | boolean | get more place details about the selected option from the Place Details API | false | |
filterReverseGeocodingByTypes | array | filter the reverse geocoding results by types - ['locality', 'administrative_area_level_3'] if you want to display only cities | ||
GooglePlacesDetailsQuery | object | "query" object for the Google Place Details API (when you press on a suggestion) | ||
GooglePlacesSearchQuery | object | "query" object for the Google Places Nearby API (when you use current location to find nearby places) | { rankby: 'distance', type: 'restaurant' } |
|
GoogleReverseGeocodingQuery | object | "query" object for the Google Geocode API (when you use current location to get the current address) | ||
isRowScrollable | boolean | enable/disable horizontal scrolling of a list result https://reactnative.dev/docs/scrollview#scrollenabled | true | |
keepResultsAfterBlur | boolean | show list of results after blur | false | true | false |
keyboardShouldPersistTaps | string | Determines when the keyboard should stay visible after a tap https://reactnative.dev/docs/scrollview#keyboardshouldpersisttaps | 'always' | 'never' | 'always' | 'handled' |
listEmptyComponent | function | use FlatList's ListEmptyComponent prop when no autocomplete results are found. | ||
listUnderlayColor | string | underlay color of the list result when pressed https://reactnative.dev/docs/touchablehighlight#underlaycolor | '#c8c7cc' | |
listViewDisplayed | string | override the default behavior of showing the list (results) view | 'auto' | 'auto' | true | false |
minLength | number | minimum length of text to trigger a search | 0 | |
nearbyPlacesAPI | string | which API to use for current location | 'GooglePlacesSearch' | 'none' | 'GooglePlacesSearch' | 'GoogleReverseGeocoding' |
numberOfLines | number | number of lines (android - multiline must be set to true) https://reactnative.dev/docs/textinput#numberoflines | 1 | |
onFail | function | returns if an unspecified error comes back from the API | ||
onNotFound | function | returns if the Google Places Details API returns a 'not found' code (when you press a suggestion). | ||
onPress | function | returns when after a suggestion is selected | ||
onTimeout | function | callback when a request timeout | ()=>console.warn('google places autocomplete: request timeout') |
|
placeholder | string | placeholder text https://reactnative.dev/docs/textinput#placeholder | 'Search' | |
predefinedPlaces | array | Allows you to show pre-defined places (e.g. home, work) | ||
predefinedPlacesAlwaysVisible | boolean | Shows predefined places at the top of the search results | false | |
preProcess | function | do something to the text of the search input before a search request is sent | ||
query | object | "query" object for the Google Places Autocomplete API (link) | { key: 'missing api key', language: 'en', types: 'geocode' } |
|
renderDescription | function | determines the data passed to each renderRow (search result) | ||
renderHeaderComponent | function | use the ListHeaderComponent from FlatList when showing autocomplete results |
||
renderLeftButton | function | add a component to the left side of the Text Input | ||
renderRightButton | function | add a component to the right side of the Text Input | ||
renderRow | function | custom component to render each result row (use this to show an icon beside each result). data and index will be passed as input parameters |
||
requestUrl | object | used to set the request url for the library | ||
returnKeyType | string | the return key text https://reactnative.dev/docs/textinput#returnkeytype | 'search' | |
styles | object | See styles section below | ||
suppressDefaultStyles | boolean | removes all default styling from the library | false | true | false |
textInputHide | boolean | Hide the Search input | false | true | false |
textInputProps | object | define props for the textInput, or provide a custom input component | ||
timeout | number | how many ms until the request will timeout | 20000 |
method name | type | description |
---|---|---|
getAddressText |
() => string |
return the value of TextInput |
setAddressText |
(value: string) => void |
set the value of TextInput |
focus |
void |
makes the TextInput focus |
blur |
void |
makes the TextInput lose focus |
clear |
void |
removes all text from the TextInput |
isFocused |
() => boolean |
returns true if the TextInput is currently focused; false otherwise |
getCurrentLocation |
() => void |
makes a query to find nearby places based on current location |
You can access these methods using a ref.
import React, { useEffect, useRef } from 'react';
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';
const GooglePlacesInput = () => {
const ref = useRef();
useEffect(() => {
ref.current?.setAddressText('Some Text');
}, []);
return (
<GooglePlacesAutocomplete
ref={ref}
placeholder='Search'
onPress={(data, details = null) => {
// 'details' is provided when fetchDetails = true
console.log(data, details);
}}
query={{
key: 'YOUR API KEY',
language: 'en',
}}
/>
);
};
export default GooglePlacesInput;
GooglePlacesAutocomplete
can be easily customized to meet styles of your app. Pass styles props to GooglePlacesAutocomplete
with style object for different elements (keys for style object are listed below)
key | type |
---|---|
container | object (View) |
textInputContainer | object (View style) |
textInput | object (style) |
listView | object (ListView style) |
row | object (View style) |
loader | object (View style) |
description | object (Text style) |
predefinedPlacesDescription | object (Text style) |
separator | object (View style) |
poweredContainer | object (View style) |
powered | object (Image style) |
<GooglePlacesAutocomplete
placeholder='Enter Location'
minLength={2}
autoFocus={false}
returnKeyType={'default'}
fetchDetails={true}
styles={{
textInputContainer: {
backgroundColor: 'grey',
},
textInput: {
height: 38,
color: '#5d5d5d',
fontSize: 16,
},
predefinedPlacesDescription: {
color: '#1faadb',
},
}}
/>
{
container: {
flex: 1,
},
textInputContainer: {
flexDirection: 'row',
},
textInput: {
backgroundColor: '#FFFFFF',
height: 44,
borderRadius: 5,
paddingVertical: 5,
paddingHorizontal: 10,
fontSize: 15,
flex: 1,
},
poweredContainer: {
justifyContent: 'flex-end',
alignItems: 'center',
borderBottomRightRadius: 5,
borderBottomLeftRadius: 5,
borderColor: '#c8c7cc',
borderTopWidth: 0.5,
},
powered: {},
listView: {},
row: {
backgroundColor: '#FFFFFF',
padding: 13,
height: 44,
flexDirection: 'row',
},
separator: {
height: 0.5,
backgroundColor: '#c8c7cc',
},
description: {},
loader: {
flexDirection: 'row',
justifyContent: 'flex-end',
height: 20,
},
}
Web support can be enabled via the requestUrl
prop, by passing in a URL that you can use to proxy your requests. CORS implemented by the Google Places API prevent using this library directly on the web. You will need to use a proxy server. Please be mindful of this limitation when opening an issue.
The requestUrl
prop takes an object with two required properties: useOnPlatform
and url
, and an optional headers
property.
The url
property is used to set the url that requests will be made to. If you are using the regular google maps API, you need to make sure you are ultimately hitting https://maps.googleapis.com/maps/api.
useOnPlatform
configures when the proxy url is used. It can be set to either web
- will be used only when the device platform is detected as web (but not iOS or Android, or all
- will always be used.
You can optionally specify headers to apply to your request in the headers
object.
import React from 'react';
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';
const GooglePlacesInput = () => {
return (
<GooglePlacesAutocomplete
placeholder='Search'
onPress={(data, details = null) => {
// 'details' is provided when fetchDetails = true
console.log(data, details);
}}
query={{
key: 'YOUR API KEY',
language: 'en',
}}
requestUrl={{
useOnPlatform: 'web', // or "all"
url:
'https://cors-anywhere.herokuapp.com/https://maps.googleapis.com/maps/api', // or any proxy server that hits https://maps.googleapis.com/maps/api
headers: {
Authorization: `an auth token`, // if required for your proxy
},
}}
/>
);
};
export default GooglePlacesInput;
Note: The library expects the same response that the Google Maps API would return.
styles
parameterThis library does not use the iOS, Android or JS SDKs from Google. This comes with some Pros and Cons.
Pros:
Cons: