您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

用React实现谷歌地图

用React实现谷歌地图

这是我先前回答的改进版本

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

首先,安装必要的库:

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;
其他 2022/1/1 18:23:02 有645人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