如何刷新Bing地图 [英] How To Refresh a Bing Map

查看:116
本文介绍了如何刷新Bing地图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我们点击地图上的图标时,我有一个显示信息框的Bing地图。

- 我需要定期刷新Bing地图

- 当一个信息框打开,它不应该刷新

I have a Bing Map which shows Info box when we click on an icon on map.
- I need to refresh the Bing map on regular intervals
- When A Info box is open , it should not refresh

推荐答案

Steps:

Create a new WPF project
Add a reference to the WPF Bing Maps Cotrol Beta DLL
Replace the MainWindow.xaml and MainWindow.xaml. vb witht eh code below
Press F5 to run

File: MainWindow.xaml

<window x:class="MainWindow" xmlns:x="#unknown">
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:m="clr-namespace:Microsoft.Maps.MapControl.WPF;assembly=Microsoft.Maps.MapControl.WPF"
    Title="MainWindow" Height="350" Width="525">
    <grid>
        <m:map x:name="MyMap" xmlns:m="#unknown" />
    </grid>
</window>





文件:MainWindow.xaml.vb



类MainWindow



Private Sub MainWindow_Loaded(发送者为对象,e为System.Windows.RoutedEventArgs)处理Me.Loaded



'建立一个ployline并每隔2秒为其添加一个随机坐标



Dim pl As New Microsoft.Maps.MapControl.WPF。 MapPolyline()

pl.Stroke =新的SolidColorBrush(Colors.Black)

pl.StrokeThickness = 5

pl.StrokeLineJoin = PenLineJoin.Round

pl.Locations = New Microsoft.Maps.MapControl.WPF.LocationCollection()



MyMap.Children.Add(pl)



随机()



Dim rnd As New Random()



Dim t As New Timers.Timer(2000)



AddHandler t.Elapsed,Sub()

Dispat cher.BeginInvoke(Sub()

'添加随机点

pl.Locations.Add(New Microsoft.Maps.MapControl.WPF.Location(
$ b) $ b rnd.Next(-180,180),rnd.Next(-180,180)))



'一种解决方法,删除并重新添加折线,我们不再使用它了

'MyMap.Children.Remove(pl)

'MyMap.Children.Add(pl)



'更好的方法,使用Jigg​​leMap

JiggleMap()

End Sub)

End Sub



t.Start()



End Sub



Private Sub JiggleMap()



'我发现最好在后台线程中运行它,以避免

'锁定UI

Dim bw As New ComponentModel.BackgroundWorker()



AddHandler bw.DoWork,Sub(发送者作为对象,e作为ComponentModel.DoWorkEventArgs)

'从参数中检索当前中心

Dim center As Microsoft.Maps.MapControl.WPF.Location = e.Argument



'创建与当前略有不同的新中心

Dim newcentre作为新的Microsoft.Maps.MapControl.WPF.Location(

centre.Latitude + 0.00000000001,centre.Longitude)



'因为我们是在BackgroundWorker中,我们需要使用Dispatcher回调

'UI线程

Dispatcher.Invoke(Sub()

'Chage the地图中心到新的中心

MyMap.Center = newcentre

结束Sub)



'设置一个计时器所以我们可以在短暂休息后重置中心
'否则更改可能在重置之前无法注册

Dim t As New Timers.Timer(125)

t.AutoReset = False


AddHandler t.Elapsed,Sub()

'由于我们在BackgroundWorker中,我们需要回电

'使用Dispatcher到UI线程

Dispatcher.Invoke(Sub()

MyMap.Center = center

End Sub)

结束次级



t.Start()

结束次级



'通过地图的当前中心点

bw.RunWorkerAsync(MyMap.Center)



End Sub



结束班



File: MainWindow.xaml.vb

Class MainWindow

Private Sub MainWindow_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded

' Build a ployline and add random coordinates to it every 2 seconds

Dim pl As New Microsoft.Maps.MapControl.WPF.MapPolyline()
pl.Stroke = New SolidColorBrush(Colors.Black)
pl.StrokeThickness = 5
pl.StrokeLineJoin = PenLineJoin.Round
pl.Locations = New Microsoft.Maps.MapControl.WPF.LocationCollection()

MyMap.Children.Add(pl)

Randomize()

Dim rnd As New Random()

Dim t As New Timers.Timer(2000)

AddHandler t.Elapsed, Sub()
Dispatcher.BeginInvoke(Sub()
' Add random point
pl.Locations.Add(New Microsoft.Maps.MapControl.WPF.Location(
rnd.Next(-180, 180), rnd.Next(-180, 180)))

' One workaround, Remove and re-add polyline, we wont use this anymore
'MyMap.Children.Remove(pl)
'MyMap.Children.Add(pl)

' better method, use JiggleMap
JiggleMap()
End Sub)
End Sub

t.Start()

End Sub

Private Sub JiggleMap()

' I find is best to run this in a background thread so as to avoid
' locking up the UI
Dim bw As New ComponentModel.BackgroundWorker()

AddHandler bw.DoWork, Sub(sender As Object, e As ComponentModel.DoWorkEventArgs)
' Retrieve current centre from arguments
Dim centre As Microsoft.Maps.MapControl.WPF.Location = e.Argument

' Create new centre which is only slightly differnet to current
Dim newcentre As New Microsoft.Maps.MapControl.WPF.Location(
centre.Latitude + 0.00000000001, centre.Longitude)

' Since we are in a BackgroundWorker we need to call back to the
' UI thread using the Dispatcher
Dispatcher.Invoke(Sub()
' Chage the map centre to the new centre
MyMap.Center = newcentre
End Sub)

' Setup a timer so we can reset the centre after a short break
' otherwise the change may not register before it is reset
Dim t As New Timers.Timer(125)
t.AutoReset = False

AddHandler t.Elapsed, Sub()
' Since we are in a BackgroundWorker we need to call back
' to the UI thread using the Dispatcher
Dispatcher.Invoke(Sub()
MyMap.Center = centre
End Sub)
End Sub

t.Start()
End Sub

' Pass in the current centre point of the map
bw.RunWorkerAsync(MyMap.Center)

End Sub

End Class


这篇关于如何刷新Bing地图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