用于移动目标的 Bing 地图 silverlight 绑定

发布于 2024-11-15 14:28:23 字数 1223 浏览 2 评论 0原文

我想在 Bing 地图上制作一个“汽车”点的动画。当物品四处移动时,我可以轻松地绘制多个点,但我希望每辆车都有一个点移动。

XAML

    <m:Map Name="myMap" Grid.Row="2" MouseClick="myMap_MouseClick" UseInertia="True">
    <m:MapLayer x:Name="carLayer" />
    </m:Map>

一些代码:

private void AddCarDot(double latitude, double longitude)
{
    Ellipse point = new Ellipse();
    point.Width = 15;
    point.Height = 15;
    point.Fill = new SolidColorBrush(Colors.Blue);
    point.Opacity = 0.65;
    Location location = new Location(latitude, longitude);
    MapLayer.SetPosition(point, location);
    MapLayer.SetPositionOrigin(point, PositionOrigin.Center);

    carLayer.Children.Add(point);
}

private void cmbCar_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(cmbCar.SelectedItem != null)
            {
                Binding binding = new Binding("CarLocation");
                binding.Source = cmbCar.SelectedItem;
                binding.Mode = BindingMode.OneWay;
                carLayer.SetBinding(MapLayer.PositionProperty, binding);
            }
        }

CarLocation 是 Location 类型的 Car 对象的属性。 然而,这不起作用,我不太确定如何让“汽车”在地图上移动。有人能指出我正确的方向吗?

I want to animate a "car" dot on a Bing map. I can easily draw multiple dots as the item travels around, but I want to have a single dot move around per car.

XAML

    <m:Map Name="myMap" Grid.Row="2" MouseClick="myMap_MouseClick" UseInertia="True">
    <m:MapLayer x:Name="carLayer" />
    </m:Map>

Some code:

private void AddCarDot(double latitude, double longitude)
{
    Ellipse point = new Ellipse();
    point.Width = 15;
    point.Height = 15;
    point.Fill = new SolidColorBrush(Colors.Blue);
    point.Opacity = 0.65;
    Location location = new Location(latitude, longitude);
    MapLayer.SetPosition(point, location);
    MapLayer.SetPositionOrigin(point, PositionOrigin.Center);

    carLayer.Children.Add(point);
}

private void cmbCar_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if(cmbCar.SelectedItem != null)
            {
                Binding binding = new Binding("CarLocation");
                binding.Source = cmbCar.SelectedItem;
                binding.Mode = BindingMode.OneWay;
                carLayer.SetBinding(MapLayer.PositionProperty, binding);
            }
        }

The CarLocation is a property on the Car object of type Location.
However that does not work and I'm not quite sure how to get the "car" to move around the map. Can someone point me in the right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一片旧的回忆 2024-11-22 14:28:23

好吧,当一个神秘的“taxiLayer”出现时,你的问题就会变得模糊,当你想在它上面设置一个绑定而不是“点”(我猜它代表一辆车)时,你的问题就会变得模糊。

需要发生的是您使用 MapLayer.Position 依赖属性作为附加属性。当附加的 UIElement 是 MapLayer 地图图层的子级时,它知道如何布局它。

因此,问题是如何为该属性分配绑定,以便当绑定对象的值发生更改时,位置会更新。我将假设在代码前面部分创建的 Elipse 可用作我称之为 car 的字段。那么代码可能如下所示:-

private Elipse AddCarDot(object source)
{
    Ellipse point = new Ellipse();
    point.Width = 15;
    point.Height = 15;
    point.Fill = new SolidColorBrush(Colors.Blue);
    point.Opacity = 0.65;
    MapLayer.SetPositionOrigin(point, PositionOrigin.Center);
    point.SetBinding(MapLayer.PositionProperty, new Binding("CarLocation") {Source = source});
    carLayer.Children.Add(point);
}

private void cmbCar_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if(cmbCar.SelectedItem != null)
    {
        AddCarDot(cmbCar);
    }
}

现在假设您的对象具有 CarLocation 属性,实现 INotifyPropertyChanged,以便在 CarLocation 时绑定可以收到警报更改后该点将适当移动。

Well you're question kind gets cloudly when a mysterious "taxiLayer" appears positively gets muddy when you want set a binding on it instead of the "point" (which I guess represents a car).

What needs to happen is you are using the MapLayer.Position dependency property as an attached property. When the UIElement to which this is attached is a child of a MapLayer map layer knows how to layout it.

So the question is how would assign a binding to this property so that when the value of the bound object changes the position is updated. I'm going to make an assumption the Elipse created in the earlier part of the code is available as field I'll call car. Then the code might look something like this:-

private Elipse AddCarDot(object source)
{
    Ellipse point = new Ellipse();
    point.Width = 15;
    point.Height = 15;
    point.Fill = new SolidColorBrush(Colors.Blue);
    point.Opacity = 0.65;
    MapLayer.SetPositionOrigin(point, PositionOrigin.Center);
    point.SetBinding(MapLayer.PositionProperty, new Binding("CarLocation") {Source = source});
    carLayer.Children.Add(point);
}

private void cmbCar_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if(cmbCar.SelectedItem != null)
    {
        AddCarDot(cmbCar);
    }
}

Now assuming you object that has a CarLocation property implement INotifyPropertyChanged so the binding can be alerted when CarLocation changes the dot will move appropriately.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文