Answered LocationRect From Two Locations

  • Tuesday, April 24, 2012 9:59 PM
     
      Has Code

    I need center a route in map.

    The problem is: LocationRect does not support two Locations as Arguments.

    Dim rect as new LocationRect
    
    Map.SetView(rect)

    In this case, how to calculate Width and Heigth in LocationRect?

    Tanks


    Luis C. Pignataro Sisted

All Replies

  • Wednesday, April 25, 2012 7:33 PM
     
     Answered

    Hi Luis,

    Below is some code that should assist.

    Jim

    ...
    Bing.Maps.LocationCollection locations = new Bing.Maps.LocationCollection();
    locations.Add(new Bing.Maps.Location(43.0, -125.0));
    locations.Add(new Bing.Maps.Location(43.0, -122.0));
    Bing.Maps.LocationRect boundingRect = GetLocationsRect(locations);
    MyMap.SetView(boundingRect);
    ...


    private Bing.Maps.LocationRect GetLocationsRect(Bing.Maps.LocationCollection locations)
    {
        Bing.Maps.LocationRect boundingRect = new Bing.Maps.LocationRect();

        if (locations.Count == 0)
        {
            // nothing to do
        }
        else if (locations.Count == 1)
        {
            boundingRect.Center = locations[0];
        }
        else
        {
            double minLatitude =  locations[0].Latitude;
            double minLongitude = locations[0].Longitude;
            double maxLatitude =  locations[0].Latitude;
            double maxLongitude = locations[0].Longitude;

            foreach (Bing.Maps.Location loc in locations)
            {
                if (loc.Latitude < minLatitude)
                {
                    minLatitude = loc.Latitude;
                }
                else if(loc.Latitude > maxLatitude)
                {
                    maxLatitude = loc.Latitude;
                }

                if(loc.Longitude < minLongitude)
                {
                    minLongitude = loc.Longitude;
                }
                else if(loc.Longitude > maxLongitude)
                {
                    maxLongitude = loc.Longitude;
                }
            }

            double width = maxLongitude - minLongitude;
            double height = maxLatitude - minLatitude;
            Bing.Maps.Location center =
                new Bing.Maps.Location(0.5 * (maxLatitude + minLatitude), 0.5 * (maxLongitude + minLongitude));

            boundingRect.Center = center;
            boundingRect.Width = width;
            boundingRect.Height = height;
        }

        return boundingRect;
    }