Skip to content

Commit efb59d8

Browse files
committed
feat(map): use right click (long press on mobile) to set location
fixes #21
1 parent d55efc8 commit efb59d8

File tree

1 file changed

+42
-8
lines changed

1 file changed

+42
-8
lines changed

lib/components/map/base-map.js

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import polyline from '@mapbox/polyline'
33
import objectPath from 'object-path'
44
import React, { Component, PropTypes } from 'react'
55
import { connect } from 'react-redux'
6-
import { Map } from 'react-leaflet'
6+
import { Map, Popup } from 'react-leaflet'
77

88
import { setLocation } from '../../actions/map'
99
import { constructLocation } from '../../util/map'
@@ -15,11 +15,21 @@ class BaseMap extends Component {
1515
mapClick: PropTypes.func,
1616
setLocation: PropTypes.func // TODO: rename from action name to avoid namespace conflict?
1717
}
18-
_onClick = (e) => {
19-
const location = constructLocation(e.latlng)
20-
const reverseGeocode = true
21-
if (!this.props.isFromSet) this.props.setLocation({type: 'from', location, reverseGeocode})
22-
else if (!this.props.isToSet) this.props.setLocation({type: 'to', location, reverseGeocode})
18+
state = {}
19+
_onClickTo = () => {
20+
const {setLocation} = this.props
21+
const location = constructLocation(this.state.popupPosition)
22+
setLocation({type: 'to', location, reverseGeocode: true})
23+
}
24+
25+
_onClickFrom = () => {
26+
const {setLocation} = this.props
27+
const location = constructLocation(this.state.popupPosition)
28+
setLocation({type: 'from', location, reverseGeocode: true})
29+
}
30+
31+
_onRightClick = (e) => {
32+
this.setState({popupPosition: e.latlng})
2333
}
2434
// TODO: make map controlled component
2535
_mapBoundsChanged = (e) => {
@@ -72,18 +82,21 @@ class BaseMap extends Component {
7282
children
7383
} = this.props
7484
const position = [config.map.initLat, config.map.initLon]
85+
const {popupPosition} = this.state
7586
// const position = [+mapState.lat, +mapState.lon]
7687
// const zoom = +mapState.zoom
7788
const zoom = config.map.initZoom || 13
7889
const bounds = null // mapState.bounds
90+
// TODO: currently mapProps is unused, but may later be used for controlling
91+
// map location state
7992
const mapProps = {
8093
ref: 'map',
8194
className: 'map',
8295
// center: position,
8396
// bounds: mapState.bounds || null,
8497
// zoom: config.initZoom,
8598
// zoom: +mapState.zoom,
86-
onClick: this._onClick
99+
onContextMenu: this._onRightClick
87100
// onMoveEnd: this._mapBoundsChanged,
88101
// onZoomEnd: this._mapBoundsChanged,
89102
}
@@ -101,9 +114,30 @@ class BaseMap extends Component {
101114
className='map'
102115
center={position}
103116
zoom={config.map.initZoom || 13}
104-
onClick={this._onClick}
117+
onContextMenu={this._onRightClick}
105118
>
106119
{children}
120+
{popupPosition
121+
? <Popup ref='clickPopup'
122+
key={popupPosition.toString()} // hack to ensure the popup opens only on right click
123+
position={popupPosition} // FIXME: onOpen and onClose don't seem to work?
124+
// onOpen={() => this.setState({popupPosition: null})}
125+
// onClose={() => this.setState({popupPosition: null})}
126+
>
127+
<span>
128+
Plan a trip:{' '}
129+
<a role='button'
130+
onClick={this._onClickFrom}>
131+
From here
132+
</a>{' '}|{' '}
133+
<a role='button'
134+
onClick={this._onClickTo}>
135+
To here
136+
</a>
137+
</span>
138+
</Popup>
139+
: null
140+
}
107141
</Map>
108142
)
109143
}

0 commit comments

Comments
 (0)