2018-05-17 15:13:53 +02:00
|
|
|
import hash from 'object-hash'
|
2018-05-03 14:07:31 +02:00
|
|
|
import React from 'react'
|
2020-12-25 19:54:30 +01:00
|
|
|
import { GeoJSON, MapContainer, Marker, TileLayer } from 'react-leaflet'
|
2018-05-03 21:42:54 +02:00
|
|
|
import { connect } from 'react-redux'
|
2018-05-03 14:07:31 +02:00
|
|
|
|
2019-08-25 20:23:11 +02:00
|
|
|
import { getActivityGpx, getSegmentGpx } from '../../../actions/activities'
|
2020-09-16 11:47:20 +02:00
|
|
|
import { apiUrl } from '../../../utils'
|
2019-01-04 10:07:24 +01:00
|
|
|
import { getGeoJson } from '../../../utils/activities'
|
2018-05-03 14:07:31 +02:00
|
|
|
|
2018-05-03 21:42:54 +02:00
|
|
|
class ActivityMap extends React.Component {
|
2018-05-03 14:07:31 +02:00
|
|
|
constructor(props, context) {
|
|
|
|
super(props, context)
|
|
|
|
this.state = {
|
2018-05-03 21:42:54 +02:00
|
|
|
zoom: 13,
|
2018-05-03 14:07:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-03 21:42:54 +02:00
|
|
|
componentDidMount() {
|
2019-08-25 20:23:11 +02:00
|
|
|
if (this.props.dataType === 'activity') {
|
|
|
|
this.props.loadActivityGpx(this.props.activity.id)
|
|
|
|
} else {
|
|
|
|
this.props.loadSegmentGpx(this.props.activity.id, this.props.segmentId)
|
|
|
|
}
|
2018-05-03 21:42:54 +02:00
|
|
|
}
|
|
|
|
|
2018-05-17 15:13:53 +02:00
|
|
|
componentDidUpdate(prevProps) {
|
2019-08-28 15:35:22 +02:00
|
|
|
if (
|
2019-09-01 11:04:43 +02:00
|
|
|
(this.props.dataType === 'activity' &&
|
|
|
|
prevProps.activity.id !== this.props.activity.id) ||
|
|
|
|
(this.props.dataType === 'activity' && prevProps.dataType === 'segment')
|
2019-08-25 20:23:11 +02:00
|
|
|
) {
|
2019-08-28 15:35:22 +02:00
|
|
|
this.props.loadActivityGpx(this.props.activity.id)
|
2018-05-17 15:13:53 +02:00
|
|
|
}
|
2019-08-28 15:35:22 +02:00
|
|
|
if (
|
|
|
|
this.props.dataType === 'segment' &&
|
|
|
|
prevProps.segmentId !== this.props.segmentId
|
2019-08-25 20:23:11 +02:00
|
|
|
) {
|
|
|
|
this.props.loadSegmentGpx(this.props.activity.id, this.props.segmentId)
|
|
|
|
}
|
2018-05-17 15:13:53 +02:00
|
|
|
}
|
|
|
|
|
2018-05-04 22:30:11 +02:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.props.loadActivityGpx(null)
|
|
|
|
}
|
|
|
|
|
2018-05-03 14:07:31 +02:00
|
|
|
render() {
|
2020-09-16 13:01:15 +02:00
|
|
|
const { activity, coordinates, gpxContent, mapAttribution } = this.props
|
2018-05-14 14:51:03 +02:00
|
|
|
const { jsonData } = getGeoJson(gpxContent)
|
|
|
|
const bounds = [
|
|
|
|
[activity.bounds[0], activity.bounds[1]],
|
2019-08-28 15:35:22 +02:00
|
|
|
[activity.bounds[2], activity.bounds[3]],
|
2018-05-14 14:51:03 +02:00
|
|
|
]
|
2018-05-03 21:42:54 +02:00
|
|
|
|
2018-05-03 14:07:31 +02:00
|
|
|
return (
|
2018-05-03 21:42:54 +02:00
|
|
|
<div>
|
|
|
|
{jsonData && (
|
2020-12-25 19:54:30 +01:00
|
|
|
<MapContainer
|
2018-05-03 21:42:54 +02:00
|
|
|
zoom={this.state.zoom}
|
|
|
|
bounds={bounds}
|
2018-05-16 23:52:55 +02:00
|
|
|
boundsOptions={{ padding: [10, 10] }}
|
2018-05-03 21:42:54 +02:00
|
|
|
>
|
|
|
|
<TileLayer
|
|
|
|
// eslint-disable-next-line max-len
|
2020-09-16 13:01:15 +02:00
|
|
|
attribution={mapAttribution}
|
2020-09-16 11:47:20 +02:00
|
|
|
url={`${apiUrl}activities/map_tile/{s}/{z}/{x}/{y}.png`}
|
2018-05-03 21:42:54 +02:00
|
|
|
/>
|
2018-05-17 15:13:53 +02:00
|
|
|
<GeoJSON
|
|
|
|
// hash as a key to force re-rendering
|
|
|
|
key={hash(jsonData)}
|
|
|
|
data={jsonData}
|
|
|
|
/>
|
2019-08-25 16:06:24 +02:00
|
|
|
{coordinates.latitude && (
|
|
|
|
<Marker
|
|
|
|
position={[coordinates.latitude, coordinates.longitude]}
|
|
|
|
/>
|
|
|
|
)}
|
2020-12-25 19:54:30 +01:00
|
|
|
</MapContainer>
|
2018-05-03 21:42:54 +02:00
|
|
|
)}
|
|
|
|
</div>
|
2018-05-03 14:07:31 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2018-05-03 21:42:54 +02:00
|
|
|
|
|
|
|
export default connect(
|
|
|
|
state => ({
|
2019-08-28 15:35:22 +02:00
|
|
|
gpxContent: state.gpx,
|
2020-09-16 13:01:15 +02:00
|
|
|
mapAttribution: state.application.config.map_attribution,
|
2018-05-03 21:42:54 +02:00
|
|
|
}),
|
|
|
|
dispatch => ({
|
|
|
|
loadActivityGpx: activityId => {
|
|
|
|
dispatch(getActivityGpx(activityId))
|
|
|
|
},
|
2019-08-25 20:23:11 +02:00
|
|
|
loadSegmentGpx: (activityId, segmentId) => {
|
|
|
|
dispatch(getSegmentGpx(activityId, segmentId))
|
|
|
|
},
|
2018-05-03 21:42:54 +02:00
|
|
|
})
|
|
|
|
)(ActivityMap)
|