2

I'm using Leaflet API and GeoJSON. I defined a function which zooms to the feature when clicked:

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




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

But, I don't need to zoom on the feature, but to go to the URL. So, when user click on polygon on the map, it takes him to some url.

Is that possible and how?

I thought I can maybe define some function which will lead to the url, and then pass it to layer.on click event, but not sure how.

2 Answers 2

3

Keep the code the same just replace the map.fitBounds(e.target.getBounds()); with window.open('http://www.google.com','_blank');

1

I am not familiar with LeafLet but perhaps you can try with a function like :

            function redirect(e)
            {
                // assuming you have a url property embeded in your feature
                var url = e.target.some_url_property; 
                // or hardcoded as...
                var url = "http://www.somelink";
                window.location.href=url;
            }
            // then call it on the click event :
            click: redirect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.