Address picker built with typeahead autocomplete using http://photon.komoot.de/
Switch branches/tags
Nothing to show
Clone or download
Latest commit b2507c6 Dec 9, 2016
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
debug Initial commit Dec 6, 2016
dist Initial commit Dec 6, 2016
doc Initial commit Dec 6, 2016
spec Initial commit Dec 6, 2016
src Initial commit Dec 6, 2016
.gitignore Initial commit Dec 6, 2016
.travis.yml Initial commit Dec 6, 2016
LICENSE Initial commit Dec 6, 2016
README.md Add npm badge Dec 9, 2016
bower.json Upgrade version to 1.0.0 after the transfer of repository Dec 9, 2016
gulpfile.js Initial commit Dec 6, 2016
package.json Upgrade version to 1.0.0 after the transfer of repository Dec 9, 2016

README.md

typeahead-address-photon npm

Copy of famous typeahead-addresspicker but using free and Open-Source place search service http://photon.komoot.de/.

Provides PhotonAddressEngine, an implementation of suggestions engine Bloodhound (from corejs-typeahead).

Plugin provides convinient hooks if you wanna make other processing of suggested places, rather than only show them in the drop-down list of typeahead. For example, you can choose to pin markers on Leaflet map in suggested places while typing.

Note

corejs-typeahead is maintained fork of original twitter typeahead. It's strongly recommended to switch on corejs-typeahead if you still use the old one.

Demo

Check out the demo.

How to use

Include jQuery, corejs-typeahead & typeahead-address-photon on your page :

<!-- We assume vendor_scripts to be a folder with third party libraries
     (often node_modules or bower_components) -->
<script src="vendor_scripts/jquery/dist/jquery.js"></script>
<script src="vendor_scripts/corejs-typeahead/dist/typeahead.bundle.js"></script>
<script src="vendor_scripts/typeahead-address-photon/dist/typeahead-address-photon.js"></script>

Add text input for address :

<input id="inpAddress" type="text" placeholder="Enter address here..."></input>

Instanciate PhotonAddressEngine and Typeahead :

var engine = new PhotonAddressEngine();

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

Options

PhotonAddressEngine constructor accepts object with options. The following options can be provided :

  • url - URL of the Photon API to use. Default: 'http://photon.komoot.de'
  • limit - limit number of results. Default: 5
  • formatResult - function to control the way geojson features are displayed in the results box
  • formatType - function to control the way features types (amenity, school, etc.) are displayed in the default formatResult function
  • lat, lon - latitude and longitude to make search with priority to a geo position
  • lang - preferred language

Events

Instance of PhotonAddressEngine can trigger two kind of events that allows you make your own processing of fetched OSM features.

  • addresspicker:selected - triggered when user selects one of suggestions, has OSM feature corresponding to selected place as parameter
  • addresspicker:predictions - triggered when suggestions are fetched for provided request, has all fetched OSM features as parameter

You can subscribe for those events as following :

$(engine).bind('addresspicker:selected', function (event, selectedPlace) {
  // Process selected place here ...
});

$(engine).bind('addresspicker:predictions', function (event, suggestions) {
  // Process all suggestions here ...
});

Reverse geocoding

Reverse geocoding allows to fetch places by geographic coordinates rather than by text query. This feature can be usefull when working with Leaflet map. For example, user drags marker and address input is automatically updated with new place. To do reverse geocoding use method PhotonAddressEngine.reverseGeocode([lat, lon]) that accepts array with coordinates and triggers event addresspicker:selected with first suggested result. Use it as following :

$(engine).bind('addresspicker:selected', function (event, selectedPlace) {
  // Process selected place here ...
});
...
engine.reverseGeocode([ pos.lat, pos.lng ]);