File size: 2,810 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React from 'react';
import MarkerManager from '../../util/marker_manager';
import { withRouter } from 'react-router-dom';
import queryString from 'query-string';

class SpotMap extends React.Component {
    constructor(props) {
        super(props);
        this.renderMap = this.renderMap.bind(this);
    }

    componentDidMount() {
        this.renderMap();
    }

    componentDidUpdate(prevProps) {
        this.MarkerManager.updateMarkers(this.props.spots);
        if (prevProps.location.search !== this.props.location.search){
            this.renderMap();
        }
    }


    registerListeners() {
        google.maps.event.addListener(this.map, 'idle', () => {
            const { north, south, east, west } = this.map.getBounds().toJSON();

            let bounds = {
                northEast: { lat: north, lng: east },
                southWest: { lat: south, lng: west }
            };

            this.props.updateFilter("bounds", bounds);
        });
    }

    // registerListeners() {
    //     this.map.addListener('idle', () => {
    //         let { north, south, east, west } = this.map.getBounds().toJSON();
    //         let bounds = {
    //             northEast: { lat: north, lng: east },
    //             southWest: { lat: south, lng: west }
    //         };
    //         this.props.updateFilter('bounds', bounds);
    //     })
    // }

    renderMap(){

        // let newLat, newLng;

        // if (!this.props.search) {
        //     newLat = this.props.search.lat;
        //     newLng = this.props.search.lng
        // } else {
        //     newLat = 37.7758;
        //     newLng = -122.435;
        // }

        // const mapOptions = {
        //     center: {
        //         lat: parseFloat(newLat),
        //         lng: parseFloat(newLng)
        //     },
        //     zoom: 13
        // };

        let coords;

        if (this.props.location.search) {
            coords = queryString.parse(this.props.location.search);
        } else {
            coords = this.props.search;
        }

        const mapOptions = {
            center: { lat: parseFloat(coords.lat), lng: parseFloat(coords.lng) }, 
            zoom: 13
        };


        this.map = new google.maps.Map(this.mapNode, mapOptions);
        this.registerListeners();
        this.MarkerManager = new MarkerManager(this.map);

        this.MarkerManager.updateMarkers(this.props.spots);

    }

    componentWillUnmount(){
        google.maps.event.clearListeners(this.map,'idle');
        // this.map.event.clearListeners(this.map,'idle');
    }



    render() {
        return (
            <div
                className="map-container"
                ref={map => this.mapNode = map}> 
            </div>
        );
    }
}

export default withRouter(SpotMap);