2

我正在尝试根据给定实例在地图上显示的各种路线动态设置地图控件的中心属性。我在这里使用路线的起点作为地理坐标中心。为了实现这一点,我尝试使用 bing 地图通过 windows phone 应用程序上的 mvvm 将地理坐标中心绑定到地图控件。我能够通过纬度和经度获取中心位置的数据,但无法将其绑定到 UI(xaml) 页面。

以下是我的 RouteViewModel 类

 private Location startPoint;
    public Location StartPoint
    {
        get { return startPoint; }
        set
        {
            startPoint = value;
            Change("StartPoint");
        }
    }

   private System.Device.Location.GeoCoordinate centre;

    public System.Device.Location.GeoCoordinate Centre
    {
        get { return centre; }
        set { centre = value; }
    }

    private string getcenter;

    public string Getcenter
    {
        get { return getcenter; }
        set { getcenter = value; }
    }

    private Location endPoint;
    public Location EndPoint
    {
        get { return endPoint; }
        set
        {
            endPoint = value;
            Change("EndPoint");
        }
    }

    private ObservableCollection<Location> routePoints;
    public ObservableCollection<Location> RoutePoints
    {
        get { return routePoints; }
        set
        {
            routePoints = value;
            Change("RoutePoints");
        }
    }

    private ObservableCollection<ItineraryItem> itinerary;
    public ObservableCollection<ItineraryItem> Itinerary
    {
        get
        {
            return itinerary;
        }
        set
        {
            itinerary = value;
            Change("Itinerary");
        }
    }


   private void LocationLoaded()
    {
        if (fromLocation != null && toLocation != null)
        {
            var fromWaypoint = new Waypoint();
            fromWaypoint.Description = "From";
            fromWaypoint.Location = new Location();
            fromWaypoint.Location.Altitude = fromLocation.Altitude;
            fromWaypoint.Location.Latitude = fromLocation.Latitude;
            fromWaypoint.Location.Longitude = fromLocation.Longitude;

            var toWaypoint = new Waypoint();
            toWaypoint.Description = "To";
            toWaypoint.Location = new Location();
            toWaypoint.Location.Altitude = toLocation.Altitude;
            toWaypoint.Location.Latitude = toLocation.Latitude;
            toWaypoint.Location.Longitude = toLocation.Longitude;

            var routeRequest = new RouteRequest();
            routeRequest.Credentials = new Credentials();
            routeRequest.Credentials.ApplicationId = App.BingApiKey;
            routeRequest.Waypoints = new System.Collections.ObjectModel.ObservableCollection<Waypoint>();
            routeRequest.Waypoints.Add(fromWaypoint);
            routeRequest.Waypoints.Add(toWaypoint);
            routeRequest.Options = new RouteOptions();
            routeRequest.Options.RoutePathType = RoutePathType.Points;
            routeRequest.UserProfile = new Utils.WP7.Bing.BingRoute.UserProfile();
            routeRequest.UserProfile.DistanceUnit = Utils.WP7.Bing.BingRoute.DistanceUnit.Kilometer;

            var routeClient = new RouteServiceClient("BasicHttpBinding_IRouteService");
            routeClient.CalculateRouteCompleted += new EventHandler<CalculateRouteCompletedEventArgs>(OnRouteComplete);
            routeClient.CalculateRouteAsync(routeRequest);
        }
    }


   private void OnRouteComplete(object sender, CalculateRouteCompletedEventArgs e)
    {
        if (e.Result != null && e.Result.Result != null && e.Result.Result.Legs != null & e.Result.Result.Legs.Any())
        {
            var result = e.Result.Result;
            var legs = result.Legs.FirstOrDefault();

            StartPoint = legs.ActualStart;
            EndPoint = legs.ActualEnd;
            RoutePoints = result.RoutePath.Points;
            Itinerary = legs.Itinerary;

     //Centre = StartPoint;
             Getcenter = string.Format("{0},{1}", StartPoint.Latitude.ToString(), StartPoint.Longitude.ToString());

            RaiseRouteResolved();
        }
     }

以下是我的 .xaml.cs 页面中的代码,稍后是我的 xaml 页面中的代码。

 (DataContext as RouteViewModel).ResolveRouteFromCurrent();



<maps:Map x:Name="RMaps" Center="{Binding Getcenter}" ZoomLevel="5" CredentialsProvider="{StaticResource MapCredentials}">

我也尝试过绑定 GeoCoordinate 类型的“Centre”(在上面的代码中注释),但这并不能解决我的问题。有人可以让我知道我能够解决这个问题的方式......在此先感谢。

4

1 回答 1

0

您的类需要实现INotifyPropertyChanged,并且您的Getcenter属性需要PropertyChangedEvent在值更改时引发,以便数据绑定在值更新时起作用。

于 2012-05-29T12:13:03.713 回答