ComponentOne Maps for UWP
Maps for UWP Tutorials / Adding Map Markers with a Click / Step 2 of 4: Adding Code
In This Topic
    Step 2 of 4: Adding Code
    In This Topic

    In this step, you will add code to the MainPage.xaml.cs file.

    1. Right-click the MainPage.xaml page and select View Code from the list. The MainPage.xaml.cs should open.
    2. Add the following code to the class declaration:
      C#
      Copy Code
      C1VectorLayer vl;
      Random rnd = new Random();
      C1VectorPlacemark current = null;
      Point offset = new Point();
      

    3. Add the following code below the InitializeComponent() method, within the MainPage() constructor. This will change the map source, add a C1VectorLayer, and create random coordinates:
      C#
      Copy Code
      maps.Source = new VirtualEarthHybridSource();
                  vl = new C1VectorLayer();
                  maps.Layers.Add(vl);
                  maps.RightTapped += maps_RightTapped;
      
                  for (int i = 0; i < 10; i++)
                  {
                      // create random coordinates
                      Point pt = new Point(-80 + rnd.Next(160), -80 + rnd.Next(160));
                      AddMark(pt);
                  }   
      

    4. The code that follows should be inserted after the closing curly bracket of the MainPage() constructor. This code will add a RightTapped event handler and an AddMark() method:
      C#
      Copy Code
      void maps_RightTapped(object sender, RightTappedRoutedEventArgs e)
              {
                  AddMark(maps.ScreenToGeographic(e.GetPosition(maps)));
              }
      
              void AddMark(Point pt)
              {
                  Color clr = Utils.GetRandomColor(128, 192);
                  C1VectorPlacemark mark = new C1VectorPlacemark()
                  {
                      GeoPoint = pt,
                      Label = new TextBlock()
                      {
                          RenderTransform = new TranslateTransform() { Y = -5 },
                          IsHitTestVisible = false,
                          Text = (vl.Children.Count + 1).ToString()
                      },
                      LabelPosition = LabelPosition.Top,
                      Geometry = Utils.CreateBaloon(),
                      Stroke = new SolidColorBrush(Colors.DarkGray),
                      Fill = new SolidColorBrush(clr),
                  };
      
                  mark.PointerPressed += mark_PointerPressed;
                  maps.PointerMoved += mark_PointerMoved;
                  mark.PointerReleased += mark_PointerReleased;
      
                  vl.Children.Add(mark);
      
                  mark.DoubleTapped += mark_DoubleTapped;
              }
      

    5. The last section of code to add handles the Pointer events for the C1Maps control and the DoubleTapped event for the marks:
      C#
      Copy Code
      void mark_PointerMoved(object sender, PointerRoutedEventArgs e)
              {
                  if (current == null)
                  {
                      return;
                  }
      
                  var cur = e.GetCurrentPoint(maps).Position;
                  cur = new Point(cur.X - offset.X,
                      cur.Y - offset.Y);
                  current.GeoPoint = maps.ScreenToGeographic(cur);
      
                  e.Handled = true;
              }
      
              void mark_PointerPressed(object sender, PointerRoutedEventArgs e)
              {
                  offset = e.GetCurrentPoint(sender as C1VectorPlacemark).Position;
                  current = sender as C1VectorPlacemark;
                  e.Handled = true;
              }
      
              void mark_PointerReleased(object sender, PointerRoutedEventArgs e)
              {
                  current = null;
                  e.Handled = true;
              }
      
              void mark_DoubleTapped(object sender, DoubleTappedRoutedEventArgs e)
              {
                  e.Handled = true;
                  vl.Children.Remove((C1VectorPlacemark)sender);
              }
      

    In this step, you added code to handle the Tapped and other events. You also changed the map source and created random coordinates for the initial map markers.