summaryrefslogtreecommitdiff
path: root/src/Components/Map.tsx
blob: 6c3ebab00d78e28a2897f61842ccc8e72c974fcb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import React, { Component } from 'react';
import { StyleSheet, View, Dimensions, ActivityIndicator, StatusBar } from 'react-native';
import MapView, { Marker, Region } from 'react-native-maps';
import { connect } from 'redux-su';
import { NavigationActions } from 'react-navigation';

import MapOverlay from './MapOverlay';
import MapObjects from './MapObjects';
import PortalPanel from './PortalPanel';
import debounce, { getBottomSpace } from '../helper';
import actions from '../Actions/actions';
import { LatLng } from '../Api/interfaces';
import { getZoomByRegion, getDataZoomForMapZoom } from '../Api/api';

const { width, height } = Dimensions.get("screen")
const draggableRange = {
    top: height / 1.75,
    bottom: 120 + getBottomSpace()
}

type Props = any
type State = any
class Map extends Component<Props, State> {
    static navigationOptions = ({ navigation }) => {
        return {
            title: 'Карта',
        };
    };
    refreshTimer: number | undefined
    map!: MapView;
    constructor(props: Props) {
        super(props)
        this.state = {
            user: undefined,
            region: null,
            dataZoom: 15,
        }
        this.load = debounce(this.load, 300)
    }
    componentDidMount() {
        this.refreshTimer = setInterval(() => {
            this.refresh()
        }, 30000)
    }
    componentWillUnmount() {
        clearInterval(this.refreshTimer)
    }
    componentWillMount() {
        const setPosition = (position) => {
            this.setState({
                user: {
                    latitude: position.coords.latitude,
                    longitude: position.coords.longitude
                },
            });
        }
        navigator.geolocation.getCurrentPosition(
            setPosition,
            error => alert(error.message),
            { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
        );
        navigator.geolocation.watchPosition(
            setPosition,
            error => alert(error.message),
            { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
        )
    }
    onRegionChange = (region: Region) => {
        const zoom = getZoomByRegion(width, region)
        const dataZoom = getDataZoomForMapZoom(zoom);
        this.setState({ region, dataZoom })
        this.props.actions.setRegion(region)
        setImmediate(() => this.load(false))
    }

    refresh = () => {
        setImmediate(() => this.load(true))
    }

    load = async (refresh: boolean) => {
        if (this.state.region != null) {
            this.props.actions.update(this.state.region, width, refresh)
        }
        return null
    }

    onPortalClick = (guid: string, coords: LatLng) => {
        if (this.state.selectedPortal && this.state.selectedPortal.guid == guid) {
            this.setState({ selectedPortal: false })
        } else {
            this.setState({ selectedPortal: { guid, coords } })
        }
    }

    onPortalDismiss = () => {
        this.setState({ selectedPortal: false })
    }

    onOpenPortal = (guid: string, coords: LatLng) => {
        const navigateAction = NavigationActions.navigate({
            routeName: 'Portal',
            params: { guid, coords },
        });
        this.props.navigation.dispatch(navigateAction);
    }

    goToMe = () => {
        this.map.animateToCoordinate(this.state.user)
    }

    render() {
        if (!this.state.user) {
            return <View style={styles.spinnerContainer}><ActivityIndicator size={'large'} /></View>
        }
        const initialRegion = this.props.settings.region || { ...this.state.user, latitudeDelta: 0.002, longitudeDelta: 0.002 }
        return (
            <>
                <StatusBar
                    backgroundColor="blue"
                    barStyle="dark-content"
                />
                <MapView
                    ref={r => (r != null) ? this.map = r : null}
                    style={styles.container}
                    initialRegion={initialRegion}
                    onRegionChangeComplete={this.onRegionChange}
                    showsCompass={false}
                    showsScale
                    showsUserLocation
                    userLocationAnnotationTitle=""
                    showsMyLocationButton
                    loadingEnabled
                    type={'hybrid'}
                    shouldRasterizeIOS
                    renderToHardwareTextureAndroid
                >
                    <MapObjects
                        onPortalClick={this.onPortalClick}
                        region={this.state.region || initialRegion}
                        levels={this.props.settings.filterLevel}
                        selectedPortal={this.state.selectedPortal}
                        zoom={this.state.dataZoom}
                    />
                </MapView>
                <MapOverlay
                    goToMe={this.goToMe}
                    refresh={this.refresh}
                    loading={this.props.entities.loadQueue.length}
                    selectedPortal={this.state.selectedPortal}
                    onOpenPortal={this.onOpenPortal}
                    getPortalDetails={this.props.actions.getPortalDetails}
                    portal={this.state.selectedPortal && this.props.entities.portals[this.state.selectedPortal.guid]}
                />
            </>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
    },
});

export default connect({ 'entities': 'entities', 'settings': 'settings' }, actions)(Map)