Mohammed ESSADDEK Mohammed ESSADDEK - 1 year ago 79
Javascript Question

Get the best Map View based on all polygons in Bing Maps Javascript

I have a Bing Maps where i draw some polygons,
I would like to get the best view (zoom / center) to show all the polygons.

I've tried this, and that achieve what i want, but it make a lot of time.

var poly = new Microsoft.Maps.Polygon(allPoints);
var boundaries = Microsoft.Maps.LocationRect.fromLocations(poly.getLocations());

Thanks for help.

Answer Source

If you have a lot of points (tens of thousands) it will take time. Another way to do this is to loop through your array of location objects and get the min and max latitude and longitude values and then use those to create a location rect. However I doubt that would be much faster as that is basically what the fromLocations method does behind the scenes.

Also, rather than using getLocations, just use the allPoints value you have. Might save a bit of processing.

All that said, unless you are working with really large polygons (thousands of points), I can't see this being slow. Panning and zooming may be slow, but would likely be because the browser has to constantly update the position of the points for the polygon as the map moves. The more points your polygon has, the slower the browser will become.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download