【问题标题】:Getting errors with Google Maps API, using recompose and react-google-maps使用 recompose 和 react-google-maps 获取 Google Maps API 错误
【发布时间】:2017-12-19 20:31:16
【问题描述】:

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

这是我的 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 问题、Google 和 StackOverflow 上寻找解决方案,但还没有找到解决方案。

在更改(拖动)地图上的位置时如何解决错误?

【问题讨论】:

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


    【解决方案1】:

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

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

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

    【讨论】:

      猜你喜欢
      • 2022-10-24
      • 2016-08-02
      • 2019-02-04
      • 2018-02-19
      • 2020-09-07
      • 1970-01-01
      • 2020-10-07
      • 2018-12-27
      • 1970-01-01
      相关资源
      最近更新 更多