2012-04-04 115 views
1

我的问题是关于使用Bing地图随着Windows Phone 7这是什么,我需要做的更新Silverlight的Bing地图图钉的位置控制

  • 调查服务每x秒检索汇总设置坐标
  • 如果这是第一次将服务进行轮询

    • 情节的这些坐标在地图上的自定义图钉(我用的图片和MapLayer)

      PinObject pin = new PinObject() //Custom object 
             { 
              Id = groupMember.Id, 
              PushPin = new Image() 
              { 
               Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("blackpin.png", UriKind.Relative)), 
               Opacity = 0.8, 
               Stretch = System.Windows.Media.Stretch.None 
              }, 
              PinLocation = new GeoCoordinate(groupMember.Latitude, groupMember.Longitude) 
             }; 
      imageLayer.AddChild(pin.PushPin, pin.PinLocation); //Initialized in constructor 
             pinObjects.Add(pin);// Add pin object to a list to provide a handle to the objects 
      
    • 自动设置地图缩放级别,使所有的绘制点是可见的(我假设使用LocationRect.CreateLocationRect应该做的)

        var coords = pinObjects.Select(p => p.PinLocation).ToList();       
            myMap.SetView(LocationRect.CreateLocationRect(coords)); 
      
    • 其他基于获得的新坐标,更新每个图钉在地图上的位置 PinObject pObj = pinObjects.FirstOrDefault(p => p.Id == groupMember.Id);

      MapLayer.SetPosition(pObj.PushPin, new GeoCoordinate(groupMember.Latitude, groupMember.Longitude)); 
      

引脚加载fiine和调用服务,以获得他们的新位置加载罚款为好,问题是,他们在地图上的位置是永远不会更新,所以基本上他们只是坐在尽管所有这些工作都在后台进行,但我已经调试过,所以我知道它的工作原理。如何重置引脚的位置,如果使用图像不行,我可以使用图钉对象吗?这将如何工作?

在此先感谢。

+0

我不能在你的代码中看到你确定你的位置变化是在UI层上执行的。 – bperreault 2012-04-05 00:19:26

+0

对不起BPerreault,我不明白你的问题。 – 2012-04-05 10:58:55

+0

嗨,所以我使用this.Dispatcher.BeginInvoke(()=> {});但它仍然不会更新地图上的图钉位置 – 2012-04-06 00:41:20

回答

0

我发现确保图钉更新的最佳方法是再次在地图上调用SetView()。您可以传入现有的视图以基本强制刷新。例如; MyMapControl.SetView(MyMapControl.BoundingRectangle);

+0

谢谢MrMDavidson,我已经尝试过,但它仍然无效。我会假设由于引脚位于MapLayer上,所以我们应该更新它或者将它们自己固定,但显然我错了。任何其他想法? – 2012-04-05 15:22:19

+0

我使用'DataTemplate'中创建的'Pushpin'项目将我的图钉绑定到'MapItemsControl'' ItemsSource'属性。推针将“GeoCoordinate”属性绑定到其“Location”属性。在我的回答中提到的属性更新后,我调用了'SetView()',并且它一切正常。你可以尝试发布你的XAML吗? – MrMDavidson 2012-04-08 03:06:33

0

这是一个类似于Dispatcher.BeginInvoke的选项,但它在某些情况下对我更好。当我真的需要通过一些工作离开当前线程时,我将使用私有静态类UICallbackTimer来稍微偏移执行。 (拼写错误和未经测试的代码,只需拔出部分,您必须在代码中进行调试)

UICallbackTimer不是我的代码,但可在Internet上使用。你可以通过搜索“private static class UICallbackTimer”获得关于这个类的信息。

这里是执行它的代码。

