问题
I'm working on a project which uses react-google-maps
(https://github.com/tomchentw/react-google-maps) library.
It works well, but we stumbled upon a problem with overlapping markers. Ideal solution to our case would be to use this plugin: https://github.com/jawj/OverlappingMarkerSpiderfier.
Is there any way to use it with react-google-maps
?
回答1:
How to OverlappingMarkerSpiderfier plugin integrate into react-google-maps
1)install npm-overlapping-marker-spiderfier
package: npm i npm-overlapping-marker-spiderfier
2) once installed the following component demonstrates how to create the instance of OverlappingMarkerSpiderfier class and pass markers:
import React from "react";
import PropTypes from "prop-types";
import { MAP, MARKER } from "react-google-maps/lib/constants";
class Spiderfy extends React.Component {
static contextTypes = {
[MAP]: PropTypes.object
};
constructor(props, context) {
super(props, context);
const oms = require(`npm-overlapping-marker-spiderfier/lib/oms.min`)
this.oms = new oms.OverlappingMarkerSpiderfier(this.context[MAP], {});
this.markerNodeMounted = this.markerNodeMounted.bind(this);
}
markerNodeMounted(ref) {
const marker = ref.state[MARKER];
this.oms.addMarker(marker);
window.google.maps.event.addListener(marker, "spider_click", (e) => {
if (this.props.onSpiderClick) this.props.onSpiderClick(e);
});
}
render() {
return React.Children.map(this.props.children, child =>
React.cloneElement(child, { ref: this.markerNodeMounted })
);
}
}
Usage
<GoogleMap
defaultZoom={zoom}
defaultCenter={{
lat: center.lat,
lng: center.lng
}}
>
<Spiderfy>
<Marker
position={{
lat: center.lat,
lng: center.lng
}}
/>
<Marker
position={{ lat: -37.9716929, lng: 144.772958 }}
title="Melbourne"
/>
<Marker
position={{ lat: -38.132245, lng: 144.2994245 }}
title="Geelong"
/>
<Marker
position={{ lat: -38.2277575, lng: 145.0447435 }}
title="Mornington"
/>
</Spiderfy>
</GoogleMap>
Here is a demo for your reference
来源:https://stackoverflow.com/questions/54555877/can-i-use-overlappingmarkerspiderfier-with-react-google-maps