locked
Bing Maps V8 LocationRect fromLocations RRS feed

  • Question

  • Just a suggestion, but it would be nice if LocationRect could have an additional 'fromRings', or 'fromPolygon' function, as passing the locations from a polygon using the following...

    map.setView({bounds: Microsoft.Maps.LocationRect.fromLocations(myPolygon.getLocations()});

    only takes the locations from the first ring of the polygon. I have to write a fair bit of code to get the bounding box of an entire polygon. I appreciate that this is probably a hangover from V7, before complex polygons were supported, but given the improvements I think this has been overlooked.

    I've written the following, but if there's a better way of doing it, any advice would be appreciated.

    var rings = myPolygon.getRings()
    var n, e, s, w;
    for (var i = 0, len = rings.length; i < len; i++) {
        var br = Microsoft.Maps.LocationRect.fromLocations(rings[i]);
        if (!n || br.getNorth() > n) n = br.getNorth();
        if (!e || br.getEast() > e) e = br.getEast();
        if (!s || br.getSouth() < s) s = br.getSouth();
        if (!w || br.getWest() < w) w = br.getWest();
    }
    map.setView({ bounds: Microsoft.Maps.LocationRect.fromEdges(n, w, s, e) });


    Wednesday, September 14, 2016 3:20 PM

Answers

  • In Bing Maps V8, the spatial math library provides a function that makes it easy to generate a LocationRect from an array of shapes. Here is a code sample:

    //Load the spatial math library eary in your code.
    Microsoft.Maps.loadModule("Microsoft.Maps.SpatialMath");
    
    //Later on when you have an array of shapes, you can generate a LocationRect like this:
    
    var shapes = [...];
    var locationRect = Microsoft.Maps.SpatialMath.Geometry.bounds(shapes);
    Full documentation can be found here: https://msdn.microsoft.com/en-us/library/mt762861.aspx


    [Blog] [twitter] [LinkedIn]

    • Proposed as answer by Ricky_Brundritt Wednesday, September 14, 2016 6:01 PM
    • Marked as answer by gwenci Wednesday, September 14, 2016 6:54 PM
    Wednesday, September 14, 2016 6:01 PM