Skip to content

Commit

Permalink
feat: Basic offline map style functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
gmaclennan committed May 1, 2019
1 parent ad4e848 commit 8dbb308
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/frontend/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export function getMediaUrl(attachmentId: string, size: ImageSize): string {
return `${BASE_URL}media/${size}/${attachmentId}`;
}

export function getMapStyleUrl(id: string): string {
return `${BASE_URL}styles/${id}/style.json`;
}

export function savePhoto({
fullUri,
thumbnailUri
Expand Down
7 changes: 6 additions & 1 deletion src/frontend/screens/MapScreen.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { View } from "react-native";

import MapView from "../sharedComponents/MapView";
import ObservationsContext from "../context/ObservationsContext";
import { getMapStyleUrl } from "../api";

type Props = {
onAddPress: () => void
Expand All @@ -13,7 +14,11 @@ const MapScreen = ({ onAddPress }: Props) => (
<View style={{ flex: 1 }}>
<ObservationsContext.Consumer>
{({ observations }) => (
<MapView observations={observations} onAddPress={onAddPress} />
<MapView
observations={observations}
onAddPress={onAddPress}
mapStyle={getMapStyleUrl("default")}
/>
)}
</ObservationsContext.Consumer>
</View>
Expand Down
16 changes: 11 additions & 5 deletions src/frontend/sharedComponents/MapView.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,17 +83,20 @@ class ObservationMapLayer extends React.PureComponent<{

type Props = {
observations: ObservationsMap,
mapStyle?: MapStyle,
mapStyle?: string | MapStyle,
onAddPress: () => void,
onPressObservation: (observationId: string) => void
};

class MapView extends React.Component<Props> {
class MapView extends React.Component<Props, { loadError: boolean }> {
static defaultProps = {
onAddPress: () => {},
onPressObservation: () => {}
onPressObservation: () => {},
mapStyle: "mapbox://styles/mapbox/outdoors-v9"
};

state = {};

constructor(props: Props) {
super(props);
MapboxGL.setAccessToken(
Expand Down Expand Up @@ -134,7 +137,7 @@ class MapView extends React.Component<Props> {
};

render() {
const { observations, onAddPress } = this.props;
const { observations, onAddPress, mapStyle } = this.props;

return (
<>
Expand All @@ -147,7 +150,10 @@ class MapView extends React.Component<Props> {
rotateEnabled={false}
onPress={this.handleObservationPress}
compassEnabled={false}
styleURL="mapbox://styles/mapbox/outdoors-v9"
styleURL={
this.state.loadError ? MapView.defaultProps.mapStyle : mapStyle
}
onDidFailLoadingMap={e => this.setState({ loadError: true })}
>
<ObservationMapLayer
onPress={this.handleObservationPress}
Expand Down

0 comments on commit 8dbb308

Please sign in to comment.