Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ L.Control.geocoder(options)
| Option | Type | Default | Description |
| ----------------- | ---------------- | ------------------- | ----------- |
| `collapsed` | Boolean | `true` | Collapse control unless hovered/clicked |
| `expand` | String | `"touch"` | How to expand a collapsed control: `touch` or `click` or `hover` |
| `expand` | String | `"hover"` | How to expand a collapsed control: `click` or `hover` |
| `position` | String | `"topright"` | Control [position](http://leafletjs.com/reference.html#control-positions) |
| `placeholder` | String | `"Search..."` | Placeholder text for text input
| `errorMessage` | String | `"Nothing found."` | Message when no result found / geocoding error occurs |
Expand Down
7 changes: 6 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@
<script type="text/javascript">
var map = L.map('map').setView([0, 0], 2),
geocoder = L.Control.Geocoder.mapzen('search-DopSHJw'),
control = L.Control.geocoder({
hoverControl = L.Control.geocoder({
expand: 'hover',
geocoder: geocoder
}).addTo(map),
clickControl = L.Control.geocoder({
expand: 'click',
geocoder: geocoder
}).addTo(map),
marker;
Expand Down
56 changes: 14 additions & 42 deletions src/control.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default {
options: {
showResultIcons: false,
collapsed: true,
expand: 'touch', // options: touch, click, anythingelse
expand: 'hover', // options: click or anything else (hover or touch)
position: 'topright',
placeholder: 'Search...',
errorMessage: 'Nothing found.',
Expand Down Expand Up @@ -69,54 +69,26 @@ export default {
this
);

// based on https://github.com/leaflet/leaflet/blob/2dbda53/src/control/Control.Layers.js#L187-L211
if (this.options.collapsed) {
this._map.on('click', this._collapse, this);
this._map.on('movestart', this._collapse, this);
if (this.options.expand === 'click') {
L.DomEvent.addListener(
container,
'click',
function(e) {
if (e.button === 0 && e.detail !== 2) {
this._toggle();
}
},
this
);
} else if (L.Browser.touch && this.options.expand === 'touch') {
L.DomEvent.addListener(
container,
'touchstart mousedown',
function(e) {
this._toggle();
e.preventDefault(); // mobile: clicking focuses the icon, so UI expands and immediately collapses
e.stopPropagation();
},
this
);
} else {
L.DomEvent.addListener(container, 'mouseover', this._expand, this);
L.DomEvent.addListener(container, 'mouseout', this._collapse, this);
this._map.on('movestart', this._collapse, this);
L.DomEvent.on(container, 'click', L.DomEvent.stop);
L.DomEvent.on(container, 'click', this._expand, this);
} else if (!L.Browser.android) {
L.DomEvent.addListener(container, 'mouseenter', this._expand, this);
L.DomEvent.addListener(container, 'mouseleave', this._collapse, this);
}
} else {
this._expand();
}
if (this.options.expand !== 'click') {
if (L.Browser.touch) {
L.DomEvent.addListener(
container,
'touchstart',
function() {
this._geocode();
},
this
);
L.DomEvent.on(container, 'click', L.DomEvent.stop);
L.DomEvent.on(container, 'click', this._expand, this);
} else {
L.DomEvent.addListener(
container,
'click',
function() {
this._geocode();
},
this
);
L.DomEvent.on(container, 'focus', this._expand, this);
}
}

Expand Down