Skip to content

Commit

Permalink
Change JS-params 'X', 'Y' and 'zoom' to numbers
Browse files Browse the repository at this point in the history
The JS-parameters 'X', 'Y' and 'zoom' of the map API
must now be passed as numbers instead of strings.
Therefore the permalink class has been adapted so that the
'getHashObject'- and the 'getParams'-function return numbers
for the parameters 'X', 'Y' and 'zoom'.
  • Loading branch information
chrismayer committed Sep 15, 2015
1 parent b02cbec commit 02eb697
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ Shareloc offers an easy-to-use JavaScript API to create your own OpenLayers 3 ma
var api = new Shareloc.MapApi();
var params = {
// lon of map center
X: "8.318049976895958",
X: 8.318049976895958,
// lat of map center
Y: "49.43451657605041",
Y: 49.43451657605041,
// initial map zoom level
zoom: "14",
zoom: 14,
// layer
bgLayer: "osm.base"
};
Expand Down
10 changes: 5 additions & 5 deletions src/map-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ Shareloc.MapApi = function() {
* Configuration must be in the format:
*
* {
* X: "8.318049976895958",
* Y: "49.43451657605041",
* zoom: "14",
* X: 8.318049976895958,
* Y: 49.43451657605041,
* zoom: 14,
* bgLayer: "opentopomap",
* marker: "49.43707328904662,8.306307792663572",
* popupText: "foo-popup-text"
Expand All @@ -46,8 +46,8 @@ Shareloc.MapApi.prototype.map = function(config) {
],
view: new ol.View({
// map will be recentered by extent on postrender
center: ol.proj.transform([parseFloat(params.X || 0), parseFloat(params.Y || 0)], 'EPSG:4326', 'EPSG:3857'),
zoom: parseInt(params.zoom || 2, 10)
center: ol.proj.transform([(params.X || 0), (params.Y || 0)], 'EPSG:4326', 'EPSG:3857'),
zoom: (params.zoom || 2)
}),
controls: ol.control.defaults({
attributionOptions: ({
Expand Down
32 changes: 23 additions & 9 deletions src/permalink.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,14 @@ Shareloc.Permalink = function(loc) {
* Returns a object representation of the hash string, e. g.:
*
* {
* zoom: "2",
* X: "50.02345",
* Y: "8.161234",
* zoom: 2,
* X: 50.02345,
* Y: 8.161234,
* bgLayer: "osm.base"
* }
*
* The parameters 'X', 'Y' and 'zoom' are transformed to numbers.
*
* @return {Object} object holding the key-value-pairs of the hash
* @memberof Shareloc.Permalink
* @instance
Expand All @@ -57,9 +59,9 @@ Shareloc.Permalink = function(loc) {
var fh = this.getFullHash();
var fhParts = fh.split(/(.+)=(.+)\/(.+)\/(.+)&(.+)=(.+)/i);
return {
zoom: fhParts[2],
X: fhParts[4],
Y: fhParts[3],
zoom: parseInt(fhParts[2], 10),
X: parseFloat(fhParts[4]),
Y: parseFloat(fhParts[3]),
bgLayer: fhParts[6]
};
};
Expand All @@ -68,14 +70,17 @@ Shareloc.Permalink = function(loc) {
* Returns the URL parameters of the underlying location, e.g.:
*
* {
* X: "8.318049976895958",
* Y: "49.43451657605041",
* zoom: "14",
* X: 8.318049976895958,
* Y: 49.43451657605041,
* zoom: 14,
* bgLayer: "opentopomap",
* marker: "49.43707328904662,8.306307792663572",
* popupText: "foo-popup-text"
* }
*
* The parameters 'X', 'Y' and 'zoom' are transformed to numbers, all
* others remain strings.
*
* @return {Object} object holding the key-value-pairs of the URL params
* @memberof Shareloc.Permalink
* @instance
Expand All @@ -96,6 +101,15 @@ Shareloc.Permalink = function(loc) {
/* jshint ignore:end */
params = urlParams;
}
if(params.X) {
params.X = parseFloat(params.X);
}
if(params.Y) {
params.Y = parseFloat(params.Y);
}
if(params.zoom) {
params.zoom = parseInt(params.zoom, 10);
}
return params;
};
};
Expand Down

0 comments on commit 02eb697

Please sign in to comment.