Introduction

In sometimes we may need to get map bounds area (North,West,South,East) with latitude and longitude's ,off course its quit interesting requirement for finding current loaded mapview bounds.Because for every view action ,map bounds will be changed.So that we need to detect bounds when viewchanged.

However in this post i am going to explain about "Maps bounding rectangle in windows phone 8" or "How to get Top-Left and Bottom-Right latitude and longitude of Current Map view"

Note: Before this post ,Please read about Map Events

1)Map Events and Properties for windows phone 8

Building the Sample

Adding the map control is very easy: in the XAML you need to add a reference to theMicrosoft.Phone.Maps namespace: 

XAML
xmlns:maps=”clr-namespace:Microsoft.Phone.Maps.Controls;
assembly=Microsoft.Phone.Maps”
 After that you can insert the Map control simply by declaring it in the XAML:
XAML
<maps:Map  Name="MapVieMode" LandmarksEnabled="true"  PedestrianFeaturesEnabled="true"
 HorizontalAlignment="Left"  MinWidth="500" MinHeight="750"  VerticalAlignment="Top"
 Loaded="Map_Loaded" Background="Black"/>
Note: This sample is only working  with windows phone 8 sdk and above

Description

We can approach this requirement using ViewPort convert methods

1)Which event is best for BoundingRectangle

Step1:

Add mapview to your MainPage
XAML
 <maps:Map  Name="MapVieMode" LandmarksEnabled="true"  PedestrianFeaturesEnabled="true"
 HorizontalAlignment="Left"  MinWidth="500" MinHeight="750"  VerticalAlignment="Top"
 Loaded="Map_Loaded" Background="Black"/>

Step2:

Make use of mapview ResolveCompleted event instead of CenterChanged and ZoomLevelChanged.Because ResolveCompleted event will be fire after mapview is fully drawn.And if you are using CenterChanged/ZoomLevelChanged events ,the map center will change a lot while for a zoom both map center and zoom will change a lot.And It makes lot of calls 

XAML
<maps:Map  Name="MapVieMode" LandmarksEnabled="true"  PedestrianFeaturesEnabled="true"
 HorizontalAlignment="Left"  MinWidth="500" MinHeight="750"  VerticalAlignment="Top" 
Loaded="Map_Loaded" ResolveCompleted="MapResolveCompleted" Background="Black"/>

 2)How to get MapBounds with ViewPort(ConvertViewportPointToGeoCoordinate) 

 Step1:  

   Make call GetVisibleMapArea() Method in Map ResolveCompleted Event
C#
private void MapResolveCompleted(object sender, MapResolveCompletedEventArgs e) 
        { 
            LocationRectangle MapBoundsArea = GetVisibleMapArea(MapVieMode); 
            if (MapBoundsArea != null) 
            { 
                MessageBox.Show("MapView Bounds Area:" + "\n" + "North :" + 
MapBoundsArea.North + "\n" + "West :" + MapBoundsArea.West + "\n" + 
"South :" + MapBoundsArea.South + "\n" + "East :" + MapBoundsArea.East +
 "\n" + "SouthEast :" + MapBoundsArea.Southeast + "\n" + "NorthWest :" +
 MapBoundsArea.Northwest); 
            } 
        }
 
Step2:
 Use the viewport convert methods, like:

C#
 public LocationRectangle GetVisibleMapArea(Map mMap) 
        { 
            GeoCoordinate mCenter = mMap.Center; 
            Point pCenter = mMap.ConvertGeoCoordinateToViewportPoint(mCenter); 
            GeoCoordinate topLeft = MapVieMode.ConvertViewportPointToGeoCoordinate
(new Point(00)); 
                GeoCoordinate bottomRight = MapVieMode.
ConvertViewportPointToGeoCoordinate(new Point(MapVieMode.ActualWidth
, MapVieMode.ActualHeight)); 
 
                if (topLeft != null && bottomRight != null) 
                { 
                    Point pNW = new Point(pCenter.X - mMap.ActualWidth / 2, 
pCenter.Y - mMap.ActualHeight / 2); 
                    Point pSE = new Point(pCenter.X + mMap.ActualWidth / 2, 
pCenter.Y + mMap.ActualHeight / 2); 
                    if (pNW != null && pSE != null) 
                    { 
                        GeoCoordinate gcNW =
 mMap.ConvertViewportPointToGeoCoordinate(pNW); 
                        GeoCoordinate gcSE = 
mMap.ConvertViewportPointToGeoCoordinate(pSE); 
                        return new LocationRectangle(gcNW, gcSE); 
                    } 
                } 
                      
            return null; 
 
           
        }

 3)ScreenShot





And for source code please find at my MSDN code sample

Author: SubramanyamRaju

Blog: http://bsubramanyamraju.blogspot.in/