Windows phone 7 将XML转换为地图上的PIN

Windows phone 7 将XML转换为地图上的PIN,windows-phone-7,bing-maps,Windows Phone 7,Bing Maps,我可以将列表中的Lat和Long作为完整字符串。我现在需要能够在地图上使用它 下面是显示地图的代码。目前,它显示了两个引脚和位置。(xaml) 编辑: 我正试图在bing地图上找到所有的站点。我可以得到一个列表中的所有位置,这将填充到一个列表框中。这一切都有效 它现在正试图把所有这些地点都记录在地图上,还有名字 所以我需要在网站上做什么-但在电话上-我不太确定你的问题是什么,但我认为你的图钉绑定不起作用 您已在ObservableCollection上实现INotifyPropertyChang

我可以将列表中的Lat和Long作为完整字符串。我现在需要能够在地图上使用它

下面是显示地图的代码。目前,它显示了两个引脚和位置。(xaml)

编辑: 我正试图在bing地图上找到所有的站点。我可以得到一个列表中的所有位置,这将填充到一个列表框中。这一切都有效

它现在正试图把所有这些地点都记录在地图上,还有名字


所以我需要在网站上做什么-但在电话上-

我不太确定你的问题是什么,但我认为你的图钉绑定不起作用

您已在ObservableCollection上实现INotifyPropertyChanged。那没必要。要使其工作,必须在PinModel上实现它,并在设置Location属性时调用RaisePropertyChanged方法

您的PinModel类应该如下所示,以使绑定工作(尽管您的绑定现在可能正在工作,因为您将它作为一个整体绑定一次)


还要注意,对linq查询调用的het.Single()方法返回的是单个值,而不是列表。请看一下Lance关于如何构造linq查询的答案。(有关.Single()文档,请参见此)

您检查过LINQ方法的输出了吗?您似乎只在列表中创建了一个实例。尝试此方法,而不是当前的解析方法

var locations = (from n in doc.Descendants(ns + "ArrayOfStop") 
                             select new RootContainer 
                             { 

                                 Location = (from s in n.Elements(ns + "Stop") 
                                             select new Location 
                                             { 
                                                 latitude = s.Element(ns + "Lat").Value + " ," + s.Element(ns + "Long").Value, 
                                                 // longitude = s.Element(ns + "Long").Value, 


                                             }).ToList() 
                             }); 

您好,我正在尝试获取站点列表,它们的lat和long被生成为一个列表,即纬度。然后我希望所有的站点都显示在地图上。我已经更改了pinModel,请参见编辑…可以;我看不出我可以把RaisePropertyChanged放在哪里,但它没有显示为错误OK,oi已经更新了,但是站点仍然没有在地图上填充。这一切都起作用了,这输出了位置,它将它们作为一个图钉。似乎发生的是,你的所有位置都被放在你的ObservableCollection中的一个项目中。即使在输出列表框中,它也会显示所有值,但它仍然是单个项。好的,我如何才能不将它们作为单个项来获取?这应该会有所帮助:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Device.Location;
using Microsoft.Phone.Shell;
using System.Xml.Serialization;
using System.Xml;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Controls;
using System.Runtime.Serialization.Json;
using System.Collections.ObjectModel;
using System.Runtime.Serialization;
using System.Text;
using System.Xml.Linq;
using System.Data.Linq.Mapping;
using System.ComponentModel;
using Microsoft.Phone.Controls.Maps;
using Microsoft.Phone.Controls.Maps.Platform;
namespace BrightonHoveBuses
{
public partial class location : PhoneApplicationPage
{


    public location()
    {
        InitializeComponent();
        DataContext = App.ViewMapModel;
        MapViewModel view = new MapViewModel();
        view.Load();
        this.DataContext = view;

        WebClient client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        Uri url = new Uri("http://www.henry-edwards.co.uk/feed.txt", UriKind.Absolute);
        client.DownloadStringAsync(url);

    }
    GeoCoordinateWatcher watcher;


    // Click the event handler for the “Start Location” button.
    private void startLocationButton_Click(object sender, RoutedEventArgs e)
    {
        // The watcher variable was previously declared as type GeoCoordinateWatcher. 
        if (watcher == null)
        {
            watcher = new GeoCoordinateWatcher(GeoPositionAccuracy.High); // using high accuracy
            watcher.MovementThreshold = 20; // use MovementThreshold to ignore noise in the signal

            watcher.StatusChanged += new EventHandler<GeoPositionStatusChangedEventArgs>(watcher_StatusChanged);
            watcher.PositionChanged += new EventHandler<GeoPositionChangedEventArgs<GeoCoordinate>>(watcher_PositionChanged);
        }


        watcher.Start();
    } // End of the Start button Click handler.





    public class RootContainer
    {
        [DataMember]
        public string StopName { get; set; }

        [DataMember]
        public string StopId { get; set; }

        [DataMember]
        public string Stop { get; set; }

        [DataMember]
        public string RouteId { get; set; }

        [DataMember]
        public string RouteName { get; set; }

        [DataMember]
        public string latitude { get; set; }

        [DataMember]
        public string longitude { get; set; }

        [DataMember]
        public List<Location> Location { get; set; }


    }





