Skip to content

Commit

Permalink
Merge pull request #32 from mridulnagpal/add-precision-options
Browse files Browse the repository at this point in the history
reworking GridSystem to be based on latitude precision
  • Loading branch information
mridulnagpal authored Jun 17, 2017
2 parents e2b5c5a + 02b91e2 commit 53c17c1
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 19 deletions.
49 changes: 40 additions & 9 deletions dist/Leaflet.BlurredLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13275,8 +13275,11 @@ BlurredLocation = function BlurredLocation(options) {
options = options || {};
options.map = options.map || L.map('map');

options.addGrid = options.addGrid || require('./core/addGrid.js');
options.addGrid(options.map);
options.gridSystem = options.gridSystem || require('./core/gridSystem.js');

gridSystemOptions = options.gridSystemOptions || {};
gridSystemOptions.map = options.map
options.gridSystem(gridSystemOptions);

L.tileLayer("https://a.tiles.mapbox.com/v3/jywarren.map-lmrwb2em/{z}/{x}/{y}.png").addTo(options.map);

Expand Down Expand Up @@ -13315,7 +13318,7 @@ BlurredLocation = function BlurredLocation(options) {
return options.map.getSize();
}

addGrid = options.addGrid;
gridSystem = options.gridSystem;

function panMapToGeocodedLocation(selector) {
var input = document.getElementById(selector);
Expand All @@ -13340,7 +13343,6 @@ BlurredLocation = function BlurredLocation(options) {
}

lat.addEventListener('change', function() {
console.log("hello");
panIfValue();
});
lng.addEventListener('change', function() {
Expand Down Expand Up @@ -13379,13 +13381,42 @@ BlurredLocation = function BlurredLocation(options) {
}
}

function gridWidthInPixels(degrees) {
var p1 = L.latLng(getLat(),getLon());
var p2 = L.latLng(getLat()+degrees, getLon()+degrees);
var l1 = options.map.latLngToContainerPoint(p1);
var l2 = options.map.latLngToContainerPoint(p2);
return {
x: Math.abs(l2.x - l1.x),
y: Math.abs(l2.y - l1.y),
}
}

function findPrecisionForMinimumGridWidth(width) {
var degrees = 1, precision = 1;
while(gridWidthInPixels(degrees).x > width) {
degrees/= 10;
precision+= 1;
}
return precision;
}

function truncateToPrecision(number, digits) {
var multiplier = Math.pow(10, digits),
adjustedNum = number * multiplier,
truncatedNum = Math[adjustedNum < 0 ? 'ceil' : 'floor'](adjustedNum);

return truncatedNum / multiplier;
}


return {
getLat: getLat,
getLon: getLon,
goTo: goTo,
geocode: geocode,
getSize: getSize,
addGrid: addGrid,
gridSystem: gridSystem,
panMapToGeocodedLocation: panMapToGeocodedLocation,
getPlacenameFromCoordinates: getPlacenameFromCoordinates,
panMapWhenInputsChange: panMapWhenInputsChange,
Expand All @@ -13396,10 +13427,10 @@ BlurredLocation = function BlurredLocation(options) {

exports.BlurredLocation = BlurredLocation;

},{"./core/addGrid.js":5,"leaflet":2}],5:[function(require,module,exports){
module.exports = function addGrid(map, onChangeLocation) {
},{"./core/gridSystem.js":5,"leaflet":2}],5:[function(require,module,exports){
module.exports = function addGrid(options) {

var map = map || document.getElementById("map") || L.map('map');
var map = options.map || document.getElementById("map") || L.map('map');

// A function to return the style of a cell
function create_cell_style(fill) {
Expand All @@ -13422,7 +13453,7 @@ module.exports = function addGrid(map, onChangeLocation) {
include: L.Mixin.Events,

options: {
cellSize: 64,
cellSize: options.cellSize || 40,
delayFactor: 0.5,
},

Expand Down
4 changes: 2 additions & 2 deletions spec/javascripts/test_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ describe("Basic testing", function() {
expect(blurredLocation.getLon()).toBe(-0.09);
});

it("Checks if blurredLocation has a property named addGrid", function() {
expect(blurredLocation.hasOwnProperty("addGrid")).toBe(true);
it("Checks if blurredLocation has a property named gridSystem", function() {
expect(blurredLocation.hasOwnProperty("gridSystem")).toBe(true);
});
// it("geocode spec", function() {
// var geometry = blurredLocation.geocode("Buenos Aires");
Expand Down
40 changes: 35 additions & 5 deletions src/blurredLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@ BlurredLocation = function BlurredLocation(options) {
options = options || {};
options.map = options.map || L.map('map');

options.addGrid = options.addGrid || require('./core/addGrid.js');
options.addGrid(options.map);
options.gridSystem = options.gridSystem || require('./core/gridSystem.js');

gridSystemOptions = options.gridSystemOptions || {};
gridSystemOptions.map = options.map
options.gridSystem(gridSystemOptions);

L.tileLayer("https://a.tiles.mapbox.com/v3/jywarren.map-lmrwb2em/{z}/{x}/{y}.png").addTo(options.map);

Expand Down Expand Up @@ -46,8 +49,6 @@ BlurredLocation = function BlurredLocation(options) {
return options.map.getSize();
}

addGrid = options.addGrid;

function panMapToGeocodedLocation(selector) {
var input = document.getElementById(selector);

Expand Down Expand Up @@ -109,13 +110,42 @@ BlurredLocation = function BlurredLocation(options) {
}
}

function gridWidthInPixels(degrees) {
var p1 = L.latLng(getLat(),getLon());
var p2 = L.latLng(getLat()+degrees, getLon()+degrees);
var l1 = options.map.latLngToContainerPoint(p1);
var l2 = options.map.latLngToContainerPoint(p2);
return {
x: Math.abs(l2.x - l1.x),
y: Math.abs(l2.y - l1.y),
}
}

function findPrecisionForMinimumGridWidth(width) {
var degrees = 1, precision = 1;
while(gridWidthInPixels(degrees).x > width) {
degrees/= 10;
precision+= 1;
}
return precision;
}

function truncateToPrecision(number, digits) {
var multiplier = Math.pow(10, digits),
adjustedNum = number * multiplier,
truncatedNum = Math[adjustedNum < 0 ? 'ceil' : 'floor'](adjustedNum);

return truncatedNum / multiplier;
}


return {
getLat: getLat,
getLon: getLon,
goTo: goTo,
geocode: geocode,
getSize: getSize,
addGrid: addGrid,
gridSystem: options.gridSystem,
panMapToGeocodedLocation: panMapToGeocodedLocation,
getPlacenameFromCoordinates: getPlacenameFromCoordinates,
panMapWhenInputsChange: panMapWhenInputsChange,
Expand Down
6 changes: 3 additions & 3 deletions src/core/addGrid.js → src/core/gridSystem.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = function addGrid(map, onChangeLocation) {
module.exports = function addGrid(options) {

var map = map || document.getElementById("map") || L.map('map');
var map = options.map || document.getElementById("map") || L.map('map');

// A function to return the style of a cell
function create_cell_style(fill) {
Expand All @@ -23,7 +23,7 @@ module.exports = function addGrid(map, onChangeLocation) {
include: L.Mixin.Events,

options: {
cellSize: 64,
cellSize: options.cellSize || 40,
delayFactor: 0.5,
},

Expand Down

0 comments on commit 53c17c1

Please sign in to comment.