Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Can I zoom the map to the extent of a GeoJSON layer? #35

Closed
palewire opened this issue Nov 22, 2010 · 3 comments
Closed

Can I zoom the map to the extent of a GeoJSON layer? #35

palewire opened this issue Nov 22, 2010 · 3 comments

Comments

@palewire
Copy link

So, i've created my map

    var po = org.polymaps;
    
    map = po.map()
        .container(document.getElementById("map-canvas").appendChild(po.svg("svg")))
        .zoomRange([1, 6])
        .zoom(4)
        .add(po.interact());
    map.add(po.image().url("http://s3.amazonaws.com/com.modestmaps.bluemarble/{Z}-r{Y}-c{X}.jpg"));

And I've added a JSON layer:


    data = po.geoJson().url("/json/?slug=foo").tile(false);
    map.add(data);

How can I get the map to zoom to the extent of that layer?

@RandomEtc
Copy link
Contributor

I'm not sure if any internal code already calculates the extent of the layer, so you probably need to loop over the features and calculate that yourself.

Once you have the min and max lat/lon of your features you can call map.extent(newExtent) to change the visible area. http://polymaps.org/docs/map.html#extent

@palewire
Copy link
Author

Thanks. I think I got it by adding a load function my json that links to:

Array.max = function( array ){
    return Math.max.apply( Math, array );
};
Array.min = function( array ){
    return Math.min.apply( Math, array );
};

function load(e) {
  var x = Array();
  var y = Array();
  for (var i = 0; i < e.features.length; i++) {
    var feature = e.features[i];
    var coords = feature.data.geometry.coordinates;
    var polygons = $.map(coords, function(a){return a});
    var points = $.map(polygons, function(a){return a});
    $.map(points, function(a){
        if(!isNaN(a[0])){x.push(a[0])}; 
        if(!isNaN(a[1])){y.push(a[1])};
    });
  }
  xMax = Array.max(x);
  xMin = Array.min(x);
  yMax = Array.max(y);
  yMin = Array.min(y);
  map.extent([{lon:xMin,lat:yMin},{lon:xMax,lat:yMax}])
}

@drewda
Copy link

drewda commented Mar 16, 2011

Just for future reference here's a relevant bit of code: https://github.com/mbostock/polymaps/blob/master/examples/bounds/bounds.js

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants