如何使用Reaction-Google-Maps显示多个标记
我正在尝试使用名为Reaction-Google-Maps的NPM包显示多个标记,但我遇到了困难。我在这里跟随演示,了解如何显示一个地图标记(https://tomchentw.github.io/react-google-maps/events/simple-click-event),但是,当我将第二个标记元素添加到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>
);
}
}
解决方案
若要显示多个标记,您只需在数组中迭代标记位置即可。
{marks.map((mark, index) => <Marker key={index} position={mark} />)}
这里有一个具有更多功能的解决方案。每当用户单击该代码段时,它都会在地图上添加多个圆圈。
import React, { Component } from "react";
import { withScriptjs, withGoogleMap, GoogleMap, Circle } from "react-google-maps";
const Map = withScriptjs(
withGoogleMap(props => (
<GoogleMap
defaultZoom={12}
defaultCenter={{ lat: -34.397, lng: 150.644 }}
onClick={e => props.onMapClick(e)}
>
{props.marks.map((mark, index) => (
<Circle
key={index}
center={mark}
radius={1000}
options={{
strokeColor: "#66009a",
strokeOpacity: 0.8,
strokeWeight: 2,
fillColor: `#66009a`,
fillOpacity: 0.35,
zIndex: 1
}}
/>
))}
</GoogleMap>
))
);
class ReportsPage extends Component {
state = {
marks: []
};
setMark = e => {
this.setState({ marks: [...this.state.marks, e.latLng] });
};
deleteMarkS = () => {
this.setState({
marks: []
});
};
render() {
const { marks } = this.state;
return (
<div>
<button onClick={this.deleteMark}>DELETE MARKS</button>
<Map
googleMapURL="http://maps.googleapis.com/maps/api/js?key=[YOUR GOOGLE MAPS KEY]"
loadingElement={<div style={{ height: `100%` }} />}
containerElement={<div style={{ height: `400px` }} />}
mapElement={<div style={{ height: `100%` }} />}
onMapClick={this.setMark}
marks={marks}
/>;
</div>
);
}
}
export default ReportsPage;
不要忘记为您的Real Key更改[您的Google地图密钥]。
相关文章