summaryrefslogtreecommitdiff
path: root/src/Components/Map.tsx
blob: 68698f4e727b370dc19932d3d6853a30e29363d7 (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
import React, { Component } from 'react';
import { StyleSheet, View, Dimensions, ActivityIndicator } from 'react-native';
import MapView, { Marker, Region, UrlTile } 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 { getBottomSpace } from '../helper';
import actions from '../Actions/actions';
import { LatLng } from '../Api/interfaces';

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,
        }
    }
    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) => {
        this.setState({ 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) => {
        this.setState({ selectedPortal: { guid, coords } })
    }

    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>
        }

        return (
            <>
                <MapView
                    ref={r => (r != null) ? this.map = r : null}
                    style={styles.container}
                    initialRegion={{ ...this.state.user, latitudeDelta: 0.002, longitudeDelta: 0.002 }}
                    onRegionChangeComplete={this.onRegionChange}
                    showsCompass={false}
                    showsScale
                    showsUserLocation
                    showsMyLocationButton
                    loadingEnabled
                    type={'hybrid'}
                >
                    <MapObjects onPortalClick={this.onPortalClick} />
                    {this.state.selectedPortal && <Marker coordinate={this.state.selectedPortal.coords} />}
                </MapView>
                <MapOverlay
                    goToMe={this.goToMe}
                    refresh={this.refresh}
                    loading={this.props.entities.loadQueue.length}
                    selectedPortal={this.state.selectedPortal}
                    onOpenPortal={this.onOpenPortal}
                />
            </>
        );
    }
}

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

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