简体   繁体   English

google-maps-react:“未定义 google.maps.drawing”

[英]google-maps-react: "google.maps.drawing is not defined"

I have a web application that has two versions of Maps using the 'google-maps-react' module.我有一个使用“google-maps-react”模块有两个版本的地图的网络应用程序。

All the code will be available below.所有代码都将在下面提供。

The point of having two versions (each on a separate .js file) is that I want to have one page showing the map like usual with all the markers showing (also using the places library)有两个版本(每个版本都在一个单独的 .js 文件中)的重点是我希望有一个页面像往常一样显示地图并显示所有标记(也使用位置库) 带有所有标记的完整地图 And another page that uses Google's Drawing Manager, with no markers, and that allows you to place new markers, or polygons, that will become available in the Full Map (first page) after being sent to my server.另一个页面使用 Google 的绘图管理器,没有标记,允许您放置新的标记或多边形,这些标记或多边形在发送到我的服务器后将在完整地图(第一页)中可用。 在此处输入图像描述

Now my problem is everytime I'm on the first page and go to the second pages, after a couple seconds the app crashes with "google.maps.drawing" is undefined.现在我的问题是每次我在第一页并转到第二页时,几秒钟后应用程序因“google.maps.drawing”而崩溃是未定义的。 But when I start the application on the second page, it never crashes, no matter how many times I switch pages.但是当我在第二页上启动应用程序时,无论我切换多少次页面,它都不会崩溃。

I have no idea why this is happening but I assume it has something to do with asynchronous events (???).我不知道为什么会发生这种情况,但我认为它与异步事件有关(???)。

Can someone tell me why this is happening and how to fix it?有人能告诉我为什么会发生这种情况以及如何解决吗?

PS: If anyone has a better way/idea of doing these pages let me know. PS:如果有人有更好的方法/想法来做这些页面,请告诉我。

CODE TIME:代码时间:

Map with Drawing Manager:带有绘图管理器的地图:

/* global google */
import React from 'react';
import { Map, InfoWindow, GoogleApiWrapper } from 'google-maps-react';

class DrawingMap extends React.Component {

  constructor(props) {
    super(props);
    this.initMap = this.initMap.bind(this);
    this.state = {
      ...
    }
  }

  initMap(mapProps, map) {
    var self = this;
    const {google} = mapProps;

    const drawingManager = new google.maps.drawing.DrawingManager({
      drawingMode: null,
      drawingControl: false,
      drawingControlOptions: {
        position: google.maps.ControlPosition.TOP_CENTER,
        drawingModes: [
          google.maps.drawing.OverlayType.MARKER,
          google.maps.drawing.OverlayType.POLYGON
        ]
      },
      map: map
    });

  /*events and listeners and blah blah*/
  }

  render() {
    return (
      <Map google={this.props.google}
           onReady={this.initMap}
           onClick={this.onMapClicked}
           initialCenter={{...}}
           zoom={15}
           yesIWantToUseGoogleMapApiInternals >
      <InfoWindow
        visible={this.showingInfoWindow} >
      </InfoWindow>
      </Map>
    );
  }
}

export default GoogleApiWrapper({
  apiKey: key,
  libraries: ['drawing'],
  LoadingContainer: LoadingContainer
})(DrawingMap);

Map with no Drawing Manager没有绘图管理器的地图

/* global google */
import React from 'react';
import { Map, Marker, Polygon, InfoWindow, GoogleApiWrapper } from "google-maps-react";

class FullMap extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      ..
    }
  }

  initMap = (mapProps, map) => {
    var self = this;
    const { markers } = this.state;
    const {google} = mapProps;

    /* event listeners and whatnot */
  }

  render() {
    const {markers, zoom, activeMarker, activePolygon, mapCenter} = this.state;
    return (
      <Map google={this.props.google}
           onReady={this.initMap}
           initialCenter={{lat:mapCenter.lat, lng: mapCenter.lng}}
           center={{lat:mapCenter.lat, lng: mapCenter.lng}}
           zoom={14}
           streetViewControl={false}
           yesIWantToUseGoogleMapApiInternals>
      {markers && markers.map((marker, index) => marker && this.loadMarker(marker, index))}
      </Map>
    );
  }
}

export default GoogleApiWrapper({
  apiKey: key,
  libraries: ['places','geometry']
})(FullMap);
const drawingManager = new google.maps.drawing.DrawingManager({
  drawingMode: null,
  drawingControl: true,
  drawingControlOptions: {
    position: google.maps.ControlPosition.TOP_CENTER,
    drawingModes: [
      google.maps.drawing.OverlayType.MARKER,
      google.maps.drawing.OverlayType.POLYGON,
    ],
  },
  map: map,
});

===> drawingControl: true ===>绘图控制:真

您只需将绘图添加到您的库数组中,返回的地图上就会有绘图道具。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM