diff options
| author | Erik Larkö <erik@mullvad.net> | 2017-07-11 08:31:47 +0200 |
|---|---|---|
| committer | Erik Larkö <erik@mullvad.net> | 2017-07-11 08:31:47 +0200 |
| commit | a96f21751907f50370b146aa92a893db9482f5f3 (patch) | |
| tree | 6a31a394beb7b72ef8e4fdcb51a474ffffe3f0a8 /app/components/Map.js | |
| parent | dc4eb2ad10341bf694599ef9b741b1107feac903 (diff) | |
| parent | 77081e74df3b8408895b9670a8288ea3896402cd (diff) | |
| download | mullvadvpn-a96f21751907f50370b146aa92a893db9482f5f3.tar.xz mullvadvpn-a96f21751907f50370b146aa92a893db9482f5f3.zip | |
Merge branch 'extract-map'
Diffstat (limited to 'app/components/Map.js')
| -rw-r--r-- | app/components/Map.js | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/app/components/Map.js b/app/components/Map.js new file mode 100644 index 0000000000..8670397891 --- /dev/null +++ b/app/components/Map.js @@ -0,0 +1,51 @@ +// @flow + +import React, { Component } from 'react'; +import ReactMapboxGl, { Marker } from 'react-mapbox-gl'; +import { mapbox as mapboxConfig } from '../config'; +import cheapRuler from 'cheap-ruler'; + +import type { Coordinate2d } from '../types'; + +const ReactMap = ReactMapboxGl({ + accessToken: mapboxConfig.accessToken, + attributionControl: false, + interactive: false, +}); + +export class Map extends Component { + props: { + animate: boolean, + location: Coordinate2d, + altitude: number, + markerImagePath: string, + } + + render() { + + const mapBounds = this.calculateMapBounds(this.props.location, this.props.altitude); + + const mapBoundsOptions = { offset: [0, -113], animate: this.props.animate }; + + return <ReactMap style={ mapboxConfig.styleURL } + containerStyle={{ height: '100%' }} + fitBounds={ mapBounds } + fitBoundsOptions={ mapBoundsOptions }> + + <Marker coordinates={ this.convertToMapCoordinate(this.props.location) } offset={ [0, -10] }> + <img src={ this.props.markerImagePath } /> + </Marker> + </ReactMap>; + } + + calculateMapBounds(center: Coordinate2d, altitude: number): [Coordinate2d, Coordinate2d] { + const bounds = cheapRuler(center[0], 'meters').bufferPoint(center, altitude); + // convert [lat,lng] bounds to [lng,lat] + return [ [bounds[1], bounds[0]], [bounds[3], bounds[2]] ]; + } + + convertToMapCoordinate(pos: Coordinate2d): Coordinate2d { + // convert [lat,lng] bounds to [lng,lat] + return [pos[1], pos[0]]; + } +} |
