Windows Phone 8 MapView Control :How To Get Map Bounds(Top-Left and Bottom-Right) GeoCoordinate's(C#-XAML)

Windows Phone 8 MapView Control :How To Get Map Bounds(Top-Left and Bottom-Right) GeoCoordinate's(C#-XAML)

Advertisemen

Introduction

In Prev post i was described about MapView Custom Pushpin.And 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
Source File : BoundingRectangleWp8Sample

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
scriptcode" style="background-color: white; line-height: 15px; padding: 8px; position: relative;">
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






Note: Please share your thoughts,what you think about this post,Is this post really helpful for you?otherwise it would be very happy ,if you have any thoughts for to implement this requirement in any another way?I always welcome if you drop comments on this post and it would be impressive.

Follow me always at  

Have a nice day by  :)

Advertisemen

Disclaimer: Gambar, artikel ataupun video yang ada di web ini terkadang berasal dari berbagai sumber media lain. Hak Cipta sepenuhnya dipegang oleh sumber tersebut. Jika ada masalah terkait hal ini, Anda dapat menghubungi kami disini.

Tidak ada komentar:

Posting Komentar

© Copyright 2017 Tutorial Unity 3D