Below is about the simplest possible code for drawing a line between two points. the event is tied to a button that coexists on a page with a xamWebMap. The map is pretty much drawn from the samples.
The odd thing is that DrawPath_Click does nothing the first time around. I can see an element added to xamWebMap1.Layers[0].Elements and the visible attribute is set to visible but it doesn't show up on the map. The second and subsequent calls to DrawPath_Click are fine.
I checked to see if maybe I was one rendering behind all the time and I don't think that's the case but I am often wrong.
private void DrawPath_Click(object sender, RoutedEventArgs e)
{
drawLine(36 + numCalls, -80, 39 + numCalls, -95);
numCalls += 1;
}
void drawLine(double srcLat, double srcLong, double destLat, double destLong)
MapPolylineCollection lines = new MapPolylineCollection();
List<Point> points = new List<Point>();
points.Add(new Point(srcLong, srcLat));
points.Add(new Point(destLong, destLat));
lines.Add(xamWebMap1.Projection.ProjectToMap(points));
PathElement lineElement = new PathElement() { Polylines = lines };
lineElement.Fill = new SolidColorBrush(Colors.White);
lineElement.StrokeThickness = 2;
xamWebMap1.Layers[0].Elements.Add(lineElement);
Rect worldRect = lineElement.WorldRect;
worldRect = lines.GetWorldRect();
lineElement.WorldRect = worldRect;
thanks for reporting that! i have notified the docs team.
Thanks, that did it.
You need to fix the docs. I lifted the faulty code directly from:
Developers Guide/Controls/xamWebMap/Using xam Web Map/Data Elements/Add a Path Element.
You have the same error in the other "Add a xxxxx Element" topics as well.
Roger
i spoke to the lead developer on the XamWebMap and he informed me:
"the element needs to have its world rect set before being added to the layer.
"when you add an element, the map takes a look and decides if it's worth drawing it. if the world rect is empty, then the map decides that it probably isn't worth drawing."
in other words, calling Elements.Add triggers a re-render for the layer, but since the WorldRect for your element has not yet been set, it is skipped because of this optimization. this also explains why subsequent calls will render the element, since its WorldRect is set after rendering occurs the first time.
once i started using the usa_st shapefile, i was able to reproduce the problem as you reported it.
and although i don't completely understand why, setting the WorldRect property of lineElement before calling MapLayer.Elements.Add, instead of afterwards, resolved the problem.
I simplified my XAML to match yours (sort of). Same result in both IE8 and Firefox 3.
I am using Visual Studio 2008 and all Silverlight 3.
I have viewed map.layer.elements. Is there something I can look at to see if an element will be displayed?
<igMap:XamWebMap x:Name="xamWebMap1" Grid.Row="1" Grid.Column="1" >
<igMap:XamWebMap.Layers>
<igMap:MapLayer x:Name="usa_st">
<igMap:MapLayer.Reader>
<igMap:ShapeFileReader Uri="Shapefiles/usa_st" />
</igMap:MapLayer.Reader>
</igMap:MapLayer>
</igMap:XamWebMap.Layers>
</igMap:XamWebMap>