Search code examples
c#windows-phone-8.1windows-phone

How to add marker at current location on a Windows Phone App Map


First of all I'm very new to Windows Phone dev so I might miss something obvious. I have a ControlMap in my xaml, i'm trying to add marker at my current location. I've gone through many tutorials and I can't only find deprecated ways (tons of using not working) to do it and while it seems simple at first view, I simply can't make it work.

The map in the xaml :

    <Maps:MapControl x:Name="LocateMap" Height="221" Margin="0,0,-0.167,0"/>

What should I do in the .cs to add this marker ? And where ? The intent is to store it later, and then print when the app launch all the olds markers + the actual location marker.


Solution

  • If you want to set a pin on a specific location (latitude, longitude), do the following steps.

      //setting the Map center
      LocateMap.Center = CurrentLocation = new System.Device.Location.GeoCoordinate(lat, lon);
    
      // Create a small circle to mark the current location.
      Ellipse myCircle = new Ellipse();
      myCircle.Fill = App.Current.Resources["PhoneAccentBrush"] as SolidColorBrush;
     myCircle.Height = 20;
     myCircle.Width = 20;
     myCircle.Opacity = 50;
    
     // Create a MapOverlay to contain the circle.
     MapOverlay myLocationOverlay = new MapOverlay();
     myLocationOverlay.Content = myCircle;
     myLocationOverlay.PositionOrigin = new Point(0.5, 0.5);
     myLocationOverlay.GeoCoordinate = new System.Device.Location.GeoCoordinate(lat,
                   lon);
     // Create a MapLayer to contain the MapOverlay.
     MapLayer myLocationLayer = new MapLayer();
     myLocationLayer.Add(myLocationOverlay);
    
     //adding map layer to the map
     LocateMap.Layers.Add(myLocationLayer);