Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I adjust my Bing Map's View/LocationRect/Bounding Box by a small amount?

I am using this code:

photraxMap.SetView(new LocationRect(App.photosetLocationCollection));

...to zoom a map to show the List of Locations contained in photosetLocationCollection.

photraxMap is a BingMap. SetView() is a Bing Maps method, not my own custom method.

The problem is that it works too well - it shows all the markers/pushpins, but just barely - the extreme locations are "clipped" as you can see here:

enter image description here

In the screamshot, you can see that the pushpin north of San Andreas as well as the one at Columbia, at the southeast edge of the map, are partially obscured (the one at Pardee is also partially obscured, by the Map Type box, but I reckon that can't be helped).

I want them to have a little "wiggle room" as it were. This is not simply a cosmetic issue - the "outlier" pushpins are not selectable until you drag the map up or down or left or right a tad.

Is there a way to tweak the zoom level just a teensy-weensy bit (not a full zoom level higher)?

UPDATE

Based on the ideas in the answer below, I think I will try something like this:

// Adapted from Brundritt and Boonaert: http://stackoverflow.com/questions/26937358/can-i-adjust-my-bing-maps-view-locationrect-bounding-box-by-a-small-amount
// Before passing the locations to set view, call this twice, to add bogus NW and SE locations that will stretch 
// the viewable area of the map a little, like so:
// Location nwShim = GetAShimLocation(locs, true);
// Location seShim = GetAShimLocation(locs, false);
// locs.Add(nwShim); locs.Add(seShim);
public static Location GetAShimLocation(IList<Location> locations, bool IsForNorthwestCorner)
{
    const int MAP_CUSHION = 1; // Is 1 a comfortable enough cushion?
    // I don't know why the Lats are 85 instead of 90
    double maxLat = -85;
    double minLat = 85;
    double maxLon = -180;
    double minLon = 180;

    foreach (Location loc in locations)
    {
        if (loc.Latitude > maxLat)
        {
            maxLat = loc.Latitude;
        }

        if (loc.Latitude < minLat)
        {
            minLat = loc.Latitude;
        }

        if (loc.Longitude > maxLon)
        {
            maxLon = loc.Longitude;
        }

        if (loc.Longitude < minLon)
        {
            minLon = loc.Longitude;
        }
    }

    Location retLoc = new Location();
    // I'm not sure this logic/math is right - test it later
    if (IsForNorthwestCorner)
    {
        retLoc.Latitude = maxLat - MAP_CUSHION;
        retLoc.Longitude = maxLon - MAP_CUSHION;
    }
    else // SouthEast corner - stretch a little both directions
    {
        retLoc.Latitude = minLat + MAP_CUSHION;
        retLoc.Longitude = minLon + MAP_CUSHION;
    }
}
like image 355
B. Clay Shannon-B. Crow Raven Avatar asked Mar 18 '23 21:03

B. Clay Shannon-B. Crow Raven


1 Answers

You can use an array of locations used to create the Pushpins, and pass them into the fromLocations function on the Microsoft.Maps.LocationRect class. This function will return a LocationRect that encloses all the Location objects passed into it. This LocationRect can then be passed to the bounds setting property when setting the map view. Some developers may notice that this results in some pushpins being cut off at the maps edge. The reason for this is that the fromLocations function only calculates the bounding box based on the Location objects, and not on the additional area that the pushpin icons use. To accommodate this scenario, the padding setting can be used to buffer the view by a specified number of pixels. Generally setting this value to twice as large as the width/height of your pushpin icons works well. source

var locs = [array of Microsoft.Maps.Location];
var rect = Microsoft.Maps.LocationRect.fromLocations(locs);

map.setView({ bounds: rect, padding: 80 });

Et voilà ! :)

like image 148
rdhainaut Avatar answered Apr 29 '23 06:04

rdhainaut