1

我使用传单在地图上显示具有多个多边形的地理区域,并且我想在单击每个图层时将其链接到网页。例如,链接 (http://wikipedia.com) 应替换“alert("You clicked the map at " + e.latlng);"

    var map = L.map('map').setView([45.7676067, 4.8351733], 6);

    var cloudmade = L.tileLayer('http://{s}.tile.cloudmade.com/{key}/{styleId}/256/{z}/{x}/{y}.png', {
        attribution: 'Map data © 2011 OpenStreetMap, Imagery © 2012 CloudMade',
        key: 'My Key',
        styleId: 22677
    }).addTo(map);


    // control that shows state info on hover
    var info = L.control();

    info.onAdd = function (map) {
        this._div = L.DomUtil.create('div', 'info');
        this.update();
        return this._div;
    };

    info.update = function (props) {
        this._div.innerHTML = '<h4>Région</h4>' +  (props ?
            '<b>' + props.Name + '</b><br />'
            : 'Survollez une région');
    };

    info.addTo(map);


    // get color depending on population density value
    function getColor(d) {
        return d > 1000 ? '#800026' :
               d > 500  ? '#BD0026' :
               d > 200  ? '#E31A1C' :
               d > 100  ? '#FC4E2A' :
               d > 50   ? '#FD8D3C' :
               d > 20   ? '#FEB24C' :
               d > 10   ? '#FED976' :
                          '#FFEDA0';
    }

    function style(feature) {
        return {
            weight: 2,
            opacity: 1,
            color: 'white',
            dashArray: '3',
            fillOpacity: 0.7,
            fillColor: getColor('10')
        };
    }

    function highlightFeature(e) {
        var layer = e.target;

        layer.setStyle({
            weight: 3,
            color: '#666',
            dashArray: '',
            fillOpacity: 0.7,
            fillColor: getColor('50')
        });

        if (!L.Browser.ie && !L.Browser.opera) {
            layer.bringToFront();
        }

        info.update(layer.feature.properties);
    }

    var geojson;

    function resetHighlight(e) {
        geojson.resetStyle(e.target);
        info.update();
    }

    function zoomToFeature(e) {
        map.fitBounds(e.target.getBounds());
    }

    function onMapClick(e) {
        alert("You clicked the map at " + e.latlng);
    }

    map.on('click', onMapClick);

    function onEachFeature(feature, layer) {
        layer.on({
            mouseover: highlightFeature,
            mouseout: resetHighlight,
            //click: zoomToFeature
            click: onMapClick
        });
    }

    geojson = L.geoJson(regions, {
        style: style,
        onEachFeature: onEachFeature
    }).addTo(map);


</script>

谢谢你的帮助

4

1 回答 1

2

您应该能够在 onMapClick 函数中使用简单的窗口重定向:

window.location = "http://www.google.com"

或在新选项卡或窗口中打开使用 window.open

于 2012-09-25T14:37:56.383 回答