2

函数 apiIsLoaded 没有被调用,并且 map 不能被初始化。怎么了?检查时,我可以看到没有谷歌地图 iframe 的 div,只是空白。我需要调用 apiIsLoaded,但找不到任何方法来做到这一点。这是代码,任何帮助将不胜感激。

import React from 'react';
import GoogleMapReact from "google-map-react";

const LocationMap = (props) => {
    const apiIsLoaded = (map, maps) => {
        if (map) {
            map.setOptions({ gestureHandling: 'greedy', mapTypeControl: false, minZoom: 2});
        }
    };
    const { address } = props;
    const center = address ? { lat: address.lat, lng: address.lon } : { lat: 0, lng: 0 };
    return (
        <div className="google-maps-wrapper">
            <GoogleMapReact
                bootstrapURLKeys={{
                    key: process.env.REACT_APP_MAP_KEY
                }}
                defaultZoom={address ? 8 : 2}
                center={[center.lat, center.lng]}
                defaultCenter={[0, 0]}
                yesIWantToUseGoogleMapApiInternals={true}
                onGoogleApiLoaded={({ map, maps }) => apiIsLoaded(map, maps)}>
            </GoogleMapReact>
        </div>
    );
};

export default LocationMap;
4

1 回答 1

0

检查您的方法是否被 GoogleMapReact 调用

const apiIsLoaded = (map, maps) => {
      console.log('apiIsLoaded')
        if (map) {
            map.setOptions({ gestureHandling: 'greedy', mapTypeControl: false, minZoom: 2});
        }
    };

第二件事是你不需要通过是

yesIWantToUseGoogleMapApiInternals={true} 

将其更改为

 yesIWantToUseGoogleMapApiInternals
于 2020-01-20T11:05:54.173 回答