从xml解析wp7到lat / long绑定到映射

时间:2011-08-01 03:41:36

标签: data-binding bing-maps windows-phone

我被赋予了一个从Web服务器解析xml的任务,并获取用户信息,包括lat / long昵称和图像,然后将它们绑定到地图(其中有很多)。我使用WebClient和XDocument来处理解析。 lat / long的类型是什么以及如何将它们绑定到地图上?

        void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        using (var reader = new StreamReader(e.Result))
        {
            //  string[] _elements = { "one", "two", "three" }; 
            int[] counter = { 1 };
            string s = reader.ReadToEnd();
            Stream str = e.Result;
            str.Position = 0;
            XDocument xdoc = XDocument.Load(str);

            var data = from query in xdoc.Descendants("position")
                       select new mapping
                       {
                  //         color = counter[0]++,
                           album = (string)query.Element("album"),
                           track = (string)query.Element("track"),
                           artist = (string)query.Element("artist"),
                           nickname = (string)query.Element("user_info").Element("nickname"),
                           pos_lat = (GeoCoordinate)query.Element("lat"),

                       };
            //   lb1.ItemsSource = data;   
    //        listbox1.ItemsSource = data;
        }
    }


    public class mapping
    {
        public int index { get; set; }
        public string album { get; set; }
        public string track { get; set; }
        public string artist { get; set; }
        public GeoCoordinate pos_lat { get; set; }
        public GeoCoordinate pos_lon { get; set; }
        public string nickname { get; set; }
    }


<Microsoft_Phone_Controls_Maps:Map Height="427" HorizontalAlignment="Left" Margin="12,6,0,0" Name="map1" VerticalAlignment="Top" Width="438" />

1 个答案:

答案 0 :(得分:1)

我认为你误解了GeoCoordinate是为了。 GeoCoordinate是一个坐标,代表纬度,经度和一些other geographical location properties

您正在读出的值是double,表示纬度或经度。您根本无法将double投射到GeoCoordinate,并且<= 1>对纬度和经度都有单独的GeoCoordinate

所以你需要做的只是将纬度和经度读出为双倍,然后根据它创建一个GeoCoordinate

使用Double.TryParse来处理转化(因为您最初将所有值都读为string