Pan function (or setview)?

I want to create a panTo function. When I click the link, the map moves to the coordinates. But I'm not sure how to pass the value of the function. I start with a reference to the Pointfield (pt) value as follows:

<a href="#" class="marker" value="{{ mymodel.pt }}">Link</a>

Then I tried this:

$("#dialog").on("click", ".marker", function(e) {
    e.preventDefault();
    map.panTo($(this).attr("value"));
    });

This did not work. I think this is a problem with the area in which the can not function reads the variable "map", because it is not under the initial map script.

So my next idea is to create a β€œpanTo” function and place it under the built-in script map (which will be a suitable area) and call this function from the click event. Not sure if this will work, but I wonder how to pass the β€œvalue” from the link to it?

Thank you for your responses!

+3
1

, data HTML. , data-position, JavaScript, . .

<div id="map">
    <div id="map-navigation" class="map-navigation">
        <a href="#" data-zoom="12" data-position="37.7733,-122.4367">
            San Francisco
        </a>
    </div>
</div>

<script>
var map = L.map('map').setView([51.505, -0.09], 13);

L.tileLayer('http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/997/256/{z}/{x}/{y}.png', {
                maxZoom: 18,
                attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery Β© <a href="http://cloudmade.com">CloudMade</a>'
            }).addTo(map);

document.getElementById('map-navigation').onclick = function(e) {
    var pos = e.target.getAttribute('data-position');
    var zoom = e.target.getAttribute('data-zoom');
    if (pos && zoom) {
        var loc = pos.split(',');
        var z = parseInt(zoom);
        map.setView(loc, z, {animation: true});
        return false;
    }
}
</script>
+5

All Articles