Проблемы с реакции-Google-карты + пользовательское информационное окно: не могу отобразить информационное окно - PullRequest
0 голосов
/ 25 сентября 2018

Я пытаюсь отобразить информационные окна на карте реагировать на google-maps, и документированный способ у меня не сработал.

Поэтому я решил изменить мир и создать пользовательские информационные окна.Google намеревался это.И я получаю сообщение об ошибке: b.get не является функцией.

Не могли бы вы мне помочь?

Вот мой код:

import React, { Component } from 'react';
import { Fragment } from 'react'
import {withScriptjs, withGoogleMap, GoogleMap, Marker, InfoWindow} from "react-google-maps";
import { MarkerClusterer } from "react-google-maps/lib/components/addons/MarkerClusterer"


let result = [];
let infoWindowOpen = true;

class Map extends Component {

  state = {
    venueInfo: 'https://api.londontheatredirect.com/rest/v2/Venues/',
    venues: [],
    markerPosition: [],
    map: []
  }

  componentDidMount() {

**Я получаю информацию из другого API, здесь нет ошибок **

    this.waitForGoogle()
  }




  waitForGoogle = (geocoder, map) => { !window.google?  (
      console.log('waiting for google'),
      setTimeout(this.waitForGoogle, 100)
    ) : (
    console.log(this.state),
      console.log('google is loaded'),
      geocoder = new window.google.maps.Geocoder(),
      map = window.google.maps.Map,

      this.setState({map: map}),

      this.geocodeAddress(geocoder)

    )
  }


  // implement markers from venue addresses
  geocodeAddress = (geocoder) => {
    this.state.venues.length === 0? (console.log('no venues found, waiting...'), setTimeout(this.geocodeAddress, 1000)):
    this.state.venues.slice(10, 20).map((venue) =>  // I HAD TO DO THIS TO AVOID GOOGLE API ERROR FOR TOO MANY REQUESTS

    {
      const address = venue[0]
      const name = venue[1]
      const that = this

      this.getAddress(geocoder, address, that)
    })
  }

  getAddress(geocoder, address, that) {
    if (address) {
      // ------------------
      //here is the handy code for geocode markers from google developers
      // ------------------
      geocoder = new window.google.maps.Geocoder();

      geocoder.geocode( { 'address': address, 'region': 'uk'}, function(results, status) {
        if (status === 'OK') {
          result.push([[results[0].geometry.location.lat(), results[0].geometry.location.lng()]])
        } else {
          alert('Geocode was not successful for the following reason: ' + status);
        }
        that.setState({ markerPosition: result})
      });
    }
  }

  drop() {
    if (window.google){
      return this.state.markerPosition.map((marker, index) =>
        <Fragment>
         <Marker
          onClick={() => {this.handleMarker(marker)}}
          key={index}
          position={{ lat: marker[0][0], lng: marker[0][1] }}
          />

        </Fragment>
      )
    }
  }

  handleMarker(marker) {
    console.log(this.state.map)
    console.log(marker)
     const infowindow = new window.google.maps.InfoWindow({
          content: '<h1>Placeholder text InfoWindow</h1>'
        });
    infowindow.open(this.state.map, marker);

    // let's show the infoWindow
    infoWindowOpen= true

  }

  render() {
    // variables from react-google-maps
    const MapWithAMarkerClusterer = withScriptjs(withGoogleMap(props =>
      <GoogleMap
        defaultZoom={this.props.mapZoom}
        defaultCenter={this.props.mapCenter}
      >
        <MarkerClusterer
          averageCenter
          enableRetinaIcons
          gridSize={60}
        >
          {this.drop()}
        </MarkerClusterer>
      </GoogleMap>
  ));

  return (
    <MapWithAMarkerClusterer
      googleMapURL="https://maps.googleapis.com/maps/api/js?key=AIzaSyBXHssNaFm57yh0sOVvfmjp8VkfiPTW7yY&v=3.exp&libraries=geometry,drawing,places"
      loadingElement={<div style={{ height: 'calc(100vh / 2)' }} />}
      containerElement={<div style={{ height: '100%' }} />}
      mapElement={<div style={{ height: `100%` }} />}
      visible = {true}
    />
  )

  }
}
export default Map;
...