I have a shapefile at the county level and need to add 50+ shapefiles at the precinct level, but only if the county is clicked on. This is the function I have:
Private Sub xamWebMap_ElementClick(ByVal sender As Object, ByVal e As MapElementClickEventArgs) Dim map1 As XamWebMap = DirectCast(sender, XamWebMap) Dim i As Integer i += 1 For Each ele As MapElement In map1.Layers(0).Elements Dim builder As New StringBuilder() ele.Name = builder.Append("precinct" + i.ToString()).ToString i = i + 1 Next Dim elementName As String = e.Element.Name map1.WindowFit(e.Element) Dim newLayer As New MapLayer() newLayer.LayerName = elementName newLayer.VisibleFromScale = 3 map1.Layers.Add(newLayer) Dim reader As New ShapeFileReader() Dim converter As New DataMapping.Converter() reader.Uri = "Shapefiles/" + elementName reader.DataMapping = TryCast(converter.ConvertFromString("Caption=NAME"), DataMapping) newLayer.Reader = reader Dim layer As MapLayer = DirectCast(map1.Layers.FindName(elementName).ElementAt(0), MapLayer) Dim elements As IEnumerable(Of MapElement) = TryCast(layer.Elements.FindElement("Caption", "Name"), IEnumerable(Of MapElement)) For Each element As MapElement In layer.Elements pop.IsOpen = False element.IsClickable = False Next If elements.Count() > 0 Then pop.IsOpen = True Dim element As MapElement = TryCast(elements.ElementAt(0), MapElement) element.Caption = Nothing End If End Sub
I am naming all of the shapefiles precinct1, precinct2 and so on. Can anyone help?
Thanks
What's the behavior you are seeing for this code? Does it give an error? Or does the new layer just not show up? I'll see if I can put an example together for dynamically adding a layer. In the meantime, have you reviewed this information: http://help.infragistics.com/Help/NetAdvantage/DV/2009.2/CLR3.5/html/SL_DV_xamWebMap_Using_Multiple_Layers.html
-Graham
public partial class MainPage : UserControl
{
public MainPage()
InitializeComponent();
}
private void map1_WindowRectChanged(object sender, Infragistics.Silverlight.Map.MapWindowRectChangedEventArgs e)
XamWebMap map1 = (XamWebMap)sender;
if (Math.Round(map1.ScaleToZoom(e.WindowScale)) == 3)
MapLayer layer = (MapLayer)map1.Layers.FindName("worldLayer").ElementAt(0);
IEnumerable<MapElement> elements = layer.Elements.FindElement("Caption", "UNITED STATES") as IEnumerable<MapElement>;
if (elements.Count<MapElement>() > 0)
MapElement element = elements.ElementAt<MapElement>(0) as MapElement;
element.Caption = null;
private void map1_ElementClick(object sender, MapElementClickEventArgs e)
if (e.Element != null && e.Element.Caption == "UNITED STATES")
map1.WindowFit(e.Element);
MapLayer statesLayer = new MapLayer();
statesLayer.Name = "statesLayer";
statesLayer.VisibleFromScale = 2;
DataMapping.Converter converter = new DataMapping.Converter();
ShapeFileReader reader = new ShapeFileReader();
reader.Uri = "ShapeFiles/usa_st";
reader.DataMapping = (DataMapping)converter.ConvertFromString("Caption=STATE_ABBR");
statesLayer.Reader = reader;
map1.Layers.Add(statesLayer);
statesLayer.ImportAsync();
Here's a sample based on the link I provided above. I believe the crucial part you are missing is to tell the MapLayer to import its content from the shapefile. Something the control does for you with designtime layers.
This is the line you are looking for:
Note, it is important to call this after the reader has been added to the layer, and after the layer has been added to the map.
Let me know how it goes!
Do you have a sample that illustrates this behavior?
Hi
Here with i attached the sample code which i was tried. In this i just tried to add one layer from code behind with the use of Geo spatial data. i need to add multiple layers like this. layers are adding up when i check the layer count it shows value but map not displaying. But if i define the layer in xaml itself it showing the map.
Can you please check and if possible give some working model for the service i have created.
Find the attachment above. I have attached three files
1. MainPage.xaml
2.MainPage.xaml.cs
3.SqlDbService.svc.cs
Oniv, if you are adding the layer programmatically like that, call WindowFit on the map control when the layer is done importing. This will adjust the map window to encompass the new content.
now its working when i add one layer dynamically but when i add multiple layers the layer added last only showing up. I noticed that the GetDataCompleted event is firing only when all the layers added not when every layer added. what is the problem. I think my approach is wrong.Can you help me. I am using Geo Spatial data to create shapes. Here with i have attached the code for the setup i have tried and the service i have created.
Those calls to GetDataAsync are asynchronous, as the method name implies, so using the layerCnt field as you are trying will not work as it will get incremented to 3 before the first response even comes. You will have to use another method to identify the data returned from the data service.