    void watcher_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
    {
        if ((App.Current as App).locsettings == false)
        {
            MessageBoxResult m = MessageBox.Show("Do you want to allow this application to use information about your location?", "Use Location", MessageBoxButton.OKCancel);

            if (m == MessageBoxResult.OK)
            {
                watcher.Start();
                (App.Current as App).locsettings = true;
            }
            else if (m == MessageBoxResult.Cancel)
            {
                watcher.Stop();
                (App.Current as App).locsettings = false;
            }
        }
        switch (e.Status)
        {
            case GeoPositionStatus.Disabled:
                // The Location Service is disabled or unsupported.
                // Check to see whether the user has disabled the Location Service.
                if (watcher.Permission == GeoPositionPermission.Denied)
                {
                    // The user has disabled the Location Service on their device.
                    MessageBox.Show("Location services must be enabled in your phone settings");
                }
                else
                {
                    MessageBox.Show("Location services must be enabled");
                }
                break;

        }
    }

    // Click the event handler for the “Start Location” button.
    private void stopLocationButton_Click(object sender, RoutedEventArgs e)
    {
        watcher.Stop();
    }


    private GeoCoordinateWatcher loc = null;


    public string stopslist;

    private void watcher_PositionChanged(object sender, GeoPositionChangedEventArgs<GeoCoordinate> e)
    {
        myPushPin.Location = e.Position.Location;
        map1.SetView(myPushPin.Location, 17.0);
        watcher.MovementThreshold = 100;
    }

    void loc_StatusChanged(object sender, GeoPositionStatusChangedEventArgs e)
    {
        if (e.Status == GeoPositionStatus.Ready)
        {
            map1.SetView(loc.Position.Location, 17.0);
            loc.Stop();

        }
    }

    void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            if (e.Result != null)
            {
                XDocument doc = XDocument.Parse(e.Result);
                XNamespace ns = "http://schemas.datacontract.org/2004/07/BusExpress.ClassLibrary";
                var locations = (from n in doc.Descendants(ns + "ArrayOfStop")
                                 select new RootContainer
                                 {

                                     Location = (from s in n.Elements(ns + "Stop")
                                                 select new Location
                                                 {
                                                     latitude = s.Element(ns + "Lat").Value + " ," + s.Element(ns + "Long").Value,
                                                     // longitude = s.Element(ns + "Long").Value,


                                                 }).ToList()
                                 }).Single();

                // Do something with the list of Route Names in routeNames 
                listBox1.ItemsSource = locations.Location;
            }
        }
    }

 public class MapViewModel : INotifyPropertyChanged
{
    public void Load()
    {

        //Do something here to populate your view collection with pins

        Pins.Add(new PinModel() { Id = 2, Name = string.Format("Pin # 2"), Location = new GeoCoordinate(39.932825, -75.168396) });

    }

    private ObservableCollection<PinModel> _pins = new ObservableCollection<PinModel>();
    public ObservableCollection<PinModel> Pins
    {
        get { return _pins; }
        set { _pins = value; RaisePropertyChanged("Pins"); }
    }

    //Event code to ensure the page updates to model changes.
    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

    private void Pushpin_MouseLeftButtonUp_1(object sender, MouseButtonEventArgs e)
    {
        Pushpin pin = (Pushpin)sender;
        MessageBox.Show(pin.Content.ToString());
    }
    public class PinModel
    {
        public string Name { get; set; }
        public int Id { get; set; }
        public GeoCoordinate Location { get; set; }
    }

   }

    private void ButtonLocation_Click(object sender, EventArgs e)
    {
        loc = new GeoCoordinateWatcher(GeoPositionAccuracy.Default);
        //EventHandler for location service status changes
        loc.StatusChanged += loc_StatusChanged;
        //If the location service is disabled or not supported
        if (loc.Status == GeoPositionStatus.Disabled)
        {
            //Display a message
            MessageBox.Show("Location services must be enabled");
            return;
        }
        loc.Start();
    }

    private void Pushpin_MouseLeftButtonUp_1(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        Pushpin pin = (Pushpin)sender;
    }

    private void Buttonminus_Click(object sender, EventArgs e)
    {
        double zoom;
        zoom = map1.ZoomLevel;
        map1.ZoomLevel = --zoom;

    }

    private void Buttonplus_Click(object sender, EventArgs e)
    {
        double zoom;
        zoom = map1.ZoomLevel;
        map1.ZoomLevel = ++zoom;

    }

    private void ApplicationBarRoad_Click(object sender, EventArgs e)
    {
        map1.Mode = new RoadMode();

    }

    private void ApplicationBarAerial_Click(object sender, EventArgs e)
    {
        map1.Mode = new AerialMode();

    }


   }


  }
  public class PinModel : INotifyPropertyChanged
    {
        public string Name { get; set; }
        public int Id { get; set; }
        public GeoCoordinate Location { get; set; }
    }
public class PinModel : INotifyPropertyChanged
{
    public string Name { get; set; }
    public int Id { get; set; }
    private GeoCoordinate _location;
    public GeoCoordinate Location
    {
        get
        {
            return _location;
        }
        set
        {
            _location = value;
            RaisePropertyChanged("Location");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;
    private void RaisePropertyChanged(string propertyName)
    {
        if (this.PropertyChanged != null)
        {
            this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    } 

} 
var locations = (from n in doc.Descendants(ns + "ArrayOfStop") 
                             select new RootContainer 
                             { 

                                 Location = (from s in n.Elements(ns + "Stop") 
                                             select new Location 
                                             { 
                                                 latitude = s.Element(ns + "Lat").Value + " ," + s.Element(ns + "Long").Value, 
                                                 // longitude = s.Element(ns + "Long").Value, 


                                             }).ToList() 
                             });