Skip to content

Commit

Permalink
Merge branch 'v1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
jonataswalker committed Nov 18, 2015
2 parents ee31f59 + 9d85e8b commit db39dde
Show file tree
Hide file tree
Showing 8 changed files with 945 additions and 867 deletions.
72 changes: 72 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
ROOT_DIR := $(shell dirname $(realpath $(lastword $(MAKEFILE_LIST))))
SRC_DIR := $(ROOT_DIR)/src
BUILD_DIR := $(ROOT_DIR)/build
JS_DEBUG := $(BUILD_DIR)/ol3-geocoder-debug.js
JS_FINAL := $(BUILD_DIR)/ol3-geocoder.js
CSS_COMBINED := $(BUILD_DIR)/ol3-geocoder.css
CSS_FINAL := $(BUILD_DIR)/ol3-geocoder.min.css

JS_FILES = $(SRC_DIR)/wrapper-head.js \
$(SRC_DIR)/base.js \
$(SRC_DIR)/nominatim.js \
$(SRC_DIR)/utils.js \
$(SRC_DIR)/wrapper-tail.js

CSS_FILES = $(SRC_DIR)/ol3-geocoder.css

CLEANCSS = /usr/local/bin/cleancss
CLEANCSSFLAGS = --skip-restructuring
POSTCSS = /usr/bin/postcss
POSTCSSFLAGS = --use autoprefixer -b "last 2 versions"
JSHINT = /usr/bin/jshint
UGLIFYJS = /usr/bin/uglifyjs
UGLIFYJSFLAGS = --mangle --mangle-regex --screw-ie8 -c warnings=false
JS_BEAUTIFY = /usr/bin/js-beautify
BEAUTIFYFLAGS = -f - --indent-size 2 --preserve-newlines
NODEMON = /usr/bin/nodemon
PARALLELSHELL = /usr/bin/parallelshell

# just to create variables like NODEMON_JS_FLAGS when called
define NodemonFlags
UP_LANG = $(shell echo $(1) | tr '[:lower:]' '[:upper:]')
NODEMON_$$(UP_LANG)_FLAGS := --on-change-only --watch "$(SRC_DIR)" --ext "$(1)" --exec "make build-$(1)"
endef

# targets
build-watch: build watch

watch:
$(PARALLELSHELL) "make watch-js" "make watch-css"

build: build-js build-css

build-js: combine-js jshint uglifyjs
@echo "Build JS ... OK"

build-css: combine-css cleancss
@echo "Build CSS ... OK"

uglifyjs:
@$(UGLIFYJS) $(JS_DEBUG) $(UGLIFYJSFLAGS) > $(JS_FINAL)

jshint:
@$(JSHINT) $(JS_DEBUG)

cleancss:
@$(POSTCSS) $(POSTCSSFLAGS) $(CSS_COMBINED) | $(CLEANCSS) $(CLEANCSSFLAGS) > $(CSS_FINAL)

combine-js:
@cat $(JS_FILES) | $(JS_BEAUTIFY) $(BEAUTIFYFLAGS) > $(JS_DEBUG)

combine-css:
@cat $(CSS_FILES) > $(CSS_COMBINED)

watch-js: $(JS_FILES)
$(eval $(call NodemonFlags,js))
@$(NODEMON) $(NODEMON_JS_FLAGS)

watch-css: $(CSS_FILES)
$(eval $(call NodemonFlags,css))
@$(NODEMON) $(NODEMON_CSS_FLAGS)

