Skip to content

Commit cb61e89

Browse files
author
David Emory
committed
fix(map): Fix issues related to map auto-fitting to results
1 parent 2c8eaec commit cb61e89

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

lib/components/map/base-map.js

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
1-
import { latLngBounds } from 'leaflet'
2-
import polyline from '@mapbox/polyline'
31
import objectPath from 'object-path'
42
import React, { Component, PropTypes } from 'react'
53
import { connect } from 'react-redux'
6-
import { Map, TileLayer, LayersControl, Marker, Popup } from 'react-leaflet'
4+
import { Map, TileLayer, LayersControl } from 'react-leaflet'
75

86
import { setLocation } from '../../actions/map'
97
import { constructLocation } from '../../util/map'
108
import { getActiveItinerary, getActiveSearch } from '../../util/state'
9+
import { getItineraryBounds } from '../../util/itinerary'
1110

1211
class BaseMap extends Component {
1312
static propTypes = {
@@ -45,20 +44,19 @@ class BaseMap extends Component {
4544

4645
componentWillReceiveProps(nextProps) {
4746
// 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+
) {
5755
this.refs.map &&
58-
this.refs.map.leafletElement.fitBounds(latLngBounds(coords), {
56+
this.refs.map.leafletElement.fitBounds(newItinBounds, {
5957
padding: [3, 3]
6058
})
61-
}*/
59+
}
6260
// Pan to to itinerary step if made active (clicked)
6361
if (
6462
nextProps.itinerary &&
@@ -86,6 +84,12 @@ class BaseMap extends Component {
8684

8785
resized () {
8886
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+
}
8993
}
9094

9195
render () {

lib/util/itinerary.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import React from 'react'
2+
import { latLngBounds } from 'leaflet'
3+
import polyline from '@mapbox/polyline'
24

35
import ModeIcon from '../components/icons/mode-icon'
46

@@ -70,3 +72,14 @@ export function getModeIcon (mode, customIcons) {
7072
}
7173
return <ModeIcon mode={mode} />
7274
}
75+
76+
export function getItineraryBounds (itinerary) {
77+
let coords = []
78+
itinerary.legs.forEach(leg => {
79+
const legCoords = polyline
80+
.toGeoJSON(leg.legGeometry.points)
81+
.coordinates.map(c => [c[1], c[0]])
82+
coords = [...coords, ...legCoords]
83+
})
84+
return latLngBounds(coords)
85+
}

0 commit comments

Comments
 (0)