Skip to content

Commit 99ef13b

Browse files
mridulnagpaljywarren
authored andcommitted
Graticule integration (#52)
* Graticule integrated * Graticule integrated * Cellsize spec removed * Test passing * Colored grid * map exposed to public API and Readme section added * tweaks * Library included * Tweaks * tweaks * Set interval changed
1 parent b8c4597 commit 99ef13b

7 files changed

Lines changed: 614 additions & 403 deletions

File tree

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,5 @@ To add new tests, edit the `*_spec.js` files in `/spec/javascripts/`.
5858
|`getFullLat()` | Used to get the full latitude of the center of the map, regardless what the precision is| `blurredLocation.getFullLat()` This would return the full latitude value as a floating numeric|
5959
|`getFullLon()` | Used to get the full longitude of the center of the map, regardless what the precision is| `blurredLocation.getFullLon()` This would return the full longitude value as a floating numeric|
6060
|`getPrecision()` | Used to get the precision of degrees currently occupied by one cell of the grid. This would return an integer which represents the number of decimal places occupied by a cell. For instance, a precision of 1 will mean 0.1 degrees per cell, 2 will mean 0.01 degrees, and so on | `blurredLocation.getPrecision()` This would return the precision of the map at the current zoom level |
61-
| getPlacenameFromCoordinates()| Used to get the human-readable location name in text of specific latitude and longitude. This would take in 3 arguments namely latitude, longitude and a callback function which is called on success and would return address of the location pinpointed by those co-ordinates| `blurredLocation.getPlacenameFromCoordinates(43,43,function(result) {console.log(result);} // This would return the output to the console`|
61+
| `getPlacenameFromCoordinates()`| Used to get the human-readable location name in text of specific latitude and longitude. This would take in 3 arguments namely latitude, longitude and a callback function which is called on success and would return address of the location pinpointed by those co-ordinates| `blurredLocation.getPlacenameFromCoordinates(43,43,function(result) {console.log(result);} // This would return the output to the console`|
62+
|map |Used to access the leaflet map element to perform leaflet commands on it| blurredLocation.map //This would return the leaflet map element|

dist/Leaflet.BlurredLocation.js

Lines changed: 559 additions & 195 deletions
Large diffs are not rendered by default.

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
"jasmine-jquery": "^2.1.1",
3232
"matchdep": "^1.0.1",
3333
"require": "^2.4.20",
34-
"resig-class": "^1.0.0"
34+
"resig-class": "^1.0.0",
35+
"leaflet-graticule": "git://github.com/jywarren/Leaflet.Graticule.git#patch-1"
3536
},
3637
"dependencies": {
3738
"jquery": "^3.2.1",

spec/javascripts/test_spec.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,19 @@ describe("Basic testing", function() {
2929
expect(blurredLocation.hasOwnProperty("gridSystem")).toBe(true);
3030
});
3131

32-
it("Checks if cellSize changes with change in zoom", function() {
33-
34-
blurredLocation.setZoom(13);
35-
36-
expect(blurredLocation.gridSystem.getCellSize().rows).toBe(58.25);
37-
expect(blurredLocation.gridSystem.getCellSize().cols).toBe(94.63);
38-
39-
blurredLocation.setZoom(10);
40-
41-
expect(blurredLocation.gridSystem.getCellSize().rows).toBe(72.8);
42-
expect(blurredLocation.gridSystem.getCellSize().cols).toBe(118.3);
43-
44-
});
32+
// it("Checks if cellSize changes with change in zoom", function() {
33+
//
34+
// blurredLocation.setZoom(13);
35+
//
36+
// expect(blurredLocation.gridSystem.getCellSize().rows).toBe(58.25);
37+
// expect(blurredLocation.gridSystem.getCellSize().cols).toBe(94.63);
38+
//
39+
// blurredLocation.setZoom(10);
40+
//
41+
// expect(blurredLocation.gridSystem.getCellSize().rows).toBe(72.8);
42+
// expect(blurredLocation.gridSystem.getCellSize().cols).toBe(118.3);
43+
//
44+
// });
4545

4646
it("Checks if getPrecision works and changes on zoom", function() {
4747
blurredLocation.goTo(blurredLocation.getLat(), blurredLocation.getLon(),13);

spec/javascripts/ui_spec.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ describe("UI testing", function() {
1818
expect(blurredLocation.getLat()).toBe(20);
1919
expect(blurredLocation.getLon()).toBe(15);
2020

21-
lngEl.val(20);
21+
latEl.val(2);
22+
lngEl.val(23);
2223

2324
latEl.change();
2425

25-
expect(blurredLocation.getLat()).toBe(20);
26-
expect(blurredLocation.getLon()).toBe(20);
26+
expect(blurredLocation.getLat()).toBe(2);
27+
expect(blurredLocation.getLon()).toBe(23);
2728
});
2829
});

src/blurredLocation.js

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,29 @@ BlurredLocation = function BlurredLocation(options) {
33
var L = require('leaflet');
44
var blurredLocation = this;
55
var blurred = true;
6+
require('leaflet-graticule');
67

78
options = options || {};
8-
options.map = options.map || L.map('map');
9+
options.location = options.location || {
10+
lat: 41.011234567,
11+
lon: -85.66123456789
12+
};
13+
14+
options.zoom = options.zoom || 6;
15+
16+
options.map = options.map || new L.Map('map',{zoomControl:false}).setView([options.location.lat, options.location.lon], options.zoom);
17+
918
options.pixels = options.pixels || 400;
1019

1120
options.gridSystem = options.gridSystem || require('./core/gridSystem.js');
21+
1222
options.Interface = options.Interface || require('./ui/Interface.js');
1323

1424
gridSystemOptions = options.gridSystemOptions || {};
1525
gridSystemOptions.map = options.map;
1626
gridSystemOptions.gridWidthInPixels = gridWidthInPixels;
1727
gridSystemOptions.getMinimumGridWidth = getMinimumGridWidth;
28+
1829
gridSystem = options.gridSystem(gridSystemOptions);
1930

2031
InterfaceOptions = options.InterfaceOptions || {};
@@ -26,15 +37,9 @@ BlurredLocation = function BlurredLocation(options) {
2637

2738
Interface = options.Interface(InterfaceOptions);
2839

29-
L.tileLayer("https://a.tiles.mapbox.com/v3/jywarren.map-lmrwb2em/{z}/{x}/{y}.png").addTo(options.map);
30-
31-
options.location = options.location || {
32-
lat: 41.011234567,
33-
lon: -85.66123456789
34-
};
40+
var tileLayer = L.tileLayer("https://a.tiles.mapbox.com/v3/jywarren.map-lmrwb2em/{z}/{x}/{y}.png").addTo(options.map);
3541

36-
options.zoom = options.zoom || 13;
37-
options.map.setView([options.location.lat, options.location.lon], options.zoom);
42+
// options.map.setView([options.location.lat, options.location.lon], options.zoom);
3843

3944
function getLat() {
4045
if(isBlurred())
@@ -153,7 +158,7 @@ BlurredLocation = function BlurredLocation(options) {
153158
}
154159

155160
function getFullLon() {
156-
return options.map.getCenter().lng;
161+
return parseFloat(options.map.getCenter().lng);
157162
}
158163

159164
function setBlurred(boolean) {
@@ -192,6 +197,7 @@ BlurredLocation = function BlurredLocation(options) {
192197
isBlurred: isBlurred,
193198
setBlurred: setBlurred,
194199
truncateToPrecision: truncateToPrecision,
200+
map: options.map,
195201
}
196202
}
197203

src/core/gridSystem.js

Lines changed: 18 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -2,197 +2,35 @@ module.exports = function gridSystem(options) {
22

33
var map = options.map || document.getElementById("map") || L.map('map');
44
options.cellSize = options.cellSize || { rows:100, cols:100 };
5-
// A function to return the style of a cell
6-
function create_cell_style(fill) {
7-
return {
8-
stroke: true,
9-
color: ' #ff0000 ',
10-
dashArray: null,
11-
lineCap: null,
12-
lineJoin: null,
13-
weight: 2,
14-
opacity: 1,
15-
fill: fill,
16-
fillColor: null, //same as color by default
17-
fillOpacity: 0.5,
18-
clickable: true
19-
}
20-
}
21-
22-
L.VirtualGrid = L.FeatureGroup.extend({
23-
include: L.Mixin.Events,
24-
25-
options: {
26-
cellSize: options.cellSize || { rows:100, cols:100 },
27-
delayFactor: 0.5,
28-
},
29-
30-
initialize: function(options) {
31-
L.Util.setOptions(this, options);
32-
L.FeatureGroup.prototype.initialize.call(this, [], options);
33-
},
34-
35-
onAdd: function(map) {
36-
L.FeatureGroup.prototype.onAdd.call(this, map);
37-
this._map = map;
38-
this._cells = [];
39-
this._setupGrid(map.getBounds());
40-
41-
map.on("move", this._moveHandler, this);
42-
map.on("zoomend", this._zoomHandler, this);
43-
map.on("resize", this._resizeHandler, this);
44-
},
45-
46-
onRemove: function(map) {
47-
L.FeatureGroup.prototype.onRemove.call(this, map);
48-
map.off("move", this._moveHandler, this);
49-
map.off("zoomend", this._zoomHandler, this);
50-
map.off("resize", this._resizeHandler, this);
51-
},
52-
53-
_clearLayer: function(e) {
54-
this._cells = [];
55-
},
56-
57-
_moveHandler: function(e) {
58-
this._cells = [];
59-
this._renderCells(e.target.getBounds());
60-
},
61-
62-
_zoomHandler: function(e) {
63-
this.clearLayers();
64-
this._renderCells(e.target.getBounds());
65-
66-
var pixels = options.pixels || 400;
67-
var degrees = options.getMinimumGridWidth(pixels);
68-
setCellSizeInDegrees(degrees.degrees);
69-
},
70-
71-
_renderCells: function(bounds) {
72-
var cells = this._cellsInBounds(bounds);
73-
this.focused_cell_id = cells.length == 0 ? null : cells[0].id;
74-
75-
gridSquareNWCorner = function() {
76-
var lat = cells[0].bounds._northEast.lat;
77-
var lng = cells[0].bounds._southWest.lng;
78-
var NW = { lat: lat, lng: lng }
79-
return NW;
80-
};
81-
82-
this.fire("newcells", cells);
83-
for (var i = cells.length - 1; i >= 0; i--) {
84-
var cell = cells[i];
85-
86-
var should_fill_cell = cell.id == this.focused_cell_id;
875

88-
(function(cell, i) {
89-
var cur_style = create_cell_style(should_fill_cell);
6+
require('leaflet-graticule');
907

91-
setTimeout(this.addLayer.bind(this, L.rectangle(cell.bounds, cur_style)), this.options.delayFactor * i);
8+
options.graticuleOptions = options.graticuleOptions || {
9+
showLabel: true,
10+
zoomInterval: [
11+
{start: 2, end: 2, interval: 40},
12+
{start: 3, end: 3, interval: 20},
13+
{start: 4, end: 4, interval: 10},
14+
{start: 5, end: 7, interval: 5},
15+
{start: 8, end: 20, interval: 1}
16+
],
17+
opacity: 1,
18+
color: '#ff0000',
19+
}
9220

93-
}.bind(this))(cell, i);
9421

95-
this._loadedCells.push(cell.id);
96-
}
97-
},
22+
var layer = L.latlngGraticule(options.graticuleOptions).addTo(map);
9823

99-
_resizeHandler: function(e) {
100-
this._setupSize();
101-
},
102-
103-
_setupSize: function() {
104-
this._rows = Math.ceil(this._map.getSize().x / this._cellSize.rows);
105-
this._cols = Math.ceil(this._map.getSize().y / this._cellSize.cols);
106-
},
107-
108-
_setupGrid: function(bounds) {
109-
this._origin = this._map.project(L.latLng(0,0));
110-
this._cellSize = this.options.cellSize;
111-
this._setupSize();
112-
this._loadedCells = [];
113-
this.clearLayers();
114-
this._renderCells(bounds);
115-
},
116-
117-
_cellPoint: function(row, col) {
118-
var x = this._origin.x + (row * this._cellSize.rows);
119-
var y = this._origin.y + (col * this._cellSize.cols);
120-
return new L.Point(x, y);
121-
},
122-
123-
_cellExtent: function(row, col) {
124-
var swPoint = this._cellPoint(row, col);
125-
var nePoint = this._cellPoint(row - 1, col - 1);
126-
var sw = this._map.unproject(swPoint);
127-
var ne = this._map.unproject(nePoint);
128-
return new L.LatLngBounds(ne, sw);
129-
},
130-
131-
_cellsInBounds: function(bounds) {
132-
var offset = this._map.project(bounds.getNorthWest());
133-
var center = bounds.getCenter();
134-
var offsetX = this._origin.x - offset.x;
135-
var offsetY = this._origin.y - offset.y;
136-
var offsetRows = Math.round(offsetX / this._cellSize.rows);
137-
var offsetCols = Math.round(offsetY / this._cellSize.cols);
138-
var cells = [];
139-
for (var i = 0; i <= this._rows; i++) {
140-
for (var j = 0; j <= this._cols; j++) {
141-
var row = i - offsetRows;
142-
var col = j - offsetCols;
143-
var cellBounds = this._cellExtent(row, col);
144-
var cellId = row + ":" + col;
145-
cells.push({
146-
id: cellId,
147-
bounds: cellBounds,
148-
distance: cellBounds.getCenter().distanceTo(center),
149-
});
150-
}
151-
}
152-
cells.sort(function(a, b) {
153-
return a.distance - b.distance;
154-
});
155-
return cells;
156-
}
157-
});
158-
159-
L.virtualGrid = function(options, url) {
160-
return new L.VirtualGrid(options);
161-
};
162-
163-
var layer = L.virtualGrid({
164-
cellSize: { rows:100, cols:100 }
165-
}).addTo(map);
166-
167-
function setCellSizeInDegrees(degrees) {
168-
169-
layer.remove();
170-
var pixels = options.gridWidthInPixels(1);
171-
var div = 1/degrees;
172-
options.cellSize = { rows:pixels.x/div, cols:pixels.y/div};
173-
layer = L.virtualGrid({
174-
cellSize: options.cellSize
175-
}).addTo(map);
176-
}
177-
178-
function getCellSize() {
179-
return options.cellSize;
24+
function addGrid() {
25+
layer = L.latlngGraticule(options.graticuleOptions).addTo(map);
18026
}
18127

18228
function removeGrid() {
183-
layer.remove();
184-
}
185-
186-
function addGrid() {
187-
layer = L.virtualGrid({
188-
cellSize: options.cellSize
189-
}).addTo(map);
29+
layer.remove();
19030
}
19131

19232
return {
193-
setCellSizeInDegrees: setCellSizeInDegrees,
194-
getCellSize: getCellSize,
19533
removeGrid: removeGrid,
196-
addGrid: addGrid
34+
addGrid: addGrid,
19735
}
19836
}

0 commit comments

Comments
 (0)