使用重构和反应谷歌地图获取Google Maps API错误

问题描述 投票:1回答:1

我正在使用Google Maps API在地图上显示地点。但是,每次我在地图上拖动位置时,都会出现以下错误:

errors on console

这是我的Google地图,使用Google Maps API,recomposereact-google-maps。它有效,但是给出了错误。

/*global google*/
import React from "react"
import { compose, withProps, withHandlers, withState } from "recompose"
import { withScriptjs, withGoogleMap, GoogleMap, Marker } from "react-google-maps"

const MyMapComponent = compose(
    withProps({
        googleMapURL: "https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places",
        loadingElement: <div style={{ height: `100%` }} />,
        containerElement: <div style={{ height: `400px` }} />,
        mapElement: <div style={{ height: `100%` }} />,
    }),
    withScriptjs,
    withGoogleMap,
    withState('places', 'updatePlaces', ''),
    withHandlers(() => {
        const refs = {
            map: undefined,
        }

        return {
            onMapMounted: () => ref => {
                refs.map = ref
            },
            fetchPlaces: ({ updatePlaces }) => {
                let places;
                const bounds = refs.map.getBounds();
                const service = new google.maps.places.PlacesService(refs.map.context.__SECRET_MAP_DO_NOT_USE_OR_YOU_WILL_BE_FIRED);
                const request = {
                    bounds: bounds,
                    type: ['hotel']
                };
                service.nearbySearch(request, (results, status) => {
                    if (status == google.maps.places.PlacesServiceStatus.OK) {
                        console.log(results);
                        updatePlaces(results);
                    }
                })
            }
        }
    }),
)((props) => {
    return (
        <GoogleMap
            onTilesLoaded={props.fetchPlaces}
            ref={props.onMapMounted}
            onBoundsChanged={props.fetchPlaces}
            defaultZoom={8}
            defaultCenter={{ lat: 51.508530, lng: -0.076132 }}
        >
            {props.places && props.places.map((place, i) =>
                <Marker key={i} position={{ lat: place.geometry.location.lat(), lng: place.geometry.location.lng() }} />
            )}
        </GoogleMap>
    )
})

export default class MyFancyComponent extends React.PureComponent {
    render() {
        return (
            <MyMapComponent />
        )
    }
}

我试图在Github问题上寻找解决方案,谷歌和StackOverflow,但还没找到解决方案。

在更改(拖动)地图上的位置时,如何修复错误?

google map with markers

javascript reactjs google-maps google-maps-api-3 react-google-maps
1个回答
1
投票

withHandlers期望fetchPlaces函数是一个高阶函数,所以修改:

fetchPlaces: ({ updatePlaces }) => {
   //...          
}

fetchPlaces: ({ updatePlaces })  => () => {
   //...
}
© www.soinside.com 2019 - 2024. All rights reserved.