Bing映射来自XML Feed的Pushpins

时间:2010-12-06 12:14:18

标签: c# xml silverlight windows-phone-7

我一直在使用本教程尝试在地图上显示图钉: http://compiledexperience.com/windows-phone-7/tutorials/quake

我几乎在那里帮助你们,但是

如果我用原始位替换我的位(注释掉的部分) 然后做'开始调试',我得到一个未处理的异常。如果我继续调试,我会看到地图和图钉一瞬间然后退出。 我把原来的注释代码放回原处...... 如果您需要更多信息,请告诉我。我很沮丧:(再次感谢。

调用堆栈:

  

QuakeML.dll!QuakeML.App.OnUnhandledException(object sender = {QuakeML.App},System.Windows.ApplicationUnhandledExceptionEventArgs e = {System.Windows.ApplicationUnhandledExceptionEventArgs})第37行+ 0x5字节C#       System.Windows.dll!MS.Internal.Error.CallApplicationUEHandler(System.Exception e = {“UIElement.Arrange(finalRect)无法在finalRect中使用无限或NaN值调用。”})+ 0x30字节
      System.Windows.dll!MS.Internal.Error.CallAUEHandler(uint hr = 2148474880,out uint bIsHandled = 0)+ 0x6 bytes
      [原生于管理过渡]

当前代码:

namespace QuakeML
{
    public partial class MainPage
    {
        public MainPage()
        {
            InitializeComponent();

            Loaded += OnLoaded;
        }

        private void OnLoaded(object sender, RoutedEventArgs e)
        {
            LoadQuakes();
        }

        private void LoadQuakes()
        {
            var webClient = new WebClient();

            webClient.OpenReadCompleted += OnOpenReadCompleted;


            var uri = "http://www.tfl.gov.uk/tfl/businessandpartners/syndication/feed.aspx?email=mycomputer@clara.co.uk&feedId=3";
          //  var uri = "http://magma.geonet.org.nz/services/quake/quakeml/1.0.1/query?startDate=2010-09-03&endDate=2010-09-05&magnitudeLower={0:0.0}&magnitudeUpper=8";

            webClient.OpenReadAsync(new Uri(uri, UriKind.Absolute));
        }



        private void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            var document = XDocument.Load(e.Result);

             if(document.Root == null)
                return;

             var xmlns = XNamespace.Get("http://www.tfl.gov.uk/tfl/syndication/namespaces/geo");
             //var xmlns = XNamespace.Get("http://quakeml.org/xmlns/quakeml/1.0");

                var events = from ev in document.Descendants("item")
                         select new
                         {
                             Latitude = Convert.ToDouble(ev.Element(xmlns + "Point").Element(xmlns + "lat").Value),
                             Longitude = Convert.ToDouble(ev.Element(xmlns + "Point").Element(xmlns + "long").Value),
                         };


              //var events = from ev in document.Descendants(xmlns + "event")
                //           select new
               //            {
                //              Latitude = Convert.ToDouble(ev.Element(xmlns + "origin").Element(xmlns + "latitude").Element(xmlns + "value").Value),
               //               Longitude = Convert.ToDouble(ev.Element(xmlns + "origin").Element(xmlns + "longitude").Element(xmlns + "value").Value),

               //           };

            QuakeLayer.Children.Clear();


                foreach(var ev in events)
            {
                var accentBrush = (Brush)Application.Current.Resources["PhoneAccentBrush"];

                var pin = new Pushpin
                {
                    Location = new GeoCoordinate
                    {
                        Latitude = ev.Latitude,
                        Longitude = ev.Longitude
                    },
                    Background = accentBrush,

                };

                QuakeLayer.AddChild(pin, pin.Location);
            }
        }

        private void OnRefresh(object sender, RoutedEventArgs e)
        {
            LoadQuakes();
        }

    }
}

主页上的地图项目:

                                                                                    

2 个答案:

答案 0 :(得分:1)

如果您指定了获得异常的行,那将会有所帮助,但我会猜测您使用XName的方式存在问题。

您使用“geo:Point”,“geo:lat”和“geo:long”的位置首先应创建以下变量: -

XName geoPoint = XName.Get("Point", "http://www.tfl.gov.uk/tfl/syndication/namespaces/geo");
XName geoLat = XName.Get("lat", "http://www.tfl.gov.uk/tfl/syndication/namespaces/geo");
XName geoLong = XName.Get("long", "http://www.tfl.gov.uk/tfl/syndication/namespaces/geo");

然后您的代码可能如下所示: -

var events = from ev in document.Root.Descendants("item")   
             select new   
             {   
                Latitude = (double)ev.Element(geoPoint).Element(geoLat),   
                Longitude = (double)ev.Element(geoPoint).Element(geoLong)   
             };

答案 1 :(得分:1)

您需要命名空间参考,因为它在您正在解析的XML中具有别名。

这有效:

var xmlns = XNamespace.Get("http://www.tfl.gov.uk/tfl/syndication/namespaces/geo");

var events = from ev in document.Root.Descendants("item")
                select new
                        {
                            Latitude = Convert.ToDouble(ev.Element(xmlns + "Point").Element(xmlns + "lat").Value),
                            Longitude = Convert.ToDouble(ev.Element(xmlns + "Point").Element(xmlns + "long").Value),
                        };