Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
brendan-ward committed Dec 24, 2014
0 parents commit 32c2296
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
node_modules/*
.idea/*
16 changes: 16 additions & 0 deletions Jakefile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
var uglifyjs = require("uglify-js");
var compressor = require('node-minify');

task('default', [], function(){
jake.logger.log('Compressing JavaScript files...');
new compressor.minify({
type: 'uglifyjs',
fileIn: ['L.Control.Geonames.js'],
fileOut: 'L.Control.Geonames.min.js',
callback: function(error){
if(error){
jake.logger.console.log(error);
}
}
});
});
56 changes: 56 additions & 0 deletions L.Control.Geonames.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
.leaflet-geonames-search {
background-color: #FFF;
}
.leaflet-geonames-search a{
font-size: 18px;
border-radius: 4px;
display: inline-block;
float:right;
}
.leaflet-geonames-search a {
background: none !important;
border: none !important;
}
.leaflet-geonames-search form {
float: left;
}
.leaflet-geonames-search input {
padding: 2px;
margin: 4px;
border: none;
display: none;
}
.leaflet-geonames-search ul {
display: none;
list-style: none;
padding: 0 0 2px 0;
margin: 0;
clear:both;
max-width: 250px;
}
.leaflet-geonames-search ul li {
padding: 4px;
border-top: 1px solid #DDD;
}
.leaflet-geonames-search ul.hasResults li:hover {
background: #F2F2F2;
cursor: pointer;
}
.leaflet-geonames-search input:focus {
outline: 0;
}
.leaflet-geonames-search.active a {
border-radius: 0 4px 4px 0;
border-bottom-color: #FFF;
}
.leaflet-geonames-search.active input {
display: inline-block;
}
.leaflet-geonames-search ul.hasResults,
.leaflet-geonames-search ul.noResults{
display: block;
}
.leaflet-geonames-search ul.noResults {
color: #999;
font-style: italic;
}
147 changes: 147 additions & 0 deletions L.Control.Geonames.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
L.Control.Geonames = L.Control.extend({
_active: false,
_resultsList: null,
_marker: null,
_hasResults: false,
options: {
username: '', //Geonames account username. Must be provided
maxresults: 5, //Maximum number of results to display per search
zoomLevel: null, //Max zoom level to zoom to for location. If null, will use the map's max zoom level.
className: 'fa fa-crosshairs', //class for icon
workingClass: 'fa-spin', //class for search underway
featureClasses: ['A', 'H', 'L', 'P', 'R', 'T', 'U', 'V'], //feature classes to search against. See: http://www.geonames.org/export/codes.html
baseQuery: 'isNameRequired=true', //The core query sent to GeoNames, later combined with other parameters above
position: 'topleft',
},
onAdd: function() {
this._container = L.DomUtil.create('div', 'leaflet-geonames-search leaflet-bar');
this._container.title = 'Search by location name';
var link = this._link = L.DomUtil.create('a', this.options.className, this._container);
link.href = '#';

var form = L.DomUtil.create('form', '', this._container);
L.DomEvent.addListener(form, 'submit', this._search, this);

var input = this._input = L.DomUtil.create('input', '', form);
input.type = 'text';
input.placeholder = 'Enter a location name';

this._resultsList = L.DomUtil.create('ul', '', this._container);

L.DomEvent
.on(this._container, 'dblclick', L.DomEvent.stop)
.on(this._container, 'click', L.DomEvent.stop)
.on(link, 'click', function(){
this._active = !this._active;
if (this._active){
L.DomUtil.addClass(this._container, 'active');
input.focus();
if (this._hasResults){
L.DomUtil.addClass(this._resultsList, 'hasResults');
}
}
else {
this._close();
}
}, this);

return this._container;
},
_close: function(){
L.DomUtil.removeClass(this._container, 'active');
L.DomUtil.removeClass(this._resultsList, 'hasResults');
L.DomUtil.removeClass(this._resultsList, 'noResults');
this._active = false;
if (this._marker != null){
this._map.removeLayer(this._marker);
this._marker = null;
}
},
_search: function(event){
L.DomEvent.preventDefault(event);

L.DomUtil.addClass(this._link, this.options.workingClass);
L.DomUtil.removeClass(this._resultsList, 'noResults');

//clear results
this._hasResults = false;
this._resultsList.innerHTML = '';

var url = 'http://api.geonames.org/searchJSON?q=' + encodeURIComponent(this._input.value)
+ '&maxRows=' + this.options.maxresults
+ '&username=' + this.options.username
+ '&style=LONG';
if (this.options.featureClasses && this.options.featureClasses.length){
url += '&' + this.options.featureClasses.map(function(fc){return 'featureClass=' + fc}).join('&');
}
if (this.options.baseQuery){
url += '&' + this.options.baseQuery;
}

var origScope = this;
var callbackName = 'geonamesSearchCallback';
this._jsonp(url,
function(response){
document.body.removeChild(document.getElementById('getJsonP'));
delete window[callbackName];
origScope._processResponse(response);
},
callbackName
);
},
_jsonp: function(url, callback, callbackName){
callbackName = callbackName || 'jsonpCallback';
window[callbackName] = callback;

url += '&callback=' + callbackName;
var script = document.createElement('script');
script.id = 'getJsonP';
script.src = url;
script.async = true;
document.body.appendChild(script);
},
_processResponse: function(response){
L.DomUtil.removeClass(this._link, this.options.workingClass);

if (response.geonames.length > 0){
L.DomUtil.addClass(this._resultsList, 'hasResults');
this._hasResults = true;
var li;
response.geonames.forEach(function(geoname){
li = L.DomUtil.create('li', '', this._resultsList);
li.innerHTML = this._getName(geoname);
L.DomEvent.addListener(li, 'click', function(){
var lat = parseFloat(geoname.lat);
var lon = parseFloat(geoname.lng);
if (this._marker != null){
this._map.removeLayer(this._marker);
this._marker = null;
}
this._marker = L.marker([lat, lon]).addTo(this._map).bindPopup(this._getName(geoname));
this._map.setView([lat, lon], this.options.zoomLevel || this._map.getMaxZoom(), false);
this._marker.openPopup();
}, this);
}, this);
}
else {
L.DomUtil.addClass(this._resultsList, 'noResults');
li = L.DomUtil.create('li', '', this._resultsList);
li.innerText = 'No results found';
}
},
_getName: function(geoname){
var name = geoname.name;
var extraName;
['adminName1', 'countryName'].forEach(function(d){
extraName = geoname[d];
if (extraName && extraName != '' && extraName != geoname.name){
name += ', ' + extraName;
}
}, this);
return name;
}
});

L.control.geonames = function (options) {
return new L.Control.Geonames(options);
};
1 change: 1 addition & 0 deletions L.Control.Geonames.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
Copyright (c) 2014, Conservation Biology Institute

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
61 changes: 61 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Leaflet.Geonames

A [GeoNames](http://www.geonames.org/) powered geocoding search control for Leaflet.

It allows you to enter a placename, display a list of search results using GeoNames, and select a placename to zoom to.

Location markers remain on the map until the control is closed (click on icon to open / close).

See the [example](//consbio.github.io/Leaflet.Geonames).

*Tested with Leaflet 0.7.x*




## Usage

Include the CSS:

```
<link rel="stylesheet" href="L.Control.Geonames.css" />
```

This control uses [Font Awesome](http://fortawesome.github.io/Font-Awesome/) for the icon by default. To use, include:

```
<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" />
```


Include the JavaScript:

```
<script src="L.Control.Geonames.min.js"></script>
```


Example usage:

```
var control = L.control.geonames({
username: '', // Geonames account username. Must be provided
zoomLevel: null, // Max zoom level to zoom to for location. If null, will use the map's max zoom level.
maxresults: 5, // Maximum number of results to display per search
className: 'fa fa-crosshairs', // class for icon
workingClass: 'fa-spin', // class for search underway
featureClasses: ['A', 'H', 'L', 'P', 'R', 'T', 'U', 'V'], // feature classes to search against. See: http://www.geonames.org/export/codes.html
baseQuery: 'isNameRequired=true', // The core query sent to GeoNames, later combined with other parameters above
position: 'topleft'
});
map.addControl(control);
```




## Credits:
Developed with support from the [South Atlantic Landscape Conservation Cooperative](http://www.southatlanticlcc.org/)

Some ideas derived from [L.GeoSearch](https://github.com/smeijer/L.GeoSearch).

20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "Leaflet.Geonames",
"version": "0.0.1",
"description": "Geonames Geocoding Search Control for Leaflet",
"homepage": "http://github.com/consbio/Leaflet.Geonames",
"author": {
"name": "Brendan Ward",
"email": "bcward@consbio.org"
},
"devDependencies": {
"uglify-js": "~2.4.16",
"jake": "~8.0.10",
"node-minify": "~1.0.1"
},
"repository": {
"type": "git",
"url": "git://github.com/consbio/Leaflet.Geonames"
},
"keywords": ["leaflet"]
}

0 comments on commit 32c2296

Please sign in to comment.