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;
this worked fine for me using your code on a page like this:
<Grid x:Name="LayoutRoot"> <igMap:XamWebMap x:Name="xamWebMap1"> <igMap:XamWebMap.Layers> <igMap:MapLayer> <igMap:MapLayer.Reader> <igMap:ShapeFileReader Uri="world" /> </igMap:MapLayer.Reader> </igMap:MapLayer> </igMap:XamWebMap.Layers> </igMap:XamWebMap> <Button Height="50" Width="50" x:Name="DrawPath" Click="DrawPath_Click" /> </Grid>
the first time i clicked the button, a white line was drawn from South Carolina to the Mississippi river. i tested this using version 9.1.20091.1000. what version are you using?
is the map fully loaded and rendered when you click this button?
as a workaround, you could try calling MapLayer.RenderWindow() to force a re-render. beware, though, this causes a full refresh of the map which is an unnecessarily large operation and could impact your application's performance.
I wasn't entirely sure how to check the version so I went to Infragistics.Silverlight.DataVisualization.Map.v9.1.dll and clicked on properties. The version showed 9.1.20091.1000.
I am still seeing nothing after the first time through my code. I am using Silverlight 3 and FireFox 3.5.2.
I notice in the online docs that the tree control has no vertical scroll bar in FireFox but does in IE.
I will run the app in IE and see if I get different behavior.
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>
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 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.
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
thanks for reporting that! i have notified the docs team.