Socket
Socket
Sign inDemoInstall

react-native-google-places-autocomplete

Package Overview
Dependencies
3
Maintainers
4
Versions
83
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    react-native-google-places-autocomplete

Customizable Google Places autocomplete component for iOS and Android React-Native apps


Version published
Weekly downloads
41K
decreased by-15.41%
Maintainers
4
Install size
383 kB
Created
Weekly downloads
 

Readme

Source

react-native-google-places-autocomplete

Customizable Google Places autocomplete component for iOS and Android React-Native apps

Preview

Installation

  1. npm install react-native-google-places-autocomplete --save
  2. Get your Google Places API keys and enable "Google Places API Web Service" (NOT Android or iOS) in the console.
  3. Enable "Google Maps Geocoding API" if you want to use GoogleReverseGeocoding for Current Location

Basic Example

Basic Address Search

import React from 'react';
import { Image, Text } from 'react-native';
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',
      }}
    />
  );
};

export default GooglePlacesInput;

You can also try the basic example in a snack here

More Examples

Get Current Location

Click to expand

Extra step required!

If you are targeting React Native 0.60.0+ you must install either @react-native-community/geolocation (link) or react-native-geolocation-service(link).

Please make sure you follow the installation instructions there and add navigator.geolocation = require(GEOLOCATION_PACKAGE) somewhere in you application before <GooglePlacesAutocomplete />.

import React from 'react';
import { Image, Text } from 'react-native';
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';

// navigator.geolocation = require('@react-native-community/geolocation');
// navigator.geolocation = require('react-native-geolocation-service');

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',
      }}
      currentLocation={true}
      currentLocationLabel='Current location'
    />
  );
};

export default GooglePlacesInput;

Search with predefined option

Click to expand
import React from 'react';
import { Image, Text } from 'react-native';
import { GooglePlacesAutocomplete } from 'react-native-google-places-autocomplete';

const homePlace = {
  description: 'Home',
  geometry: { location: { lat: 48.8152937, lng: 2.4597668 } },
};
const workPlace = {
  description: 'Work',
  geometry: { location: { lat: 48.8496818, lng: 2.2940881 } },
};

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',
      }}
      predefinedPlaces={[homePlace, workPlace]}
    />
  );
};

export default GooglePlacesInput;

Limit results to one country

Click to expand
import React from 'react';
import { Image, Text } from 'react-native';
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',
        components: 'country:us',
      }}
    />
  );
};

export default GooglePlacesInput;

Styling

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)

keytype
containerobject (View)
descriptionobject (Text style)
textInputContainerobject (View style)
textInputobject (style)
loaderobject (View style)
listViewobject (ListView style)
predefinedPlacesDescriptionobject (Text style)
poweredContainerobject (View style)
poweredobject (Image style)
separatorobject (View style)
rowobject (View style)

Example

<GooglePlacesAutocomplete
  placeholder='Enter Location'
  minLength={2}
  autoFocus={false}
  returnKeyType={'default'}
  fetchDetails={true}
  styles={{
    textInputContainer: {
      backgroundColor: 'rgba(0,0,0,0)',
      borderTopWidth: 0,
      borderBottomWidth: 0,
    },
    textInput: {
      marginLeft: 0,
      marginRight: 0,
      height: 38,
      color: '#5d5d5d',
      fontSize: 16,
    },
    predefinedPlacesDescription: {
      color: '#1faadb',
    },
  }}
/>

Web Support

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 can use a proxy server like CORS Anywhere or roll your own. Please be mindful of this limitation when opening an issue.

Note: The library expects the same response that the Google Maps API would return.

Features

  • Places autocompletion
  • iOS and Android compatibility
  • Places details fetching + ActivityIndicatorIOS/ProgressBarAndroid loaders
  • Customizable using the styles parameter
  • XHR cancellations when typing fast
  • Google Places terms compliant
  • Predefined places
  • typescript types
  • Current location

Changelog

  • 1.4.2+: Please see the releases tab for the changelog information.
  • 1.3.9 : Multiple bugfixes + fixed breaking change in React Native.
  • 1.3.6 : Fixed accuracy issue.
  • 1.3.5 : Fixed bug where input was being cleared.
  • 1.3.4 : Fixed bug where loading was breaking the component.
  • 1.3.3 : Fixed key prop warning and added loading indicator.
  • 1.3.2 : Added small feature which makes the request on componentDidMount() when you already have the default value set.
  • 1.3.1 : Update react-native peerDependecy. (> 0.46)
  • 1.3.0 : Added support for React 16 (isMounted() and propTypes bugfix), support for restricted API key and moving from ListView to Flatlist.
  • 1.2.12 : Fixed render description + docs.
  • 1.2.11 : Fixed current location result onPress event.
  • 1.2.10 : Set default debounce to 0. Fixed debounce typing lag.
  • 1.2.9 : Added isRowScrollable prop.
  • 1.2.8 : Added underlineColorAndroid, listUnderlayColor, renderLeftButton, renderRightButton props. Added nearbyPlacesAPI option None.

License

MIT

Authors

Keywords

FAQs

Last updated on 14 May 2020

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc