react-google-mapsというnpmパッケージを使用して複数のマーカーを表示しようとしていますが、苦労しています。私は1つのマップマーカー(https://tomchentw.github.io/react-google-maps/events/simple-click-event)を表示する方法についてここでデモを行っていますが、GoogleMapコンポーネントに2番目のマーカー要素を追加することがわかりましたら、そのマーカーを表示することはできません座標も変更されました)。それは本当に私を気にしています。誰かが間違っていることを指摘できれば、本当に感謝しています。react-google-mapsで複数のマーカーを表示するには
ありがとうございました。
ここでは、私がそれを手にしたようなコードです。 私が変更したのは、新しい座標で2番目のマーカーが追加されたことだけです。
あなたはGoogleMap
コンポーネントの子としてのマーカーの配列を渡すと、そのようにそれらの上にマッピングする必要があり
/* global google */
import {
default as React,
Component,
} from "react";
import withGoogleMap from './assets/withGoogleMap';
import GoogleMap from './assets/GoogleMap';
import Marker from './assets/Marker';
const SimpleClickEventExampleGoogleMap = withGoogleMap(props => (
<GoogleMap
ref={props.onMapMounted}
zoom={props.zoom}
center={props.center}
onCenterChanged={props.onCenterChanged}
>
<Marker
defaultPosition={props.center}
title="Click to zoom"
onClick={props.onMarkerClick}
/>
<Marker
defaultPosition={props.marker2center}
title="Click to zoom"
onClick={props.onMarkerClick}
/>
</GoogleMap>
));
const INITIAL_CENTER = { lat: -25.363882, lng: 131.044922 };
const MARKER1_CENTER = { lat: -25.363882, lng: 131.044922 };
const MARKER2_CENTER = { lat: -25.44, lng: 131.55 };
/*
* https://developers.google.com/maps/documentation/javascript/examples/event-simple
*
* Add <script src="https://maps.googleapis.com/maps/api/js"></script> to your HTML to provide google.maps reference
*/
export default class SimpleClickEventExample extends Component {
state = {
zoom: 4,
center: INITIAL_CENTER,
marker2center: MARKER2_CENTER
};
handleMapMounted = this.handleMapMounted.bind(this);
handleCenterChanged = this.handleCenterChanged.bind(this);
handleMarkerClick = this.handleMarkerClick.bind(this);
handleMapMounted(map) {
this._map = map;
}
handleMarkerClick() {
this.setState({
zoom: 8,
});
}
handleCenterChanged() {
const nextCenter = this._map.getCenter();
if (nextCenter.equals(new google.maps.LatLng(INITIAL_CENTER))) {
// Notice: Check nextCenter equality here,
// or it will fire center_changed event infinitely
return;
}
if (this._timeoutId) {
clearTimeout(this._timeoutId);
}
this._timeoutId = setTimeout(() => {
this.setState({ center: INITIAL_CENTER });
this._timeoutId = null;
}, 3000);
this.setState({
// Because center now is a controlled variable, we need to set it to new
// value when "center_changed". Or in the next render it will use out-dated
// state.center and reset the center of the map to the old location.
// We can never drag the map.
center: nextCenter,
});
}
componentWillUnmount() {
if (this._timeoutId) {
clearTimeout(this._timeoutId);
}
}
render() {
return (
<div className='googleMap' style={{width: "500px", height: "500px", margin: "0 auto"}}>
<SimpleClickEventExampleGoogleMap
containerElement={
<div style={{ height: `100%` }} />
}
mapElement={
<div style={{ height: `100%` }} />
}
zoom={this.state.zoom}
center={this.state.center}
onMapMounted={this.handleMapMounted}
onCenterChanged={this.handleCenterChanged}
onMarkerClick={this.handleMarkerClick}
/>
</div>
);
}
}
そのリンクは、もはや有効で –
ありがとう@ user3270407!リンクが修正されました。 –