Skip to content

Commit 34c65c2

Browse files
committed
✨ Maps!
1 parent 367d8f6 commit 34c65c2

File tree

5 files changed

+165
-4
lines changed

5 files changed

+165
-4
lines changed

package-lock.json

Lines changed: 23 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@
4747
"classnames": "^2.2.5",
4848
"design-web-toolkit": "github:italia/design-web-toolkit",
4949
"jquery": "^3.2.1",
50+
"leaflet": "^1.2.0",
5051
"react": "^15.6.1",
5152
"react-dom": "^15.6.1",
53+
"react-leaflet": "^1.7.0",
5254
"tablesaw": "^3.0.3"
5355
},
5456
"jest": {

src/components/Map/Map.js

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
import {Map, TileLayer} from 'react-leaflet';
4+
export {
5+
LayersControl,
6+
MapLayer,
7+
GridLayer,
8+
WMSTileLayer,
9+
ImageOverlay,
10+
LayerGroup,
11+
Marker,
12+
Path,
13+
Circle,
14+
CircleMarker,
15+
FeatureGroup,
16+
GeoJSON,
17+
Polygon,
18+
Polyline,
19+
Rectangle,
20+
Popup,
21+
Tooltip,
22+
} from 'react-leaflet';
23+
24+
class OSMap extends React.Component {
25+
state = {
26+
isLoaded: false,
27+
};
28+
componentDidMount() {
29+
this.setState({loaded: true});
30+
}
31+
32+
render() {
33+
// Avoid to load Leaflet on SSR or in NodeJS
34+
if (!this.state.loaded) {
35+
return null;
36+
}
37+
38+
const {children, height, ...rest} = this.props;
39+
40+
return (
41+
<Map {...rest} style={{height: `${height}px`}}>
42+
<TileLayer
43+
attribution="&amp;copy <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors"
44+
url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"
45+
/>
46+
{children}
47+
</Map>
48+
);
49+
}
50+
}
51+
52+
OSMap.propTypes = {
53+
children: PropTypes.node,
54+
height: PropTypes.number,
55+
};
56+
57+
export default OSMap;

src/components/Map/Map.story.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import React from 'react';
2+
import {storiesOf} from '@storybook/react';
3+
import {withInfo} from '@storybook/addon-info';
4+
import {Map, TileLayer, Marker, Popup} from 'react-leaflet';
5+
import L from 'leaflet';
6+
7+
const position = [41.9, 12.49];
8+
const icon = L.divIcon({
9+
html:
10+
'<div style="background: blue; width: 55px; padding-left: 2px;"><img src="https://italia.github.io/design-web-toolkit/theme/docs/logo-it.svg" height="42" width="42"/></div>',
11+
});
12+
13+
const NiceMap = ({attribution, tilesUrl}) => (
14+
<Map center={position} zoom={10} style={{height: 500}}>
15+
<TileLayer attribution={attribution} url={tilesUrl} />
16+
<Marker position={position} icon={icon}>
17+
<Popup>
18+
<span>
19+
I'm here and I'm a component.<br /> With OpenStreet Map
20+
tiles.
21+
</span>
22+
</Popup>
23+
</Marker>
24+
</Map>
25+
);
26+
27+
storiesOf('Map', module)
28+
.add(
29+
'OpenStreet Layer',
30+
withInfo('OpenStreet Layer')(() => (
31+
<NiceMap
32+
tilesUrl={'http://{s}.tile.osm.org/{z}/{x}/{y}.png'}
33+
attribution={
34+
'&copy; <a href=&quot;http://osm.org/copyright&quot;>OpenStreetMap</a> contributors'
35+
}
36+
/>
37+
))
38+
)
39+
.add(
40+
'Stamen.Toner',
41+
withInfo('Stamen.Toner')(() => (
42+
<NiceMap
43+
attribution={
44+
'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
45+
}
46+
tilesUrl={
47+
'https://stamen-tiles-{s}.a.ssl.fastly.net/toner/{z}/{x}/{y}.png'
48+
}
49+
/>
50+
))
51+
)
52+
.add(
53+
'CartoDB Positron',
54+
withInfo('CartoDB Positron')(() => (
55+
<NiceMap
56+
attribution={
57+
'&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a> &copy; <a href="http://cartodb.com/attributions">CartoDB</a>'
58+
}
59+
tilesUrl={
60+
'https://cartodb-basemaps-{s}.global.ssl.fastly.net/light_all/{z}/{x}/{y}.png'
61+
}
62+
/>
63+
))
64+
)
65+
.add(
66+
'Stamen Watercolor',
67+
withInfo('Stamen Watercolor')(() => (
68+
<NiceMap
69+
attribution={
70+
'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
71+
}
72+
tilesUrl={
73+
'https://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.png'
74+
}
75+
/>
76+
))
77+
);

src/index.css

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
1-
@import './styles/build.css';
1+
@import '~leaflet/dist/leaflet.css';
2+
@import './styles/build.css';
3+
4+
.leaflet-container {
5+
width: 100%;
6+
}

0 commit comments

Comments
 (0)