Can I use OverlappingMarkerSpiderfier with react-google-maps?

五迷三道 提交于 2019-12-11 05:11:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!