.DEFAULT_GOAL := build
57 changes: 40 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,29 +27,52 @@ Download [latest release](https://github.com/jonataswalker/ol3-geocoder/releases
##### Instantiate with some options and add the Control
```javascript
var geocoder = new Geocoder('nominatim', {
provider: 'mapquest',
key: '__some_key__',
lang: 'pt-BR', //en-US, fr-FR
placeholder: 'Search for ...',
limit: 5,
keepOpen: true
provider: 'mapquest',
key: '__some_key__',
lang: 'pt-BR', //en-US, fr-FR
placeholder: 'Search for ...',
limit: 5,
keepOpen: true
});
map.addControl(geocoder);
```

##### Listen and do something when an address is chosen
```javascript
geocoder.on('change:geocoder', function(evt){
var
feature_id = evt.target.get('geocoder'),
feature = geocoder.getSource().getFeatureById(feature_id),
address = feature.get('address'),
coord = feature.getGeometry().getCoordinates()
;
content.innerHTML = '<p>'+address+'</p>';
overlay.setPosition(coord);
geocoder.on('addresschosen', function(evt){
var
feature = evt.feature,
coord = evt.coordinate,
address_html = feature.get('address_html')
;
content.innerHTML = '<p>'+address_html+'</p>';
overlay.setPosition(coord);
});
```

## Where are the docs?
Work in progress.
# API

## Constructor

#### `new Geocoder(control_type, options)`

###### `control_type` `{String}`
Maybe later we will have other types like `'reverse'`. So for now just pass `'nominatim'`.

###### `options` is an object with the following possible properties:
* `provider` : `'osm'` (default), `'mapquest'`, `'google'`, `'photon'`; Your preferable provider;
* `key` : ''; API Key if required;
* `placeholder` : 'Search for an address'; Placeholder for text input;
* `featureStyle`: `ol.style.Style`; Feature style;
* `lang` : `'en-US'`; Preferable language;
* `limit` : `5`; Limit of results;
* `keepOpen` : `false`; Whether the results keep openned;
* `debug` : `false`; If true logs provider's response;

## Methods

#### `geocoder.getLayer()`
Returns the layer `{ol.layer.Vector}` created by Geocoder control.

#### `geocoder.getSource()`
Returns the source `{ol.source.Vector}` created by Geocoder control.
30 changes: 1 addition & 29 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,44 +1,16 @@
{
"name": "ol3-geocoder",
"version": "1.2.0",
"version": "1.4.0",
"description": "A geocoder extension for OpenLayers 3.",
"main": "ol3-geocoder.js",
"author": "Jonatas Walker",
"license": "MIT",
"config": {
"js_debug": "ol3-geocoder-debug.js",
"js_minified": "ol3-geocoder.js",
"css_combined": "ol3-geocoder.css",
"css_minified": "ol3-geocoder.min.css"
},
"devDependencies": {
"jshint": "latest",
"opener": "latest",
"live-reload": "latest",
"uglify-js": "latest",
"clean-css": "latest",
"postcss": "latest",
"nodemon": "latest",
"parallelshell": "latest"
},
"scripts": {
"prelint": "node combine.js",
"lint": "jshint build/$npm_package_config_js_debug",

"preuglify": "npm run lint",
"uglify": "uglifyjs build/$npm_package_config_js_debug --mangle --mangle-regex --screw-ie8 -c warnings=false -o build/$npm_package_config_js_minified",

"prebuild:css": "postcss --use autoprefixer -b 'last 2 versions' src/$npm_package_config_css_combined -o build/$npm_package_config_css_combined",

"build:css": "cleancss --skip-restructuring -o build/$npm_package_config_css_minified build/$npm_package_config_css_combined",

"watch:css": "nodemon --quiet --on-change-only --watch 'src' --ext 'css' --exec 'npm run build:css'",

"build:js": "npm run uglify",
"watch:js": "nodemon --quiet --on-change-only --watch 'src' --ext 'js' --exec 'npm run build:js'",

"build": "npm run build:js && npm run build:css",

"build:watch": "npm run build && parallelshell 'npm run watch:js' 'npm run watch:css'"
}
}
46 changes: 19 additions & 27 deletions src/base.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,39 @@
/**
* @constructor
* @extends {ol.control.Control}
* @fires change:geocoder
* @param {string|undefined} control_type Nominatim|Reverse.
* @fires {Geocoder.EventType}
* @param {string} control_type Nominatim|Reverse.
* @param {object|undefined} opt_options Options.
*/
var Geocoder = function(control_type, opt_options){
//some checks before continue
utils.assert(
typeof control_type === "string" || typeof control_type === "undefined",
'@param `control_type` should be string|undefined type!'
);
utils.assert(
typeof opt_options === "object" || typeof opt_options === "undefined",
'@param `opt_options` should be object|undefined type!'
);

control_type = control_type || 'nominatim';

var nominatim = new Geocoder.Nominatim(this, opt_options);
this.layer = nominatim.layer;

ol.control.Control.call(this, {
element: nominatim.els.container
});

//set event to be captured with 'change:geocoder'
this.set('geocoder', '');
utils.assert(typeof control_type === 'string', '@param `control_type`' +
' should be string type!'
);
utils.assert(typeof opt_options === 'object' || typeof opt_options === 'undefined',
'@param `opt_options` should be object|undefined type!'
);

control_type = control_type || 'nominatim';

var nominatim = new Geocoder.Nominatim(this, opt_options);
this.layer = nominatim.layer;

ol.control.Control.call(this, {
element: nominatim.els.container
});
};
ol.inherits(Geocoder, ol.control.Control);

/**
* @return {ol.source.Vector} Returns the source created by this control
*/
Geocoder.prototype.getSource = function(){
return this.layer.getSource();
return this.layer.getSource();
};


/**
* @return {ol.layer.Vector} Returns the layer created by this control
*/
Geocoder.prototype.getLayer = function(){
return this.layer;
return this.layer;
};

Loading

0 comments on commit db39dde

Please sign in to comment.