Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

typeahead-addresspicker

sgruhier6061.0.1

Example of adding google map API to typeahead jquery plugin to display address autocomplete suggestions.

jquery, plugin, typeahead, addresspicker, google, map

readme

Typeahead Address Picker Build Status

It's not an extension of typeahead plugin itself, but a new data source for twitter typeahead (version > 0.10.0)

The AddressPicker is a subclass of a Bloodhound class. It connects suggestions to Google Map AutocompleteService.

But it's much more than a simple suggestion engine because it can be linked to a google map to display/edit location.

How to use

Without a Google Map

The simplest usage is to use it as suggestion engine, without displaying results on google map.

1) Include typeahead and google map with places activated

<script src="http://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false&libraries=places"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="../dist/typeahead.js"></script>
<script src="../dist/typeahead-addresspicker.js"></script>

2) Add an input text

<input id="address" type="text" placeholder="Enter an address">

3) Instanciate an AddressPicker and a typeahead

var addressPicker = new AddressPicker();

$('#address').typeahead(null, {
  displayKey: 'description',
  source: addressPicker.ttAdapter()
});

With a Google Map

For a better user experience, you can connect it to a google map to display results. You just need to add a div as for a google map place holder and connect it to the AddressPicker

1) As before

2) Add a div

<input id="address" type="text" placeholder="Enter an address">
<div id="map"></div>

3) Instantiate an AddressPicker with the google map div element or ID and connect typeahead events.

// instantiate the addressPicker suggestion engine (based on bloodhound)
var addressPicker = new AddressPicker({
 map: {
  id: '#map'
 }
});

// instantiate the typeahead UI
$('#address').typeahead(null, {
  displayKey: 'description',
  source: addressPicker.ttAdapter()
});

// Bind some event to update map on autocomplete selection
$('#address').bind('typeahead:selected', addressPicker.updateMap);
$('#address').bind('typeahead:cursorchanged', addressPicker.updateMap);

Options

When you instantiate a new AddressPicker you can pass a list of options new AddressPicker(options)

Available Options:

  • map (Hash): Map id and options to link typeahead to a goggle map (default: none).

    • id (String/Element) DOM map element or CSS selector
    • all google.maps.Map constructor options. Default values are:
      {
      zoom: 3,
      center: new google.maps.LatLng(0, 0),
      mapTypeId: google.maps.MapTypeId.ROADMAP
      }
  • marker (Hash): Marker options display on the map.

    • All google.maps.Marker constructor options. Default values are:
      {
      draggable: true,
      visible: false,
      position: MAP_CENTER
      }
  • autocompleteService (Hash) : options passed to google.maps.places.AutocompleteService#getPlacePredictions (default: {types: ['geocode']}) For more details read Google documentation. You can add a lot of options, like get only address for a country, or get only cities.

    Example To get only cities in United States:

    {
      autocompleteService: {
        types: ['(cities)'], 
        componentRestrictions: { country: 'US' }
      }
    }
  • zoomForLocation (Number): Zoom value when an accurate address is selected (default: 16).

  • reverseGeocoding (Boolean): Reverse geocoding when marker is dragged on map (default: false).
  • placeDetails (Boolean): If not using with a map, you can skip the getDetails portion to speed up the query (default: false).

Events

Only one event is trigger by AddressPicker object: addresspicker:selected. The event is fired when an address is selected or when the marker is dragged. If reverseGeocoding is activated, a full response is trigger, otherwise only lat/lng.

To simplify google response parsing, we fire an object of type AddressPickerResult with some accessors:

  • lat()
  • lng()
  • addressTypes()
  • addressComponents()
  • nameForType: (type, shortName = false)

Listen that event to get values you need and store them in your form. Here is an example:

// Proxy inputs typeahead events to addressPicker
addressPicker.bindDefaultTypeaheadEvent($('#address'))

// Listen for selected places result
$(addressPicker).on('addresspicker:selected', function (event, result) {
  $('#your_lat_input').val(result.lat());
  $('#your_lng_input').val(result.lng());
  $('#your_city_input').val(result.nameForType('locality'));
});

Tests

The code is tested as much as possible. If you want to add features, please add spec in your pull request.

Demo

A demo is included in the github repository, and is available here: http://sgruhier.github.io/typeahead-addresspicker

Quick example to show how to use twitter typeahead autocomplete and google map API to make an address picker.

This example is just a first try and could be enhanced to fully replace my previous address picker: http://xilinus.com/jquery-addresspicker/demos/

Any suggestions (using issues) or pull requests are welcome.

Todo

  • Connect HTML5 geolocalisation API to display user position
  • Anything else that could make sense to be added :). You can open an issue with a label "feature" to open a discussion on a feature/API you'd like to add.

Credits

