Giter Club home page Giter Club logo

rxplaces's Introduction

RxPlaces Build Status Download

A reactive approach for the Google Maps Webservice API

Current features:

  • Places Autocomplete
  • Reverse Geocoding from given place_id

Download

compile 'com.a99.rxplaces:library:0.1.0-alpha3'

Proguard Rules

Add the following line to your proguard-rules.pro

-keep class com.a99.rxplaces.** { *; }

Places Autocomplete

Initialization

You should use the function create(GOOGLE_MAPS_API_AUTOCOMPLETE_KEY), where GOOGLE_MAPS_API_AUTOCOMPLETE_KEY is the generated key from the Google developer console:

val rxAutocomplete = RxAutocomplete.create(GOOGLE_MAPS_API_AUTOCOMPLETE_KEY)

Usage

You can observe an EditText:

rxAutocomplete.observe(editText, options)

You can also observe any String data source:

Observable<String> dataSource = Observable.just("place");

rxAutocomplete.observe(dataSource, options)

Options Parameters

You can customize any value provided from the Google Maps Place Autocomplete webservice:

val options = AutocompleteOptions.create {
  offset { MIN_OFFSET }
  location { latLng.latitude to latLng.longitude }
  radius { RADIUS_IN_METERS }
  language { locale.language }
  types { AutocompleteType.CITIES } //Check the AutocompleteType class to get all the possible types
  components { listOf(DEFAULT_COUNTRY) }
  strictBounds { true }
}

Listening events

You can use the stateStream() to handle the provided events from the API:

rxAutocomplete.stateStream()
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe {
      when(it) {
        AutocompleteState.QUERYING -> view.showProgress()
        AutocompleteState.FAILURE -> view.showError()
        AutocompleteState.SUCCESS -> view.hideProgress()
      }
    }

API Defaults

  • The default min key stroke is 3
  • The default query interval is 2 seconds

Reverse Geocoding

To get the latitude and longitude from a given place, you can use the GeocodeRepository

Initialization

You should use the function create(GOOGLE_MAPS_API_GEOCODE_KEY), where GOOGLE_MAPS_API_GEOCODE_KEY is the generated key from the Google developer console:

val geocodeRepository = GeocodeRepository.create(BuildConfig.GOOGLE_MAPS_API_GEOCODE_KEY)

Usage

You can use the method reverseGeocode(placeId):

geocodeRepository.reverseGeocode(placeId)
    .subscribeOn(Schedulers.io())
    .take(1)
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(
        { view.dispatchGeocodeResult(it)},
        { view.showError() }
    )

Accessing the provided results

You can easily get the result information through the API given functions, example:

private fun createResult(result: GeocodeResult) = Result(
  latitude = result.geometry.location.lat,
  longitude = result.geometry.location.lng,
  formattedAddress = result.formattedAddress,
  street = result.getStreetName(),
  number = result.getStreetNumber(),
  city = result.getCity(),
  neighborhood = result.getNeighborhood(),
  postalCode = result.getPostalCode(),
)

Note: the API will return the long name attribute from the response.

Demo

Look the demo folder

Tests

./gradlew test

Contributing

Don't be shy, send a Pull Request! Here is how:

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -m 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

Copyright (C) 2017 99

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

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.