2014-08-28 33 views
2

我一直致力于通过串行端口获取来自GPS(L76)开发板的NMEA数据的GPS地图绘制应用程序。 CSV数据被解析并且经度/纬度信息被保存在队列中。目标是在位置信息通过串行端口可用时,在基于WPF的地图应用程序上动态显示图钉。在并发环境中更新Bing Map地图

Dispatcher.Invoke(() => 
{ 
    XAMLMap.CredentialsProvider = new ApplicationIdCredentialsProvider("the key"); 
    XAMLMap.Center = new Microsoft.Maps.MapControl.WPF.Location(50.8, -1.12222); 

    Pushpin currentPushpin = new Pushpin(); 
    currentPushpin.Location = new Microsoft.Maps.MapControl.WPF.Location(50.8, -1.12222); 
    XAMLMap.ZoomLevel = 18; 
    XAMLMap.Margin = new Thickness(243, 151, 0, 0); 
    XAMLMap.Children.Add(currentPushpin); 
    mapGrid.Children.Add(XAMLMap); 
}); 

然而,当推针位置坐标是从内的串行数据动态地生成的显示的实际挑战:

<WPF:Map Name="XAMLMap" ZoomLevel="17" CredentialsProvider="the key" Mode="road" Margin="241,157,10.334,10"> 
</WPF:Map> 

应用程序成功通过下面给出的代码将显示其初始秉地图显示以下事件方法:

void serialPortHandler_NewSerialDataRecieved(object sender, SerialDataEventArgs e) 
{ 
    AddLocation(currentLocation); 
} 

private void AddLocation(Location location) 
{ 
    Dispatcher.Invoke(() => 
    { 
     try 
     {       
      Microsoft.Maps.MapControl.WPF.Location currentLocation = new Microsoft.Maps.MapControl.WPF.Location(location.latitude, location.longitude); 

      Pushpin currentPushpin = new Pushpin(); 
      currentPushpin.Location = currentLocation;  
      XAMLMap.Margin = new Thickness(243, 151, 0, 0);       
      XAMLMap.Children.Add(currentPushpin); 
      mapGrid.Children.Add(XAMLMap);  
     } 
     catch (Exception exc) 
     {  
     } 
    }); 
} 

更新后的坐标纬度/经度值仍然正确放置在地图上(XAMLMap)。然而,当地图被缩小/放入或平移时,地图内容(即道路,地理细节,分辨率)完全不更新,即根本不更新新可见地图信息,留下不规则块的拼图。

此外,请注意,问题与我将XAMLMap对象添加到上面的代码或任何其他子代中的网格的方式无关。

我已经研究了各种可能性来解决这个问题,但到目前为止一直没有成功。这还包括:

  1. 保持该映射应用程序通过一个单独的线程
  2. 使用定时器控制,并使用Peek方法返回并从队列

任何帮助显示最顶层的位置对象或在这方面的方向将受到高度赞赏。

回答

2

它看起来像你的代码不断添加地图到网格。您应该在XAML中创建一次映射,并仅在其中设置凭据。不需要在代码中设置凭据。我也不确定为什么在添加图钉时更改地图的边距。不需要该代码。

您AddLocation方法应该是这个样子:

private void AddLocation(Location location) 
{ 
    Dispatcher.Invoke(() => 
    { 
     try 
     {       
Microsoft.Maps.MapControl.WPF.Location currentLocation = new Microsoft.Maps.MapControl.WPF.Location(location.latitude, location.longitude); 

      Pushpin currentPushpin = new Pushpin(); 
      currentPushpin.Location = currentLocation;       
      XAMLMap.Children.Add(currentPushpin); 
     } 
     catch (Exception exc) 
     {  
     } 
    }); 
}