UICallbackTimer.DelayExecution(TimeSpan.FromSeconds(.01), 
             () => (

MapLayer.SetPosition(pObj.PushPin,新会有地理座标(groupMember.Latitude,groupMember.Longitude))

);

这里是类(我把它放在当前对象的内部,使之保持私有,以我的课)添加using语句的System.Threading

private static class UICallbackTimer 
    { 
     private static bool _running = false; 
     private static int runawayCounter = 0; 

     public static bool running() 
     { 
      if (_running && runawayCounter++ < 10) 
       return _running; 

      runawayCounter = 0; 
      _running = false; 
      return _running; 
     } 

     public static void DelayExecution(TimeSpan delay, Action action) 
     { 
      _running = true; 
      System.Threading.Timer timer = null; 
      SynchronizationContext context = SynchronizationContext.Current; 
      timer = new System.Threading.Timer(
       (ignore) => 
       { 
        timer.Dispose(); 
        _running = false; 
        context.Post(ignore2 => action(), null); 
       }, null, delay, TimeSpan.FromMilliseconds(-1)); 
     } 
    } 
+0

嗯,有趣!我需要对此做一些修改。感谢您指出它 – 2012-04-13 11:11:49

0

大问题!

这里有一些realllly丑陋的代码,但至少它的工作,并从一开始。

我从here得到了主要结构。我会很感激,如果有人可以发布具有适当绑定和更少代码的答案。

<Grid x:Name="LayoutRoot" Background="Transparent"> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <!--RESOURCES--> 
    <Grid.Resources> 
     <DataTemplate x:Key="LogoTemplate"> 
      <maps:Pushpin Location="{Binding PinLocation}" /> 
     </DataTemplate> 
     <maps:MapItemsControl x:Name="GroupAPins" 
        ItemTemplate="{StaticResource LogoTemplate}" 
        ItemsSource="{Binding PinsA}"> 
     </maps:MapItemsControl> 
    </Grid.Resources> 
    <Grid x:Name="ContentPanel" Grid.Row="0" Margin="12,0,12,0"/> 
</Grid> 

public partial class MapPage : PhoneApplicationPage 
{ 
    private ObservableCollection<PinData> _pinsA = new ObservableCollection<PinData>(); 
    private Map MyMap; 
    public ObservableCollection<PinData> PinsA { get { return this._pinsA; } } 

    public MapPage() 
    { 
     InitializeComponent(); 

     this.DataContext = this; 
     //Create a map. 
     MyMap = new Map(); 
     MyMap.CredentialsProvider = new ApplicationIdCredentialsProvider("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"); 
     //Remove the List of items from the resource and add it to the map 
     this.LayoutRoot.Resources.Remove("GroupAPins"); 
     MyMap.Children.Add(GroupAPins); 
     MyMap.Center = new GeoCoordinate(40.74569634433956, -73.96717071533204); 
     MyMap.ZoomLevel = 5; 
     //Add the map to the content panel. 
     ContentPanel.Children.Add(MyMap); 
     loadAPins_fromString(); 
    } 

    //ADD PIN TO COLLECTION 
    private void addPin(String lat, String lon) 
    { 
     PinData tmpPin; 
     tmpPin = new PinData() 
     { 
      PinLocation = new GeoCoordinate(System.Convert.ToDouble(lat), System.Convert.ToDouble(lon)) 
     }; 
     _pinsA.Add(tmpPin); 

     var timer = new DispatcherTimer(); 
     timer.Interval = TimeSpan.FromSeconds(1); 
     timer.Tick += delegate(object sender, EventArgs args) 
         { 
          PinsA.Remove(tmpPin); 
          tmpPin.PinLocation.Latitude += 1; 
          PinsA.Add(tmpPin); 

         }; 
     timer.Start(); 

    } 

    //LOAD PINS ONE BY ONE 
    private string loadAPins_fromString() 
    { 
     //BAD 
     addPin("42.35960626034072", "-71.09212160110473"); 
     //addPin("51.388066116760086", "30.098590850830067"); 
     //addPin("48.17972265679143", "11.54910385608672"); 
     addPin("40.28802528051879", "-76.65668606758117"); 

     var coords = PinsA.Select(p => p.PinLocation).ToList(); 
     MyMap.SetView(LocationRect.CreateLocationRect(coords)); 

     return "A PINS LOADED - STRING"; 
    } 
} 

public class PinData 
{ 
    public GeoCoordinate PinLocation{get;set;} 
} 
+0

这看起来不错,我会给它一个镜头。尽管我已经在某种程度上已经有了这个工作,但是随着地图缩放级别随着每次调用MyMap.SetView(LocationRect.CreateLocationRect(coords))而降低,它仍然会变得越来越小。只要我能够顺利运行,我一定会回来看看我的工作代码。谢谢 – 2012-04-13 11:10:33