|
1 |
| -import { latLngBounds } from 'leaflet' |
2 |
| -import polyline from '@mapbox/polyline' |
3 | 1 | import objectPath from 'object-path'
|
4 | 2 | import React, { Component, PropTypes } from 'react'
|
5 | 3 | import { connect } from 'react-redux'
|
6 |
| -import { Map, TileLayer, LayersControl, Marker, Popup } from 'react-leaflet' |
| 4 | +import { Map, TileLayer, LayersControl } from 'react-leaflet' |
7 | 5 |
|
8 | 6 | import { setLocation } from '../../actions/map'
|
9 | 7 | import { constructLocation } from '../../util/map'
|
10 | 8 | import { getActiveItinerary, getActiveSearch } from '../../util/state'
|
| 9 | +import { getItineraryBounds } from '../../util/itinerary' |
11 | 10 |
|
12 | 11 | class BaseMap extends Component {
|
13 | 12 | static propTypes = {
|
@@ -45,20 +44,19 @@ class BaseMap extends Component {
|
45 | 44 |
|
46 | 45 | componentWillReceiveProps(nextProps) {
|
47 | 46 | // TODO: maybe setting bounds ought to be handled in map props...
|
48 |
| - // Pan to to entire itinerary if made active (clicked) |
49 |
| - /*if (nextProps.itinerary && nextProps.activeLeg === null) { |
50 |
| - let coords = [] |
51 |
| - nextProps.itinerary.legs.forEach(leg => { |
52 |
| - const legCoords = polyline |
53 |
| - .toGeoJSON(leg.legGeometry.points) |
54 |
| - .coordinates.map(c => [c[1], c[0]]) |
55 |
| - coords = [...coords, ...legCoords] |
56 |
| - }) |
| 47 | + |
| 48 | + // Fit map to to entire itinerary if active itinerary bounds changed |
| 49 | + const oldItinBounds = this.props.itinerary && getItineraryBounds(this.props.itinerary) |
| 50 | + const newItinBounds = nextProps.itinerary && getItineraryBounds(nextProps.itinerary) |
| 51 | + if ( |
| 52 | + (!oldItinBounds && newItinBounds) || |
| 53 | + (oldItinBounds && newItinBounds && !oldItinBounds.equals(newItinBounds)) |
| 54 | + ) { |
57 | 55 | this.refs.map &&
|
58 |
| - this.refs.map.leafletElement.fitBounds(latLngBounds(coords), { |
| 56 | + this.refs.map.leafletElement.fitBounds(newItinBounds, { |
59 | 57 | padding: [3, 3]
|
60 | 58 | })
|
61 |
| - }*/ |
| 59 | + } |
62 | 60 | // Pan to to itinerary step if made active (clicked)
|
63 | 61 | if (
|
64 | 62 | nextProps.itinerary &&
|
@@ -86,6 +84,12 @@ class BaseMap extends Component {
|
86 | 84 |
|
87 | 85 | resized () {
|
88 | 86 | this.refs.map.leafletElement.invalidateSize()
|
| 87 | + if (this.props.itinerary) { |
| 88 | + this.refs.map && |
| 89 | + this.refs.map.leafletElement.fitBounds(getItineraryBounds(this.props.itinerary), { |
| 90 | + padding: [3, 3] |
| 91 | + }) |
| 92 | + } |
89 | 93 | }
|
90 | 94 |
|
91 | 95 | render () {
|
|
0 commit comments