Skip to content
This repository has been archived by the owner on Feb 14, 2023. It is now read-only.

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexei KLENIN committed Dec 6, 2016
0 parents commit bc87518
Show file tree
Hide file tree
Showing 13 changed files with 1,061 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
node_modules
7 changes: 7 additions & 0 deletions .travis.yml
@@ -0,0 +1,7 @@
language: node_js
node_js:
- "6.3"
install:
- npm install -g gulp bower
- npm install
script: gulp
21 changes: 21 additions & 0 deletions LICENSE
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2016 Alexei KLENIN

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
76 changes: 76 additions & 0 deletions README.md
@@ -0,0 +1,76 @@
# typeahead-address-photon

Copy of famous [typeahead-addresspicker](https://github.com/sgruhier/typeahead-addresspicker)
but using free and Open-Source place search service http://photon.komoot.de/.

Provides `PhotonAddressEngine`, an implementation of suggestions engine [Bloodhound](https://github.com/twitter/typeahead.js/blob/master/doc/bloodhound.md) (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](http://leafletjs.com/) map in suggested places while typing.

<img src="https://raw.github.com/komoot/typeahead-address-photon/master/doc/screenshot.png"/>

### Note
[corejs-typeahead](https://github.com/corejavascript/typeahead.js) is maintained fork of original [twitter typeahead](http://twitter.github.io/typeahead.js/). It's strongly recommended to switch on corejs-typeahead if you still use the old one.

## Demo
Check out the [demo](http://komoot.github.io/typeahead-address-photon/).

## How to use
Include jQuery, corejs-typeahead & typeahead-address-photon on your page :
```xml
<!-- 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 :
```xml
<input id="inpAddress" type="text" placeholder="Enter address here..."></input>
```

Instanciate `PhotonAddressEngine` and `Typeahead` :
```javascript
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 :
```javascript
$(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 :

```javascript
$(engine).bind('addresspicker:selected', function (event, selectedPlace) {
// Process selected place here ...
});
...
engine.reverseGeocode([ pos.lat, pos.lng ]);
```
28 changes: 28 additions & 0 deletions bower.json
@@ -0,0 +1,28 @@
{
"name": "typeahead-address-photon",
"version": "0.0.1",
"description": "Address picker built with typeahead autocomplete using http://photon.komoot.de/",
"main": "dist/typeahead-address-photon.js",
"authors": [
"Alexei KLENIN"
],
"license": "MIT",
"keywords": [
"typeahead",
"address",
"photon",
"places",
"picker",
"bloodhound"
],
"homepage": "https://github.com/komoot/typeahead-address-photon",
"ignore": [
"**/.*",
"node_modules",
"bower_components",
"debug",
"spec",
"src",
"gulpfile.js"
]
}
101 changes: 101 additions & 0 deletions debug/index.html
@@ -0,0 +1,101 @@
<!doctype html>
<html>
<head>
<title>Debug of typeahead-address-photon</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#map {
height: 400px
}
</style>
</head>
<body class="container">
<h1>Type address</h1>
<div class="row">
<div class="col-md-6">
<form>
<input id="inpAddress" class="form-control" type="text" placeholder="Enter address here..."></input>
</form>
</div>
<div id="map" class="col-md-6"></div>
</div>
</body>
</html>

<!-- Vendor styles -->
<link rel="stylesheet" href="../node_modules/bootstrap/dist/css/bootstrap.css" />
<link rel="stylesheet" href="../node_modules/typeahead.js-bootstrap-css/typeaheadjs.css" />
<link rel="stylesheet" href="../node_modules/leaflet/dist/leaflet.css" />

<!-- Vendor Javascripts -->
<script src="../node_modules/jquery/dist/jquery.js"></script>
<script src="../node_modules/corejs-typeahead/dist/typeahead.bundle.js"></script>
<script src="../node_modules/bootstrap/dist/js/bootstrap.js"></script>
<script src="../node_modules/leaflet/dist/leaflet-src.js"></script>

<!-- Developed script -->
<script src="../src/typeahead-address-photon.js"></script>

<!-- Test script -->
<script>
var map = L.map('map').setView([48.847547, 2.351074], 14),
markers = L.featureGroup();

L.tileLayer('http://{s}.basemaps.cartocdn.com/light_all/{z}/{x}/{y}.png', {
zoomControl: true,
maxZoom: 18
}).addTo(map);

markers.addTo(map);

function showPredictions(event, predictions) {
map.removeLayer(markers);
markers = L.featureGroup();
markers.addTo(map);

predictions.forEach(function (prediction) {
L.marker([ prediction.geometry.coordinates[1],
prediction.geometry.coordinates[0] ]).addTo(markers);
map.fitBounds(markers.getBounds());
});
}

var engine = new PhotonAddressEngine({
lang: 'fr',
lat: 48.847547,
lon: 2.351074,
onPredictions: showPredictions
});

function showSelected(event, selected) {
$('#inpAddress').val(selected.description);

map.removeLayer(markers);
markers = L.featureGroup();
markers.addTo(map);

L.marker([
selected.geometry.coordinates[1],
selected.geometry.coordinates[0]
], {
draggable: true
}).on('dragend', function (event) {
var pos = event.target.getLatLng();
engine.reverseGeocode([ pos.lat, pos.lng ]);
}).addTo(markers);
map.fitBounds(markers.getBounds());
}

$('#inpAddress').typeahead({
hint: true,
highlight: true,
minLength: 3
}, {
source: engine.ttAdapter(),
displayKey: 'description'
});

engine.bindDefaultTypeaheadEvent($('#inpAddress'));
$(engine).bind('addresspicker:predictions', showPredictions);
$(engine).bind('addresspicker:selected', showSelected);
</script>

0 comments on commit bc87518

Please sign in to comment.