【问题标题】:Implementing google maps with react用 React 实现谷歌地图
【发布时间】:2017-04-24 04:59:43
【问题描述】:

在这里反应新手,请多多包涵 :) 希望这很容易解决

目前,我只是想让地图出现在屏幕上,但是当我运行代码时,页面完全是空白的,甚至我的其他 div 也没有出现。这是我的代码,要点:https://gist.github.com/JoeyBodnar/f76c5d434d57c6cc6108513ad79d4cb7

需要注意的几点:1) 从项目目录中,我已经运行了 npm install --save react-google-maps 2) 使地图出现的代码取自这里:https://github.com/tomchentw/react-google-maps

那么我到底做错了什么?我在错误的地方声明“const GettingStartedGoogleMap”吗?还是我在 div 中做错了什么?

还要注意,如果我从该文件中删除所有与谷歌地图相关的代码,一切都会正常工作。

感谢任何帮助,谢谢

编辑,这是我的代码,即使是硬编码高度仍然显示空白屏幕:

 return (
     <GettingStartedGoogleMap
 containerElement={
    <div style={{ height: `400px` }} />
 }
 mapElement={
 <div style={{ height: `400px` }} />
 }
 onMapLoad={_.noop}
 onMapClick={_.noop}
 markers={markers}
 onMarkerRightClick={_.noop}
/>
  );

【问题讨论】:

  • 您是否遇到了一些控制台错误? sn-p 可能是必须的

标签: google-maps reactjs


【解决方案1】:

这是我之前回答的改进版

我刚刚测试了您的代码,其中包含许多错误、未定义和未使用的变量! 因此,您可以改用以下代码,这非常简单(这将让您通过当前问题并显示地图!)

首先,安装必要的库:

npm install --save google-map-react
npm install --save prop-types

然后,您可以复制下面的整个内容:

import React, { Component } from 'react';
import GoogleMapReact from 'google-map-react';

const AnyReactComponent = ({ text }) => <div>{text}</div>;

class MyClass extends Component {
  constructor(props){
    super(props);

  }

  render() {
    return (
      <GoogleMapReact
        defaultCenter={this.props.center}
        defaultZoom={this.props.zoom}
        style={{height: '300px'}}
      >
        <AnyReactComponent
          lat={59.955413}
          lng={30.337844}
          text={'Google Map'}
        />
      </GoogleMapReact>
    );
  }
}
MyClass.defaultProps = {
  center: {lat: 59.95, lng: 30.33},
  zoom: 11
};

export default MyClass;

【讨论】:

  • 您也可以使用我之前回答中的解决方案来动态设置地图高度。我还建议你阅读一些关于 React 组件生命周期的文档:facebook.github.io/react/docs/…
  • 嘿,您能否提供一个用于动态添加标记的示例代码,带有图像.. 如果可以,我将非常感谢您!
  • @HelpingHand 你可以试试:(只改变 1 行)const AnyReactComponent = ({ text }) =&gt; &lt;div&gt;&lt;img src="YOUR-IMG-SRC" className="YOUR-IMG-CLASS" /&gt;&lt;/div&gt;; 你也可以像反应一样添加样式:style={{width: '100px', height: '100px',}} 如果这对你有用,请考虑给我一个 up-也投票,谢谢
  • @thinhvo0108 解决了添加图像!但是你能给我一个例子如何动态添加标记吗?或者我会问一个问题并将链接发送给您?非常感谢!
  • 嘿@thinhvo0108 这是链接..stackoverflow.com/questions/43937887/…
【解决方案2】:

以像素为单位设置&lt;div style={{ height: 400px }} /&gt; 的高度。

    <GettingStartedGoogleMap
     containerElement={
        <div style={{ height: `400px` }} />
   }
   mapElement={
     <div style={{ height: `400px` }} />
   }
   onMapLoad={_.noop}
   onMapClick={_.noop}
   markers={markers}
   onMarkerRightClick={_.noop}
  />

【讨论】:

    【解决方案3】:

    google-map-react这样的npm模块

    在调试时所有的 div 都在绝对位置

      width: 100%;
      height: 85vh;
      position: relative;
    

    将解决问题

    【讨论】:

      【解决方案4】:

      事实是:GoogleMap 要求在渲染之前正确设置元素的长度! 因此,您可以:

      • 为您的地图元素设置静态高度:(第 62 行)

        mapElement={
          <div style={{ height: `300px` }} />
        }
        

      • 页面加载后使用脚本动态设置:

        componentDidMount() {
            console.log("component is mounted");
            this.setState({
              pageHeight = document.documentElement.offsetHeight + 'px'
            });
        }
        ...
        mapElement={
          <div style={{ height: this.state.pageHeight }} />
        }        
        

      对不起,我没有足够的时间测试您的代码,我可以根据自己的经验看到问题!因此,如果地图尚未显示,请随意为您的“containerElement”元素设置像素高度!

      编辑版本如下:

      请阅读我的新答案

      【讨论】:

      • 我已经尝试过了,但页面仍然呈现空白屏幕,请参阅我的编辑。
      • 我刚刚发布了新的编辑,你介意再试一次吗?此外,如果可能的话,请告诉我一些错误,以防代码还不能工作! (也许你还需要安装一些缺失的 react 库左右)
      • 对不起,我刚刚添加了另一个编辑:“npm install --save prop-types” 我在该示例中使用的 GoogleMap 库似乎需要该库,所以也可以随意安装它!
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-18
      • 2011-04-19
      相关资源
      最近更新 更多