【问题标题】:unable to bind map geocoordinate center on to map on windows phone through mvvm using bing maps无法使用 bing 地图通过 mvvm 将地图地理坐标中心绑定到 Windows 手机上的地图
【发布时间】:2012-05-29 09:25:13
【问题描述】:

我正在尝试根据给定实例在地图上显示的各种路线动态设置地图控件的中心属性。我在这里使用路线的起点作为地理坐标中心。为了实现这一点,我尝试使用 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”(在上面的代码中注释),但这并不能解决我的问题。有人可以告诉我如何解决这个问题...提前致谢。

【问题讨论】:

    标签: c# xaml mvvm windows-phone-7.1 bing-maps


    【解决方案1】:

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

    【讨论】:

    • 感谢您的回答,但它并没有解决问题。另一个问题是,一旦将路线设置为中心,我就无法设置地图的缩放级别...您能告诉我如何获得它...再次感谢。
    • 添加另一个属性(再次引发NotifyPropertyChanged 事件)并将ZoomLevel 绑定到它
    • 实际上我想根据不同路线的距离可能不同的路线动态设置缩放级别..即长或短并且不能使所有路线的缩放级别都相同。那么,有什么方法可以实现它...如果是,请告诉我...谢谢。
    • 这种情况下你可以ask the map to zoom to fit
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多