Select to view content in your preferred language

Help for Newbie

897
3
09-20-2011 07:15 AM
JamesCulpepper
New Contributor
I'm relatively new to silverlight and have been asked to try and figure out how to map some points using the ESRI silverlight maps.
I have a xml file that contains lat and long attributes.  I'm trying to loop through the xml doc and add the lat/long to a layer. I had some issues with the code so now I'm just trying to add the first lat/long in the xml doc as a single point in the layer.  My problem is that the map will load (display) but the points or point will not display.  Please help.
Here is the c#
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Windows.Controls;
using System.Windows.Media;
using ESRI.ArcGIS.Client;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Symbols;
using System.Xml;
using ESRI.ArcGIS.Client.Graphics;

namespace addpointsfromxml
{
    public partial class MainPage : UserControl
    {
        private static ESRI.ArcGIS.Client.Projection.WebMercator mercator = new ESRI.ArcGIS.Client.Projection.WebMercator();

        public MainPage()
        {
            
            InitializeComponent();
            AddMarkerGraphics();
        }


        void AddMarkerGraphics()
        {
            GraphicsLayer point = MyMap.Layers["MyGraphicsLayer"] as GraphicsLayer;
            XmlReader reader = XmlReader.Create("crashLoc.xml");
          
            
            
            
                while (reader.ReadToFollowing("marker"))
                {
                    Graphic g = new Graphic();
                    {


                        double x = Convert.ToDouble(reader.GetAttribute("lat"));
                        double y = Convert.ToDouble(reader.GetAttribute("lng"));

                        ESRI.ArcGIS.Client.Geometry.Geometry geo = new MapPoint(x, y);
                        g.Attributes.Add("Latitute", x);
                        g.Attributes.Add("Longitutde", y);
                        
                        point.Graphics.Add(g);
                        
                        //reader.ReadToDescendant("marker");
                    }
                }
                MyMap.Layers.Add(point);
        }   

    }
}


and here is the xaml:
<UserControl x:Class="addpointsfromxml.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:esri="clr-namespace:ESRI.ArcGIS.Client;assembly=ESRI.ArcGIS.Client"
    xmlns:esriSymbols="clr-namespace:ESRI.ArcGIS.Client.Symbols;assembly=ESRI.ArcGIS.Client">

    <Grid x:Name="LayoutRoot" Background="White">

        <Grid.Resources>
            <esriSymbols:SimpleMarkerSymbol x:Key="RedMarkerSymbol" Color="Red" Size="12" Style="Circle" />
            <esriSymbols:SimpleMarkerSymbol x:Key="BlackMarkerSymbol" Color="Black" Size="14" Style="Diamond" />
            <esriSymbols:PictureMarkerSymbol x:Key="GlobePictureSymbol" OffsetX="8" OffsetY="8" 
                Source="/Assets/images/globe-16x16.png" />
            <esriSymbols:SimpleLineSymbol x:Key="DefaultLineSymbol" Color="Green" Style="DashDot" Width="4" />
            <esriSymbols:SimpleFillSymbol x:Key="DefaultFillSymbol" Fill="Green" BorderBrush="Blue" 
                BorderThickness="3" />
        </Grid.Resources>

        <esri:Map x:Name="MyMap" WrapAround="True" Background="White">
            <esri:ArcGISTiledMapServiceLayer ID="PhysicalTiledLayer" 
                      Url="http://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer"/>
            <esri:GraphicsLayer ID="MyGraphicsLayer" />
        </esri:Map>

    </Grid>
</UserControl>


this is a part of the xml I am getting the lat/lng from:
<?xml version="1.0" encoding="utf-8"?>
<Markers>
<marker lat="35.1628834" lng="-89.8608" address="6069 MACON" Date_time="2011-08-01T11:12:35" Crash_number="-2145850900" />
<marker lat="35.145575" lng="-90.054077" address="67 MADISON" Date_time="2011-08-01T11:25:44" Crash_number="-2145850883" />
<marker lat="35.095578" lng="-90.012385" address="1843 MEADOWHILL" Date_time="2011-08-01T19:27:42" Crash_number="-2145850299" />
<marker lat="35.130815" lng="-89.860618" address="6019 WALNUT GROVE" Date_time="2011-08-01T08:52:54" Crash_number="-2145851066" />
<marker lat="35.044939" lng="-89.866404" address="3830 HICKORY HILL" Date_time="2011-08-02T00:03:48" Crash_number="-2145849937" />
<marker lat="35.021242" lng="-90.063027" address="32 SHELBY" Date_time="2011-08-02T02:05:40" Crash_number="-2145849833" />
<marker lat="35.0290315" lng="-89.869286" address="5812 SUNNY MORNING" Date_time="2011-08-02T05:44:55" Crash_number="-2145849698" />
0 Kudos
3 Replies
DominiqueBroux
Esri Frequent Contributor
Your map is using mercator projected coordinates because the spatial reference of your tiled layer is  102100.

