MapPolyline not being drawn
Asked Answered
Y

2

3

I am trying to use a MapPolyLine in my Map to show a real-time route, hopefully it will move/scale this time. The thing is the line is not being shown on the map, and I cannot find any programming mistake:

C#

MapLayer pathLayer;

//Constructor
 pathLayer = new MapLayer();
 MapPolyline line = new MapPolyline();
 line.StrokeColor = Colors.Red;
 line.StrokeThickness = 10;
 //line.Path.Add(several points); Tested, no effect
 MapOverlay overlay = new MapOverlay();
 overlay.Content = line;
 //overlay.GeoCoordinate = new GeoCoordinate(0,0); Tested, no effect
 //overlay.PositionOrigin = new Point(0.0, 1.0); Tested, no effect
 pathLayer.Add(overlay);
 MyMap.Layers.Add(pathLayer);


void geolocator_PositionChanged(Geolocator sender, PositionChangedEventArgs args)
{
  MapPolyline line = pathLayer.First(TrackPath).Content as MapPolyline;
  line.Path.Add(args.Position.Coordinate); // Checked values, line.Path adds them correctly
}

EDIT: New info. The emulator shows an error when trying to add it using XAML, and the emulator shows the name of the class on the top of the map as a graphic glitch:

Emulator error on top, XAML on the right

Yarbrough answered 29/11, 2012 at 12:25 Comment(1)
I also see the XAML error but it is still building and the polyline is showing up when I define the path in XAML.Yuk
Y
9

MapPolylines and MapPolygons should be added to the MapElements collection... not a MapLayer or a MapOverlay.

You should be able to make this example work for you.

        MapPolyline line = new MapPolyline();
        line.StrokeColor = Colors.Red;
        line.StrokeThickness = 10;
        line.Path.Add(new GeoCoordinate(47.6602, -122.098358));
        line.Path.Add(new GeoCoordinate(47.561482, -122.071544));
        MyMap.MapElements.Add(line);

In your GeoCoord watcher you'll have to get the line from the map's MapElements collection, and add the new position to the line's path instead of predefining like I did. This should be doable.

Yuk answered 29/11, 2012 at 21:25 Comment(1)
Noticed you've answered a few questions on wp8 and I was wondering if you could out this one - #13830553Naught
T
0

In Windows Phone 8.1 try add points in this way. "punkty" is my collection.

  List<BasicGeoposition> PosList = new List<BasicGeoposition>();
  foreach (var item in punkty)
  {
    PosList.Add(new BasicGeoposition()
    {
      Latitude = item.Position.Latitude,
      Longitude = item.Position.Longitude
    });
  }

  //Example of one point
  //PosList.Add(new BasicGeoposition()
  //{
  //  Latitude = 52.46479093,
  //  Longitude = 16.91743341
  //});

  MapPolyline line = new MapPolyline();
  line.StrokeColor = Colors.Red;
  line.StrokeThickness = 5;
  line.Path = new Geopath(PosList);

  myMap.MapElements.Add(line);
Tyratyrannical answered 30/11, 2015 at 16:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.