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

function zoomToFeature(e) {

 function onEachFeature(feature, layer) {
                    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.

Thank you for any help.


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


Quick answer to your message, It seems you basically want to redirect the user right ? 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";
            // then call it on the click event :
            click: redirect

my two cents ..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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