[GIS] Zoom to fit pushpins in Bing Maps

bing-maps

Given a bunch of pushpins in Bing Maps, is there a quick and easy way to zoom in on those pushpins such that they are all visible, and fairly spread out? For example, say I have hundreds of pins all over Australia. Then I would want to zoom in to Australia as a continent, but seeing other continents would be too big, yet seeing only Sydney would be too small.

Best Answer

The solution to this is to set the bounds property of the viewOptions object.

The bounds property takes a locationRect. Since I knew the maximum and minimum longitudes and latitudes, I used the fromEdges constructor. There are other ways of making a locationRect, go here for more information on how to make a locationRect.

Once you have the locationRect, simply set the bounds property either at the initial creation of the map, or later using the setView method for the map class. Here is my code:

box=new Microsoft.Maps.LocationRect.fromEdges(maxLat,minLong,minLat,maxLong); map=new Microsoft.Maps.Map(document.getElementById("mapDiv"), { credentials:"Your bing map developer key", bounds: box });

Related Question