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
1)Which event is best for BoundingRectangle
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"/>
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(0, 0));
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.
When MapElements are added to map ResolveCompleted is fired repeatedly and app crashes.Any solution for this ?
ReplyDeleteHi,
DeleteI just replied to your question at
http://social.msdn.microsoft.com/Forums/en-US/628190cf-ffbd-4340-91ac-05e84f0628df/why-resolvecompleted-event-is-firing-when-a-mapelements-is-added-to-map-control-?forum=wpdevelop
hey Raju, I am currently doing research topic of Shortest Path Algorithm. I have one question related it, in Windows Phone: bing maps.
ReplyDeleteIs it true ? Window Phone: Bing maps using djikstra algorithm precisely (Customizable Route Planning) for routing planning ?
How it work ?
hey Raju, I am trying to do something similar with the new UWP map control in a XAML app. However there is no ResolveCompleted event and nothing which looks similar. Any Ideas?
ReplyDelete