Skip to content

Commit ab0ba90

Browse files
committed
Add a "recenter on building" button on map
A public path is added in webpack.config.js because of a problem with font loading. cf. coryhouse/react-slingshot#125
1 parent bc4c971 commit ab0ba90

File tree

3 files changed

+54
-13
lines changed

3 files changed

+54
-13
lines changed

entry.js

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,19 @@ var Session = require('./session.js');
1010
var BuildingService = require('./buildingservice.js');
1111
var LoadingStatus = require('./loadingstatus.js');
1212

13+
require('leaflet-easybutton');
14+
1315
require('leaflet/dist/leaflet.css');
16+
require('leaflet/dist/images/marker-icon.png');
17+
require('leaflet/dist/images/marker-icon-2x.png');
18+
require('leaflet/dist/images/marker-shadow.png');
19+
require('leaflet-easybutton/src/easy-button.css');
20+
require('font-awesome/css/font-awesome.css');
1421
require('./style.css');
1522

1623
// since leaflet is bundled into the browserify package it won't be able to detect where the images
1724
// solution is to point it to where you host the the leaflet images yourself
18-
//L.Icon.Default.imagePath = 'http://cdn.leafletjs.com/leaflet-0.7.3/images';
25+
L.Icon.Default.imagePath = 'http://cdn.leafletjs.com/leaflet-0.7.3/images';
1926

2027
$("body").append("<div id='wrapper'></div>");
2128
var wrapper = $("body").children("#wrapper");
@@ -38,6 +45,7 @@ wrapper.children("#footer").append(
3845

3946
var _username = undefined;
4047
var _map = undefined;
48+
var _recenterButton = undefined;
4149
var _buildingPolygon = undefined;
4250
var _session = new Session();
4351
var _loadingStatus = new LoadingStatus();
@@ -93,8 +101,6 @@ function fetchUserName(callback) {
93101
});
94102
};
95103

96-
_loadingStatus.addListener(updateButtons);
97-
98104
function updateButtons()
99105
{
100106
var loading = _loadingStatus.isLoading;
@@ -106,6 +112,12 @@ function updateButtons()
106112
if (_session.currentIndex <= 0) {
107113
$("#previous-building").prop('disabled', true);
108114
}
115+
116+
if (_session.currentIndex < 0) {
117+
_recenterButton.disable();
118+
} else {
119+
_recenterButton.enable();
120+
}
109121
}
110122

111123
function updateConnectionStatusDisplay() {
@@ -120,9 +132,6 @@ function updateConnectionStatusDisplay() {
120132
}
121133
}
122134

123-
updateConnectionStatusDisplay();
124-
updateButtons();
125-
126135
if (auth.authenticated()) {
127136
$("#connection-status").text("Connected, retrieving username...");
128137
fetchUserName();
@@ -210,8 +219,35 @@ document.getElementById('next-building').onclick = function() {
210219
displayNextBuilding();
211220
};
212221

213-
var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
214-
var osmAttrib = 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
215-
var osm = new L.TileLayer(osmUrl, {minZoom: 0, maxZoom: 18, attribution: osmAttrib});
216-
_map = L.map('map').setView([46.935, 2.780], 7);
217-
_map.addLayer(osm);
222+
function init() {
223+
var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
224+
var osmAttrib = 'Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
225+
var osm = new L.TileLayer(osmUrl, {minZoom: 0, maxZoom: 18, attribution: osmAttrib});
226+
_map = L.map('map').setView([46.935, 2.780], 7);
227+
_map.addLayer(osm);
228+
229+
_recenterButton = L.easyButton(
230+
'fa-crosshairs fa-lg',
231+
function(button, map) {
232+
if (defined(_buildingPolygon)) {
233+
_map.fitBounds(_buildingPolygon.getBounds());
234+
}
235+
},
236+
'', // title
237+
'recenter-button' // id
238+
);
239+
_recenterButton.addTo(_map);
240+
_recenterButton.disable();
241+
242+
// Set the title here and not in the button constructor because when set by
243+
// the constructor, the title is only displayable when the button is active.
244+
// With this alternative way its always displayable.
245+
$("#recenter-button").closest(".leaflet-control").prop("title", "Recenter on building");
246+
247+
_loadingStatus.addListener(updateButtons);
248+
249+
updateConnectionStatusDisplay();
250+
updateButtons();
251+
}
252+
253+
init();

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,10 @@
77
"css-loader": "^0.23.1",
88
"defined": "^1.0.0",
99
"file-loader": "^0.9.0",
10+
"font-awesome": "^4.6.3",
1011
"jquery": "^3.1.0",
1112
"leaflet": "^0.7.7",
13+
"leaflet-easybutton": "^1.3.1",
1214
"osm-auth": "https://github.com/tpetillon/osm-auth#landing-url",
1315
"style-loader": "^0.13.1",
1416
"url-loader": "^0.5.7"

webpack.config.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,15 @@ module.exports = {
22
entry: "./entry.js",
33
output: {
44
path: __dirname,
5+
publicPath: 'http://localhost:8080/',
56
filename: "bundle.js"
67
},
78
module: {
89
loaders: [
9-
{ test: /\.css$/, loader: "style!css" },
10-
{ test: /\.(png|woff|woff2|eot|ttf|svg)$/, loader: 'url-loader?limit=100000' }
10+
{ test: /\.css$/, loader: 'style!css?sourceMap' },
11+
{ test: /\.(png)$/, loader: 'url-loader?limit=100000' },
12+
{ test: /\.(ttf|eot|svg)(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000" },
13+
{ test: /\.woff(2)?(\?v=[0-9]\.[0-9]\.[0-9])?$/, loader: "url-loader?limit=10000&mimetype=application/font-woff" }
1114
]
1215
}
1316
};

0 commit comments

Comments
 (0)