Coder Social home page Coder Social logo

kevinguebert / react-native-google-places-autocomplete Goto Github PK

View Code? Open in Web Editor NEW

This project forked from faridsafi/react-native-google-places-autocomplete

1.0 2.0 0.0 780 KB

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

License: MIT License

JavaScript 79.79% Java 6.37% Objective-C 13.84%

react-native-google-places-autocomplete's Introduction

react-native-google-places-autocomplete

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

Customizations

This PR (FaridSafi#191) added a loading indicator on search and removed in a code refactor and cleanup seen here (FaridSafi@14c4613#diff-e574d057fb2c5e71b74e6125228256e3). I still need this as the default was not working. This is just adding that code back in.

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

Example

import React from 'react';
import { View, Image } 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'
      minLength={2} // minimum length of text to search
      autoFocus={false}
      returnKeyType={'search'} // 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.log(data, details);
      }}
      
      getDefaultValue={() => ''}
      
      query={{
        // available options: https://developers.google.com/places/web-service/autocomplete
        key: 'YOUR API KEY',
        language: 'en', // language of the results
        types: '(cities)' // default: 'geocode'
      }}
      
      styles={{
        textInputContainer: {
          width: '100%'
        },
        description: {
          fontWeight: 'bold'
        },
        predefinedPlacesDescription: {
          color: '#1faadb'
        }
      }}
      
      currentLocation={true} // Will add a 'Current location' button at the top of the predefined places list
      currentLocationLabel="Current location"
      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
      predefinedPlaces={[homePlace, workPlace]}

      debounce={200} // debounce the requests in ms. Set to 0 to remove debounce. By default 0ms.
      renderLeftButton={()  => <Image source={require('path/custom/left-icon')} />}
      renderRightButton={() => <Text>Custom text after the input</Text>}
    />
  );
}

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)

key type
container object (View)
description object (Text style)
textInputContainer object (View style)
textInput object (style)
loader object (View style)
listView object (ListView style)
predefinedPlacesDescription object (Text style)
poweredContainer object (View style)
powered object (Image style)
separator object (View style)
row object (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'
    },
  }}
  currentLocation={false}
/>

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
  • Current location
  • Predefined places

Changelog

  • 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

react-native-google-places-autocomplete's People

Contributors

7772 avatar andrey-semin avatar aniruddhashevle avatar brainiac84 avatar codenamev avatar davidstoker avatar esganzerla avatar faridsafi avatar grabcode avatar guilhermepontes avatar halilb avatar hartmamt avatar hmassonn avatar j-mendez avatar jamesone avatar jmpridgen avatar kanerogers avatar kevinguebert avatar lfkwtz avatar linux08 avatar nak1b avatar oliveroneill avatar rajanjs avatar santilorenzo avatar splttrevor avatar tmaximini avatar tusharkotlapure avatar vitorebatista avatar vond avatar waqas19921 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.