This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathApp.js
104 lines (100 loc) · 2.88 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { h, Component, Fragment } from 'preact';
import 'preact/debug';
import { divIcon } from 'leaflet';
import {
Map, Marker, MarkerCluster, Polyline, TileLayer,
} from '../src';
import 'leaflet/dist/leaflet.css';
import 'leaflet.markercluster/dist/MarkerCluster.Default.css';
import './styles.css';
import route from './long-route';
const getRouteConfig = (hash) => {
switch (hash) {
case '#cluster':
return {
mapCenter: [63.83919, 20.15069],
markerCluster: [
[63.8374896962485, 20.163206074534],
[63.9374896962485, 20.163206074534],
[63.7374896962485, 20.163206074534],
[63.8574896962485, 20.163206074534],
[63.8174896962485, 20.163206074534],
[63.8274896962485, 20.163206074534],
[63.8974896962485, 20.163206074534],
],
markers: [],
polylines: [],
zoom: 8,
};
case '#polyline':
return {
mapCenter: [63.83919, 20.15069],
markerCluster: false,
markers: [
[59.3367, 18.0667],
[63.8374896962485, 20.163206074534],
],
polylines: [
route,
],
zoom: 5,
};
default:
return {
mapCenter: [59.3367, 18.0667],
markerCluster: false,
markers: [],
polylines: [],
zoom: 10,
};
}
};
export default class App extends Component {
constructor(props) {
super(props);
this.state = getRouteConfig(window.location.hash);
}
componentDidMount() {
window.addEventListener('popstate', () => {
this.setState(getRouteConfig(window.location.hash));
});
}
render(props, {
mapCenter, markerCluster, markers, polylines, zoom,
}) {
return (
<Fragment>
<header>
<h1>preact-leaflet</h1>
<ul className="menu">
{[
{ link: '#simple', title: 'Simple' },
{ link: '#cluster', title: 'Cluster' },
{ link: '#polyline', title: 'Polyline and markers' },
].map(({ link, title }) => (
<li className={window.location.hash === link ? 'active' : ''}>
<a href={link}>{title}</a>
</li>
))}
</ul>
</header>
<Map center={mapCenter} style={{ height: '100%' }} zoom={zoom}>
<TileLayer url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" />
{markers.map(position => (
<Marker icon={divIcon()} position={position} />
))}
{polylines.map(positions => (
<Polyline positions={positions} />
))}
{markerCluster && (
<MarkerCluster key="cluster">
{markerCluster.map(position => (
<Marker key={position.join(',')} icon={divIcon()} position={position} />
))}
</MarkerCluster>
)}
</Map>
</Fragment>
);
}
}