Adding Controls Over Map In Windows Phone 8.1

In Windows Phone 8 or 8.1 we can put icon/pin to show the location at any coordinate. Adding MapIcon object will do for that. But sometimes we have to show more details about that location or coordinate at that time adding grid, text block helps to show more data in map. Putting controls over map is easy if we do not have to show those controls on coordinate (lat/long) basis. But if we have to show those controls in exact location, then we have to do more exercise.

In map control you can add any object that inherits from DependencyObject to the children collection. You can add location (coordinate) and position using SetLocation and SetNomalizedAnchorPoint properties. So here is the code:

  1. protected override async void OnNavigatedTo(NavigationEventArgs e)  
  2. {  
  3.      var MyPosition = new Geopoint(new BasicGeoposition() { Latitude = 27.6838761, Longitude = 85.33530139 });  
  4.      var MyCustomPin = createPin();  
  5.      MyMap.Children.Add(MyCustomPin);  
  6.      MapControl.SetLocation(MyCustomPin, MyPosition);  
  7.      MapControl.SetNormalizedAnchorPoint(MyCustomPin, new Point(0.0, 1.0));  
  8.      await MyMap.TrySetViewAsync(MyPosition, 15, 0, 0, MapAnimationKind.Bow);  
  9. }  
  10.    
  11. private DependencyObject createPin()  
  12. {  
  13.       //Creating a Grid element.  
  14.       var myGrid = new Grid();  
  15.       myGrid.Height = 140;  
  16.       myGrid.Width = 210;  
  17.       myGrid.Background = new ImageBrush() { ImageSource = new BitmapImage(new Uri("ms-appx:///Assets/box.png")) };  
  18.    
  19.       //adding stackpanel  
  20.       var mystack = new StackPanel();  
  21.       var img = new Image();  
  22.       img.Height = 25;  
  23.       img.Width = 35;  
  24.       img.Stretch = Stretch.UniformToFill;  
  25.       img.Source = new BitmapImage(new Uri("ms-appx:///Assets/plane1.png"));  
  26.       img.HorizontalAlignment = HorizontalAlignment.Center;  
  27.       mystack.Children.Add(img);  
  28.    
  29.    
  30.       var flightcs = new TextBlock()  
  31.       {  
  32.           FontSize = 15,  
  33.           Foreground = new SolidColorBrush(Colors.Red),  
  34.           HorizontalAlignment = HorizontalAlignment.Center,  
  35.           Text = "G9-345"  
  36.       };  
  37.       mystack.Children.Add(flightcs);  
  38.    
  39.    
  40.       var ngrid = new Grid();  
  41.       ngrid.Margin = new Thickness(5, 0, 5, 0);  
  42.       ngrid.RowDefinitions.Add(new RowDefinition());  
  43.       ngrid.RowDefinitions.Add(new RowDefinition());  
  44.       ngrid.ColumnDefinitions.Add(new ColumnDefinition());  
  45.       ngrid.ColumnDefinitions.Add(new ColumnDefinition());  
  46.    
  47.       var alttb = new TextBlock() { FontSize = 15, Foreground = new SolidColorBrush(Colors.Black), Text = "Altitude" };  
  48.       alttb.SetValue(Grid.RowProperty, 0);  
  49.       alttb.SetValue(Grid.ColumnProperty, 0);  
  50.       ngrid.Children.Add(alttb);  
  51.    
  52.       var alttb1 = new TextBlock() { FontSize = 15, Foreground = new SolidColorBrush(Colors.Black), Text="15000 ft" };  
  53.       alttb1.SetValue(Grid.RowProperty, 0);  
  54.       alttb1.SetValue(Grid.ColumnProperty, 1);  
  55.       ngrid.Children.Add(alttb1);  
  56.    
  57.       var sptb = new TextBlock() { FontSize = 15, Foreground = new SolidColorBrush(Colors.Black), Text = "Speed" };  
  58.       sptb.SetValue(Grid.RowProperty, 1);  
  59.       sptb.SetValue(Grid.ColumnProperty, 0);  
  60.       ngrid.Children.Add(sptb);  
  61.    
  62.       var sptb1 = new TextBlock() { FontSize = 15, Foreground = new SolidColorBrush(Colors.Black), Text="500 Knt" };  
  63.       sptb1.SetValue(Grid.RowProperty, 1);  
  64.       sptb1.SetValue(Grid.ColumnProperty, 1);  
  65.       ngrid.Children.Add(sptb1);  
  66.    
  67.       mystack.Children.Add(ngrid);  
  68.       var nstack = new StackPanel() { Orientation = Orientation.Horizontal, Margin = new Thickness(5, 5, 5, 0) };  
  69.       var ntb = new TextBlock() { FontSize = 15, Foreground = new SolidColorBrush(Colors.Black), Text = "2017-07-07" };  
  70.       nstack.Children.Add(ntb);  
  71.    
  72.       var ntb1 = new TextBlock() { FontSize = 15, Foreground = new SolidColorBrush(Colors.Black), Margin = new Thickness(20, 0, 0, 0), Text = "9:00:00 AM" };  
  73.       nstack.Children.Add(ntb1);  
  74.    
  75.       mystack.Children.Add(nstack);  
  76.       myGrid.Children.Add(mystack);  
  77.       return myGrid;  
  78. }  
So here is the code you create pin with having some grid and TextBlock and returns it in the form of DependecyObject type object. And others are basic parts that you did in map.

Here is how it looks:

map

Above image is the small part of my project where I have shown the details of particular plane in map dynamically (because of changing lat/long). Writing controls in Xaml and adding over map is not possible because the controls that we write in Xaml is already added in the children collection of Page Control which holds maps control too. So as mentioned above the writing controls in C# (Grid, Rectangle whatever), wrapping it as DependencyObject type and passing it to the children collection of map control will help to achieve our goal.

Tip: Do not forgot to check location capabilities in AppxManifest file. Full source code is here.

Happy Coding.

 


Similar Articles