So you have either to project the geographical coordinates coming from your xml file to mercator coordinates, or to use a tiled layer in geographical coordinates (such as http://services.arcgisonline.com/ArcGIS/rest/services/ESRI_ShadedRelief_World_2D/MapServer)
0 Kudos
HugoCardenas
Emerging Contributor
James,
Dominique is right.  If you choose to project your points to web mercator (the option I would follow) do the following:

1.  Load your xml file into a collection using LINQ.  Create a small class that will hold all your xml data:

public class MyXmlData
{
  public string Lat { get; set; }
  public string Lng { get; set; }
  public string Address { get; set; }

   ... and so forth...
}

2. Write a metohd that returns the loaded the xml file into your class and into a collection:

public List<MyXmlData> GetXmlPoints()
{
  XDocument xmlDocument = XDocument.Load(@"c:\\Xmlfile.XML");
  var points = from r in xmlDocument.Descendants("Markers")
  select new MyXmlData
  {
     Lat= r.Element("lat").Value,
     Lng= r.Element("lng").Value,
     Address= r.Element("address").Value,

     ....and so forth....,
   };
   return points.ToList();
}

3.  Now iterate through your list:

List<MyXmlData> list = GetXmlPoints();
GraphicsLayer graphicLayer = new GraphicsLayer();

foreach (MyXmlData point in list)
{
  double lat = Convert.ToDouble(point.Lat);
  double lng= Convert.ToDouble(point.Lng);

  // Create a point feature with lat/long coordinates.
   MapPoint ptLatLng = new MapPoint(lng, lat);

  // Project apoint feature from lat/long to Web Mercator projection of Bing Maps.
   MapPoint ptWebMerc = Bing.Transform.GeographicToWebMercator(ptLatLng);

  // Create a graphic instance to store the the point geometry.
   ESRI.ArcGIS.Client.Graphic graphic = new ESRI.ArcGIS.Client.Graphic();

  // Add point feature to graphic instance (geometry)
  graphic.Geometry = new MapPoint(ptWebMerc.X, ptWebMerc.Y);

  // Add some attributes
  graphic.Attributes.Add("Address", point.Address);
  graphic.Attributes.Add("Lat", ptWebMerc.Y);
  graphic.Attributes.Add("Lng", ptWebMerc.X);
   graphic.Attributes.Add("Date_Time", DateTime.Parse(point.Date_Time));

  ...and so forth....

// Add graphic to graphics layer
  graphicsLayer.Graphics.Add(graphic);
}

// Add graphics layer to map
MyMap.Layers.Add(graphics);

Hope this helps!

Hugo,
0 Kudos
HugoCardenas
Emerging Contributor
James,
I am sorry.  I missed the fact your xml stores the values in attributes of the "marker" tag.  (I do not know what I was thinking!)

Therefore, call the "marker" tag, rather than "Markers", and reference "Attribute", rather than "Element" (cf. resulting method below).

public List<MyXmlData> GetXmlPoints()
    {
      XDocument xmlDocument = XDocument.Load("test.xml");
      var points = from r in xmlDocument.Descendants("marker")
                   select new MyXmlData
                   {
                     Lat =  r.Attribute("lat").Value,
                     Lng = r.Attribute("lng").Value,
                     Address = r.Attribute("address").Value,
                     Date_time = r.Attribute("Date_time").Value,
                     Crash_number = r.Attribute("Crash_number").Value,
                   };
      return points.ToList();
    }

It works fine!  I tested it.  I created a class (cf. below) like the suggested one:

public class MyXmlData
{
  public string Lat { get; set; }
  public string Lng { get; set; }
  public string Address { get; set; }
  public string Date_time { get; set; }
  public string Crash_number { get; set; }
}

Make sure your project references the linq and xml libraries:

using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;

Hope this helps!

Hugo.
0 Kudos