Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
180
Adding different icones (gif) to XamWebMap
posted

Hi

How can I add Symbol Elements to  XamWebMap that have different gif images ?

Parents
No Data
Reply
  • 816
    Suggested Answer
    posted

    Ah, I think I see what you mean.

    Solution #1: You could do this by creating a distinct MapLayer for each SymbolElement.  So, each time you would like to add a custom image, you could create a MapLayer, set its ValueTemplate, and add the SymbolElement.  Here's an example:

    In XAML:

         <UserControl.Resources>

            <DataTemplate x:Name="dtPizza">
                <Image Width="50" Height="50" Source="pizza.png" />
            </DataTemplate>

            <!-- Insert additional custom DataTemplates here ... -->

        </UserControl.Resources>

    Then in C#:

          private void statesLayer_Loaded(object sender, RoutedEventArgs e)
          {
                string s = "dtPizza";
                if (this.Resources.Contains(s))
                {
                    // Create Layer
                    MapLayer layer = new MapLayer();

                    // Fetch ValueTemplate from Resources
                    DataTemplate dt = this.Resources[s] as DataTemplate;
                    layer.ValueTemplate = dt;

                    // Create Customized-Image Element
                    Point origin = map1.MapProjection.ProjectToMap(new Point(0, 0));
                    SymbolElement element = new SymbolElement() { SymbolOrigin = origin, SymbolType = MapSymbolType.None, SymbolSize = 20 };
                    element.Value = 1; // Assign arbitrary value so that Value Template can be used

                    // add element to layer
                    layer.Elements.Add(element);

                    // add layer to xamMap
                    map1.Layers.Add(layer);

                    // make enough space in layer for shape
                    Rect worldRect = layer.WorldRect;
                    worldRect.Union(element.WorldRect);
                    layer.WorldRect = worldRect;
                }
          }

    Basically, we look for the resource called "dtPizza" and use it to place a pizza image on the map.  You can define additional DataTemplates in UserControl.Resources so that you can place other images.

    This example adds the image programmatically, but you could also do something equivalent using only XAML.

    Solution #2: If you don't want to add so many MapLayers, you could also define a single MapLayer and set the ValueTemplate to a custom object.  Then you would bind the MapElement Value to a field of your custom object, and the custom object would render an image that is different depending on the Value. This actually seems like more work to me, but it might be appropriate in some cases.

    I hope this helps answer your question?

Children