用于移动目标的 Bing 地图 silverlight 绑定
我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,当一个神秘的“taxiLayer”出现时,你的问题就会变得模糊,当你想在它上面设置一个绑定而不是“点”(我猜它代表一辆车)时,你的问题就会变得模糊。
需要发生的是您使用 MapLayer.Position 依赖属性作为附加属性。当附加的 UIElement 是
MapLayer
地图图层的子级时,它知道如何布局它。因此,问题是如何为该属性分配绑定,以便当绑定对象的值发生更改时,位置会更新。我将假设在代码前面部分创建的 Elipse 可用作我称之为
car
的字段。那么代码可能如下所示:-现在假设您的对象具有
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 aMapLayer
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:-Now assuming you object that has a
CarLocation
property implementINotifyPropertyChanged
so the binding can be alerted whenCarLocation
changes the dot will move appropriately.