map.addLayer(markerClusters)不是函数

时间:2017-05-16 07:13:48

标签: reactjs react-native leaflet leaflet.markercluster

我收到此错误:

  

map.addLayer(markerClusters)不是函数

用于我的markerClusters,因为我更新了以下包:

"leaflet": "^0.7.7",
"leaflet.markercluster": "^0.5.0",
"react-leaflet": "^0.12.3",
"react-leaflet-cluster-layer": "0.0.3",
"react-leaflet-control": "^1.1.0",

"leaflet": "^0.7.7",
"leaflet.markercluster": "^1.0.3",
"react-leaflet": "^1.1.6",
"react-leaflet-control": "^1.4.0",
"react-leaflet-cluster-layer": "0.0.3",

我需要在我的页面上为另一个地图进行此更新,因此我不想使用旧版本。有谁知道如何解决这个问题。

我刚刚发现,在SO上,地图不是全局变量,但正如其定义的那样,它是可用的。谷歌搜索还没有带来任何东西,这是有用的。

提前致谢!

这是我的MarkerClusters.js文件:

import { Component, PropTypes } from 'react';
import Leaflet from 'leaflet';
import 'leaflet.markercluster';
import styles from './MarkerCluster.scss';
import markerDefaultIcon from '../../../assets/images/marker_default.png';

export default class MarkerCluster extends Component {

    static propTypes = {
        map: PropTypes.object.isRequired,
        features: PropTypes.array
    }

    componentDidMount() {
        this.createMarkerClusters(this.props);      
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.features !== this.props.features) {
            this.createMarkerClusters(nextProps);
        }
    }

    createMarkerClusters = (props = this.props) => {
        const { map, features } = props; 
        if (!features || !features.length) {
            return null;
        }
        const markerClusters = Leaflet.markerClusterGroup({
            disableClusteringAtZoom: 14,
            maxClusterRadius: 110,
            polygonOptions: {
                color: '#fff'
            },
            iconCreateFunction(cluster) {
                const count = cluster.getChildCount();
                let clusterSizeClassName = styles.small_cluster;
                if (count > 80) {
                    clusterSizeClassName = styles.large_cluster;
                } else if (count > 15) {
                    clusterSizeClassName = styles.mid_cluster;
                }
                return Leaflet.divIcon({ 
                    iconSize: [45, 45],
                    className: `${styles.marker_cluster} ${clusterSizeClassName}`,
                    html: `<b class="${styles.cluster_inner}"> ${count} </b>`
                });
            }
        });   
        const icon = Leaflet.divIcon({
            className: '',
            iconSize : [48, 64],
            iconAnchor: [9, 63],
            html: `<img class="${styles.marker}" src='${markerDefaultIcon}'/>`
        });
        features.forEach(feature => {
            const { geometry, properties } = feature;
            const title = properties.name;
            const { coordinates: [lng, lat] } = geometry;
            const marker = Leaflet.marker(new Leaflet.LatLng(lat, lng), { title, icon });
            marker.bindPopup(this.getPopupTemplate(properties));
            markerClusters.addLayer(marker);
        });
        map.addLayer(markerClusters);
        window.setTimeout(() => {            
            if (this.markerClusters) {
                map.removeLayer(this.markerClusters); 
            }
            this.markerClusters = markerClusters; 
        }, 300);
    }

    getPopupTemplate = props => {
        const createImageHolder = image => {
            if (!image) {
                return '';
            }
            return (
                `<div class="${styles.image}">
                    <img src="${props.image.medium}" />
                </div>`
            );
        }
        return (
            `<div class="${styles.popup}">
                <a 
                    class="${styles.link}"
                    href="/d/${props.id}"
                >
                    <div class="${styles.title}">${props.name}</div>
                    ${createImageHolder(props.image)}
                    Buy
                </a>
            </div>`
        )
    }

    render() {
        return null;
    }

}

堆栈跟踪:

  

未捕获(承诺)TypeError:map.addLayer不是函数       在MarkerCluster._this.createMarkerClusters(MarkerCluster.js:64)       在MarkerCluster.componentWillReceiveProps(MarkerCluster.js:20)       在ReactCompositeComponent.js:611       at measureLifeCyclePerf(ReactCompositeComponent.js:75)       在ReactCompositeComponentWrapper.updateComponent(ReactCompositeComponent.js:610)       在ReactCompositeComponentWrapper.receiveComponent(ReactCompositeComponent.js:547)       at Object.receiveComponent(ReactReconciler.js:125)       在Object.updateChildren(ReactChildReconciler.js:109)       在ReactDOMComponent._reconcilerUpdateChildren(ReactMultiChild.js:208)       在ReactDOMComponent._updateChildren(ReactMultiChild.js:312)

1 个答案:

答案 0 :(得分:1)

所以我们发现了错误:

我们在地图中调用了MarkerCluster对象:

                    <MarkerCluster
                        map={ this.refs.map }
                        features={ features } />

因为this.refs.map是react-leaflet Map Object,它没有addLayer()函数; 此函数仅在原始的leafletElement中。

所以我们使用

map.leafletElement.addLayer();

而不是

map.addLayer();