-

   rss_rss_hh_new

 - e-mail

 

 -

 LiveInternet.ru:
: 17.03.2011
:
:
: 51

:


[ ] Google Maps API

, 01 2017 . 16:40 +
, ! google maps api React.js. , , . UX , . , , , , // , . SPA react/redux , . push , .

:



, react.js. 2 google-map-react react-google-maps. API Google maps, react.js. google-map-react - JSX , google maps api svg , , html , google-map-react .

, , . readme google-map-react , recompose , function components higher-order components. lodash . , recompose , .

google-map-react state , :
(api key )

const MAP = {
  defaultZoom: 8,
  defaultCenter: { lat: 60.814305, lng: 47.051773 },
  options: {
    maxZoom: 19,
  },
};

state = {
  mapOptions: {
    center: MAP.defaultCenter,
    zoom: MAP.defaultZoom,
  },
  clusters: [],
};

//JSX 


  {this.state.clusters.map(item => {
    if (item.numPoints === 1) {
      return (
        
      );
    }

    return (
      
    );
  })}


, this.state.clusters . supercluster

:

const TOTAL_COUNT = 200;

export const susolvkaCoords = { lat: 60.814305, lng: 47.051773 };

export const markersData = [...Array(TOTAL_COUNT)]
  .fill(0) // fill(0) for loose mode
  .map((__, index) => ({
    id: index,
    lat:
      susolvkaCoords.lat +
      0.01 *
        index *
        Math.sin(30 * Math.PI * index / 180) *
        Math.cos(50 * Math.PI * index / 180) +
      Math.sin(5 * index / 180),
    lng:
      susolvkaCoords.lng +
      0.01 *
        index *
        Math.cos(70 + 23 * Math.PI * index / 180) *
        Math.cos(50 * Math.PI * index / 180) +
      Math.sin(5 * index / 180),
  }));

/ :

handleMapChange = ({ center, zoom, bounds }) => {
  this.setState(
    {
      mapOptions: {
        center,
        zoom,
        bounds,
      },
    },
    () => {
      this.createClusters(this.props);
    }
  );
};

createClusters = props => {
  this.setState({
    clusters: this.state.mapOptions.bounds
      ? this.getClusters(props).map(({ wx, wy, numPoints, points }) => ({
          lat: wy,
          lng: wx,
          numPoints,
          id: `${numPoints}_${points[0].id}`,
          points,
        }))
      : [],
  });
};

getClusters = () => {
  const clusters = supercluster(markersData, {
    minZoom: 0,
    maxZoom: 16,
    radius: 60,
  });

  return clusters(this.state.mapOptions);
};

getClusters supercluster, . supercluster points, .


Original source: habrahabr.ru (comments, light).

https://habrahabr.ru/post/334644/

:  

: [1] []
 

:
: 

: ( )

:

  URL