当我尝试使用Google Maps加载组件时(正如预期的那样,props被传递给了组件),我只是得到了一条消息,提示"Loading map...",但是没有加载带有标记的地图。控制台中也没有错误。这是组件。我到底做错了什么?
import React, {Component} from 'react';
import {Map, InfoWindow, Marker, GoogleApiWrapper} from 'google-maps-react';
import { Constants } from './../../utils/constants';
export class MapContainer extends Component {
state = {
selectedPlace: ''
}
onMarkerClick = (e) => {
this.setState({selectedPlace: e.Name});
}
render() {
return (
<Map
google={this.props.google}
style={{width: '20vw', height: '45vh', 'top': '1.5rem'}}
containerStyle={{width: '20vw', height: '30vh'}}
initialCenter={{
lat: this.props.lat,
lng: this.props.lng
}}
zoom={15}>
{this.props.markers.length > 0 && // Rendering multiple markers for
this.props.markers.map((m) => {
return (<Marker
onClick={this.onMarkerClick}
name={this.state.selectedPlace}
position={{ lat: m.Latitude, lng: m.Longitude }}
key={m.Name} />);
})
}
{this.props.markers && // Rendering single marker for supplier details map
<Marker onClick={this.onMarkerClick}
name={this.state.selectedPlace} />
}
<InfoWindow onClose={this.onInfoWindowClose}>
<h4>{this.state.selectedPlace}</h4>
</InfoWindow>
</Map>
);
}
}
export default GoogleApiWrapper({
apiKey: (Constants.GoogleMapsApiKey),
language: "RU"
})(MapContainer)发布于 2019-06-27 00:22:52
在您的codesandbox演示中,您正在导出您的应用程序组件,您需要在其中呈现它。
export default GoogleApiWrapper({
apiKey: "",
language: "RU"
})(App);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);在这里,您呈现的是GoogleApiWrapper HOC没有包装的应用程序组件。不是导出打包的应用程序,而是按如下方式渲染它:
App = GoogleApiWrapper({
apiKey: "",
language: "RU"
})(App);
const rootElement = document.getElementById("root");
ReactDOM.render(<App />, rootElement);发布于 2019-06-26 00:20:57
尝试以像素为单位设置地图的宽度和高度
style={{width: '200px', height: '450px', 'top': '1.5rem'}}https://stackoverflow.com/questions/56725252
复制相似问题