0
我一直在玩一個react-google-maps包(https://github.com/tomchentw/react-google-maps)。該組件成功連接到地圖API,因爲我收到了各種控制檯警告到鍵,但沒有渲染 - 並沒有錯誤消息。我錯過了明顯的東西嗎?React組件不渲染,但沒有錯誤信息
import React from 'react';
import ReactDOM from 'react-dom';
import {GoogleMap, Loader, Marker} from "react-google-maps";
import { default as ScriptjsLoader } from "react-google-maps/lib/async/ScriptjsLoader";
export default class MapComponent extends React.Component {
constructor() {
super();
this.state = {
markers: [{
position: {
lat: 25.0112183,
lng: 121.52067570000001,
},
key: `Taiwan`,
defaultAnimation: 2,
}]
}
}
/*
* This is called when you click on the map.
* Go and try click now.
*/
handleMapClick(event) {
let { markers } = this.state;
markers = update(markers, {
$push: [
{
position: event.latLng,
defaultAnimation: 2,
key: Date.now()
},
],
});
this.setState({ markers });
if (markers.length === 3) {
this.props.toast(
`Right click on the marker to remove it`,
`Also check the code!`
);
}
}
handleMarkerRightclick(index, event) {
/*
* All you modify is data, and the view is driven by data.
* This is so called data-driven-development. (And yes, it's now in
* web front end and even with google maps API.)
*/
let { markers } = this.state;
markers = update(markers, {
$splice: [
[index, 1],
],
});
this.setState({ markers });
}
renderNewBehavior() {
return (
<ScriptjsLoader
hostname={"maps.googleapis.com"}
pathname={"/maps/api/js"}
query={{libraries: `geometry,drawing,places` }}
loadingElement={
<div {...this.props} style={{ height: `100%` }}>
LOADING
</div>
}
containerElement={
<div {...this.props} style={{ height: `100%` }} />
}
googleMapElement={
<GoogleMap
ref={googleMap => {
if (!googleMap) {
return;
}
console.log(googleMap);
console.log(`Zoom: ${ googleMap.getZoom() }`);
console.log(`Center: ${ googleMap.getCenter() }`);
}}
defaultZoom={3}
defaultCenter={{ lat: -25.363882, lng: 131.044922 }}
onClick={this.handleMapClick}
>
{this.state.markers.map((marker, index) => {
return (
<Marker
{...marker}
onRightclick={this.handleMarkerRightclick.bind(this, index)}
/>
);
})}
</GoogleMap>
}
/>
);
}
render() {
return this.renderNewBehavior()
}
}
export default MapComponent
注意可以使用https://github.com/tomchentw/react-google-maps/issues/186中列出的解決方案之一修復此問題 – ElJefeJames