【问题标题】:React and Google Maps with google-maps-react, cant get to API's methods使用 google-maps-react React 和 Google Maps,无法访问 API 的方法
【发布时间】:2018-02-19 12:35:18
【问题描述】:

我正在使用 google-maps-react,它工作得很好,但我只是不明白如何在我的组件中使用 Google Maps API 的方法。现在我需要获取渲染地图的边界,但找不到任何方法来做到这一点。这是代码,任何帮助将不胜感激。

import React from 'react';
import {Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps-react';

const GOOGLE_MAPS_JS_API_KEY='AIzaSyB6whuBhj_notrealkey';


class GoogleMap extends React.Component {
constructor() {

    this.state = {
        zoom: 13
    }

    this.onMapClicked = this.onMapClicked.bind(this);
    this.test = this.test.bind(this);
}

onMapClicked (props) {
    if (this.state.showingInfoWindow) {
        this.setState({
            showingInfoWindow: false,
            activeMarker: null
        })
    }
}

test(google) {
// Here i tried a lot of ways to get coords somehow
    console.log(google.maps.Map.getBounds())
}

render() {
    const {google} = this.props;

    if (!this.props.loaded) {
        return <div>Loading...</div>
    }

    return (
        <Map className='google-map'
            google={google}
            onClick={this.onMapClicked}
            zoom={this.state.zoom}
            onReady={() => this.test(google)}
            >
        </Map>
        );
    }
}

export default GoogleApiWrapper({
apiKey: (GOOGLE_MAPS_JS_API_KEY)
})(GoogleMap);

谷歌地图 API v 3.30.4

【问题讨论】:

标签: javascript reactjs google-maps google-maps-api-3 google-maps-react


【解决方案1】:

您可以尝试将您的要求调整为以下示例here

据我所知,使用 onReady 属性返回了一个引用。

例如:

import React from 'react';
import {Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps-react';

const GOOGLE_MAPS_JS_API_KEY='AIzaSyB6whuBhj_notrealkey';


class GoogleMap extends React.Component {
constructor() {

    this.state = {
        zoom: 13
    }

    this.onMapClicked = this.onMapClicked.bind(this);
    this.handleMapMount = this.handleMapMount.bind(this);
}

onMapClicked (props) {
    if (this.state.showingInfoWindow) {
        this.setState({
            showingInfoWindow: false,
            activeMarker: null
        })
    }
}

handleMapMount(mapProps, map) {
    this.map = map;

    //log map bounds
    console.log(this.map.getBounds());
}

render() {
    const {google} = this.props;

    if (!this.props.loaded) {
        return <div>Loading...</div>
    }

    return (
        <Map className='google-map'
            google={google}
            onClick={this.onMapClicked}
            zoom={this.state.zoom}
            onReady={this.handleMapMount}
            >
        </Map>
        );
    }
}

export default GoogleApiWrapper({
apiKey: (GOOGLE_MAPS_JS_API_KEY)
})(GoogleMap);

【讨论】:

  • 不以这种方式工作(甚至不调用 handleMapMount 因为 onMapMounted 不会触发任何东西,onReady 道具在地图渲染后触发,但我仍然只在控制台中收到错误我尝试通过 onReady 完成。我可以使用 refs 获取 this.map,但仍然没有方法 getBounds 或任何其他方法,只有关于渲染地图的所有信息。here is some screenshotsmb it will help
  • @dimabgood 所以应该更改handleMapMount,我会更新我的答案。
  • @dimabgood 我已经更新了我的答案。你现在可以试试。 onReady 属性应该回调 mapProps 和地图对象(地图是第二个)
  • this.map.getBounds() 仍然是 undef,我又得到了很多这张地图的道具,但仍然没有 api 的方法screenshot
  • @dimabgood 能否请您链接代码的屏幕截图?还要确保您拥有该库的最新版本。
猜你喜欢
  • 2021-08-24
  • 2023-02-12
  • 2018-12-15
  • 1970-01-01
  • 2019-10-19
  • 2019-10-12
  • 2020-11-30
  • 2020-02-22
  • 1970-01-01
相关资源
最近更新 更多