Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* global google */
  2. import React, { Component } from "react";
  3. import { Map, Polygon, GoogleApiWrapper } from "google-maps-react";
  4. import Axios from 'axios';
  5.  
  6. export class MapContainer extends Component {
  7.   constructor(props) {
  8.     super(props);
  9.     this.polygonRef = React.createRef();
  10.     this.state = {
  11.       toggleColor: false,
  12.       data: [],
  13.       polygons: []
  14.     };
  15.     this.fillColors = ["#8686B3","#F14288"];
  16.   }
  17.  
  18.   setPolygonOptions = options => {
  19.     this.polygonRef.current.polygon.setOptions(options);
  20.   };
  21.  
  22.   handleClick = e => {
  23.     const currentColor = this.state.toggleColor
  24.       ? this.fillColors[0]
  25.       : this.fillColors[1];
  26.     this.setPolygonOptions({ fillColor: currentColor });
  27.     this.setState(prevState => ({
  28.       toggleColor: !prevState.toggleColor
  29.     }));
  30.   };
  31.  
  32.   getPoly(){
  33.     Axios.get("/load").then(res => {
  34.       return res.data.features
  35.     }).then(data => {
  36.         this.setState({data: data})
  37.     })
  38.   }
  39.  
  40.   componentDidMount() {
  41.     this.getPoly();
  42.   }
  43.  
  44.   render() {
  45.     let polys = this.state.data.map((coordinates, index) => {
  46.         return(
  47.           <Polygon
  48.             key={index}  // La til indexen som en key for å kunne finne igjen angitt Polygon
  49.             ref={this.polygonRef}
  50.             onClick={this.handleClick}
  51.             paths={coordinates}
  52.             strokeOpacity={0.8}
  53.             strokeWeight={2}
  54.             fillColor={this.fillColors[0]}
  55.             fillOpacity={0.35}
  56.            />
  57.         )
  58.       })
  59.  
  60.     return (
  61.       <div className="map-container">
  62.         <Map
  63.           google={this.props.google}
  64.           className={"map"}
  65.           zoom={14}
  66.           initialCenter={{
  67.             lat: 51.5,
  68.             lng: -0.14
  69.            }}
  70.         >
  71.         {polys}
  72.         </Map>
  73.       </div>
  74.     );
  75.   }
  76. }
  77.  
  78. export default GoogleApiWrapper({
  79.   apiKey: ""
  80. })(MapContainer);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement