【发布时间】:2018-04-05 05:38:42
【问题描述】:
当我尝试包含 geoocoder api 代码时,它显示错误“TypeError: Cannot read property 'maps' of undefined”。
这是我的代码
import React from 'react';
import { compose, withProps,withHandlers } from "recompose"
import { withScriptjs, withGoogleMap, GoogleMap, Marker,InfoWindow,GeoCoder } from "react-google-maps"
import { MarkerClusterer } from 'react-google-maps/lib/components/addons/MarkerClusterer';
import { connect } from 'react-redux';
let geocoder = new google.maps.Geocoder();
const MyMapComponent = compose(
withProps({
googleMapURL: "https://maps.googleapis.com/maps/api/js?AIzaSyAoaDS6fIKlYvEHeTaakCxXqp-UwnggoEgv=3&sensor=true&callback=init",
loadingElement: <div style={{ height: `100%` }} />,
containerElement: <div style={{ height: `400px` }} />,
mapElement: <div style={{ height: `100%` }} />,
}),
withHandlers({
onMarkerClustererClick: () => (markerClusterer) => {
const clickedMarkers = markerClusterer.getMarkers()
console.log(`Current clicked markers length: ${clickedMarkers.length}`)
console.log(clickedMarkers)
},
}),
withScriptjs,
withGoogleMap
)((props) =>{
return ( <GoogleMap
zoom={props.maps.zoom}
center={{ lat:props.maps.lat, lng:props.maps.lng }}
heading={5}
>
<MarkerClusterer
onClick={props.onMarkerClustererClick}
averageCenter
enableRetinaIcons
gridSize={60}
>
{props.markers.map(marker => (
<Marker
key={marker.photo_id}
position={{ lat: marker.latitude, lng: marker.longitude }}
/>
))}
</MarkerClusterer>
<Marker
position={{lat:props.maps.lat, lng:props.maps.lng }}
>
<InfoWindow>
<p>gdgd</p>
</InfoWindow>
</Marker>
</GoogleMap>
)});
export default class DemoApp extends React.Component {
componentWillMount() {
this.setState({ markers: [] })
}
componentDidMount() {
console.log("+mymap++++++++");
console.log(this.props.myMap);
this.setState({markers:[{photo_id:1,longitude:76.911270,latitude:11.032595},
{photo_id:2,longitude:75.806682,latitude:11.259169},
{photo_id:3,longitude:77.213780,latitude:28.617671},
{photo_id:4,longitude:78.138991,latitude:9.903245}]})
}
render() {
return (
<MyMapComponent markers={this.state.markers} maps={this.props.myMap} />
)
}
}
在上面的代码中,当我尝试创建地理编码器变量时,它会显示错误。这里我尝试使用地理编码器 api 从地图的纬度和经度位置获取位置名称。
【问题讨论】:
-
在 DemoApp 中。您可能没有将 myMap 道具传递给 DemoApp 组件。 -
当我删除它时仍然显示相同的错误
-
这不会有帮助,
maps属性如果你删除它仍然是未定义的。您需要将它传递给MyMapComponent,因为它需要一个带有lat、lng和zoom键的对象。
标签: reactjs