@copyright Sébastien Gruhier / Xilinus (http://xilinus.com - http://v3.maptimize.com - http://buy.maptmize.com)

changelog

Changelog

For transparency and insight into our release cycle, releases will be numbered with the follow format:

<major>.<minor>.<patch>

And constructed with the following guidelines:

  • Breaking backwards compatibility bumps the major
  • New additions without breaking backwards compatibility bumps the minor
  • Bug fixes and misc changes bump the patch

For more information on semantic versioning, please visit http://semver.org/.


0.10.3 July 10, 2014

Hotfix

  • Fix regression that breaks Bloodhound instances when more than 1 instance is relying on remote data. #899

0.10.3 July 10, 2014

Bug fixes

  • Bloodhound#clearPrefecthCache now works with cache keys that contain regex characters. #771
  • Prevent outdated network requests from being sent. #809
  • Add support to object tokenizers for multiple property tokenization. #811
  • Fix broken jQuery#typeahead('val') method. #815
  • Remove disabled attribute from the hint input control. #839
  • Add tt-highlight class to highlighted text. #833
  • Handle non-string types that are passed to jQuery#typeahead('val', val). #881

0.10.2 March 10, 2014

  • Prevent flickering of dropdown menu when requesting remote suggestions. #718
  • Reduce hint flickering. #754
  • Added Bloodhound#{clear, clearPrefetchCache, clearRemoteCache} and made it possible to reinitialize Bloodhound instances. #703
  • Invoke local function during initialization. #687
  • In addition to HTML strings, templates can now return DOM nodes. #742
  • Prevent jQuery#typeahead('val', val) from opening dropdown menus of non-active typeaheads. #646
  • Fix bug in IE that resulted in dropdown menus with overflow being closed when clicking on the scrollbar. #705
  • Only show dropdown menu if minLength is satisfied. #710

0.10.1 February 9, 2014

Hotfix

  • Fixed bug that prevented some ajax configs from being respected. #630
  • Event delegation on suggestion clicks is no longer broken. #118
  • Ensure dataset names are valid class name suffixes. #610
  • Added support for displayKey to be a function. #633
  • jQuery#typeahead('val') now mirrors jQuery#val(). #659
  • Datasets can now be passed to jQuery plugin as an array. #664
  • Added a noConflict method to the jQuery plugin. #612
  • Bloodhound's local property can now be a function. #485

0.10.0 February 2, 2014

Introducting Bloodhound

This release was almost a complete rewrite of typeahead.js and will hopefully lay the foundation for the 1.0.0 release. It's impossible to enumerate all of the issues that were fixed. If you want to get an idea of what issues 0.10.0 resolved, take a look at the closed issues in the 0.10.0 milestone.

The most important change in 0.10.0 is that typeahead.js was broken up into 2 individual components: Bloodhound and jQuery#typeahead. Bloodhound is an feature-rich suggestion engine. jQuery#typeahead is a jQuery plugin that turns input controls into typeaheads.

It's impossible to write a typeahead library that supports every use-case out of the box – that was the main motivation behind decomposing typeahead.js. Previously, some prospective typeahead.js users were unable to use the library because either the suggestion engine or the typeahead UI did not meet their requirements. In those cases, they were either forced to fork typeahead.js and make the necessary modifications or they had to give up on using typeahead.js entirely. Now they have the option of swapping out the component that doesn't work for them with a custom implementation.

0.9.3 June 24, 2013

  • Ensure cursor visibility in menus with overflow. #209
  • Fixed bug that led to the menu staying open when it should have been closed. #260
  • Private browsing in Safari no longer breaks prefetch. #270
  • Pressing tab while a suggestion is highlighted now results in a selection. #266
  • Dataset name is now passed as an argument for typeahead:selected event. #207

0.9.2 April 14, 2013

  • Prefetch usage no longer breaks when cookies are disabled. #190
  • Precompiled templates are now wrapped in the appropriate DOM element. #172

0.9.1 April 1, 2013

  • Multiple requests no longer get sent for a query when datasets share a remote source. #152
  • Datasets now support precompiled templates. #137
  • Cached remote suggestions now get rendered immediately. #156
  • Added typeahead:autocompleted event. #132
  • Added a plugin method for programmatically setting the query. Experimental. #159
  • Added minLength option for datasets. Experimental. #131
  • Prefetch objects now support thumbprint option. Experimental. #157

0.9.0 March 24, 2013

Custom events, no more typeahead.css, and an improved API

  • Implemented the triggering of custom events. #106
  • Got rid of typeahead.css and now apply styling through JavaScript. #15
  • Made the API more flexible and addressed a handful of remote issues by rewriting the transport component. #25
  • Added support for dataset headers and footers. #81
  • No longer cache unnamed datasets. #116
  • Made the key name of the value property configurable. #115
  • Input values set before initialization of typeaheads are now respected. #109
  • Fixed an input value/hint casing bug. #108

0.8.2 March 04, 2013

  • Fixed bug causing error to be thrown when initializing a typeahead on multiple elements. #51
  • Tokens with falsy values are now filtered out – was causing wonky behavior. #75
  • No longer making remote requests for blank queries. #74
  • Datums with regex characters in their value no longer cause errors. #77
  • Now compatible with the Closure Compiler. #48
  • Reference to jQuery is now obtained through window.jQuery, not window.$. #47
  • Added a plugin method for destroying typeaheads. Won't be documented until v0.9 and might change before then. #59

0.8.1 February 25, 2013

  • Fixed bug preventing local and prefetch from being used together. #39
  • No longer prevent default browser behavior when up or down arrow is pressed with a modifier. #6
  • Hint is hidden when user entered query is wider than the input. #26
  • Data stored in localStorage now expires properly. #34
  • Normalized search tokens and fixed query tokenization. #38
  • Remote suggestions now are appended, not prepended to suggestions list. #40
  • Fixed some typos through the codebase. #3

0.8.0 February 19, 2013

Initial public release

  • Prefetch and search data locally insanely fast.
  • Search hard-coded, prefetched, and/or remote data.
  • Hinting.
  • RTL/IME/international support.
  • Search multiple datasets.
  • Share datasets (and caching) between multiple inputs.
  • And much, much more...