开发者

Bing maps Pushpins from XML Feed

开发者 https://www.devze.com 2023-01-29 05:50 出处:网络
Ive been using this tutorial to try and display pushpins on a map: http://compiledexperience.com/windows-phone-7/tutorials/quake

Ive been using this tutorial to try and display pushpins on a map: http://compiledexperience.com/windows-phone-7/tutorials/quake

Im almost there wit hthe help you you guys below, but

If I replace my bits with the original bits (commented out parts) and then do 'Start debugging', I get an unhandled exception. If I continue debugging I see the map and the pushpins for a split second and then it quits out. I put back the original commented code and its fine... Let me know if you need anymore info. Im so frustrated at this :( Thanks once again.

Callstack:

QuakeML.dll!QuakeML.App.OnUnhandledException(object sender = {QuakeML.App}, System.Windows.ApplicationUnhandledExceptionEventArgs e = {System.Windows.ApplicationUnhandledExceptionEventArgs}) Line 37 + 0x5 bytes C# System.Windows.dll!MS.Internal.Error.CallApplicationUEHandler(System.Exception e = {"UIElement.Arrange(finalRect) cannot be called with Infinite or NaN values in finalRect. "}) + 0x30 bytes

System.Windows.dll!MS.Internal.Error.CallAUEHandler(uint hr = 2148474880, out uint bIsHandled = 0) + 0x6 bytes

[Native to Managed Transition]

Current code:

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开发者_StackOverflow社区(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();
        }

    }
}

Map item on mainpage:


You need the namespace reference as it's aliased in the XML you're parsing.

This works:

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),
                        };


It would help if you specified the line on which you get the exception but I'm going to guess its a problem with the way you are using XNames.

Where you are using "geo:Point", "geo:lat" and "geo:long" you should first create these variables:-

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");

Then your code can look like:-

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)   
             };
0

精彩评论

暂无评论...
验证码 换一张
取 消