gpt4 book ai didi

javascript - 如何使用 react-google-maps 显示多个标记

转载 作者:行者123 更新时间:2023-11-29 10:31:58 25 4
gpt4 key购买 nike

我正在尝试使用名为 react-google-maps 的 npm 包显示多个标记,但遇到了困难。我在这里关注如何显示一个 map 标记 ( 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} />)}

这是一个具有更多功能的解决方案。每当用户点击时,代码片段都会在 map 上添加多个圆圈。

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;

不要忘记为您的真实 key 更改 [YOUR GOOGLE MAPS KEY]。

关于javascript - 如何使用 react-google-maps 显示多个标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43859785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com