2015-02-05 63 views
0

我有一个应用程序与Bing地图,我需要绘制用户的路线dinamically。 现在我只能用图钉追踪用户的位置。有没有一种方法来绘制路线?在运行时绘制路线

更新1: 我使用这个处理程序分配给geolocator.positionChanged:

private void geolocator_DrawRoute(Geolocator sender, PositionChangedEventArgs args) 
    { 
     // Need to get back onto UI thread before updating location information 
     this.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(
     () => 
     { 
      //Get the current location 
      Location location = new Location(args.Position.Coordinate.Point.Position.Latitude, 
       args.Position.Coordinate.Point.Position.Longitude); 
      _rotta.Add(location); 
      if (_rotta.Count > 1) 
      { 
       var polyline = new MapPolyline { Locations = _rotta, Color = Colors.Blue, Width = 3 }; 
       _shapeLayer.Shapes.Add(polyline); 
      } 

      //Update the position of the GPS pushpin 
      MapLayer.SetPosition(GpsPushpin, location); 

      //Update the map view to the current GPS location 
      MyMap.SetView(location, 18); 
     })); 
    } 

更新2:

private void geolocator_DrawRoute(Geolocator sender, PositionChangedEventArgs args) 
    { 
     // Need to get back onto UI thread before updating location information 
     Dispatcher.RunAsync(CoreDispatcherPriority.Normal, new DispatchedHandler(
     () => 
     { 
      //Get the current location 
      var location = new Location(args.Position.Coordinate.Point.Position.Latitude, args.Position.Coordinate.Point.Position.Longitude); 
      _rottaLoc.Add(location); 
      if (_rotta == null) 
      { 
       _rotta = new MapPolyline { Locations = _rottaLoc, Color = Colors.Blue, Width = 4 }; 
      } 
      else 
      { 
       _rotta.Locations = _rottaLoc; 
      } 

      _shapeLayer.Shapes.Add(_rotta); 

      //Update the position of the GPS pushpin 
      MapLayer.SetPosition(GpsPushpin, location); 

      //Update the map view to the current GPS location 
      MyMap.SetView(location, 18); 
     })); 
    } 
+0

您想在用户在地图上移动时绘制线条,即追踪他们的路线吗?或者你想绘制一条行车路线并显示沿着该路线的用户位置? – rbrundritt 2015-02-06 17:38:03

+0

@rbrunditt我想画一条随着用户移动的折线!我可以使用geolocator的PositionChanged事件吗? – 2015-02-06 17:39:26

+0

是的,你可以使用PositionChanged事件 – rbrundritt 2015-02-10 20:35:14

回答

0

要做到这一点的第一步是获取用户位置。如果您使用JavaScript,则可以使用Geolocation API获取用户位置并在移动时监视它们。这里有一些关于如何做到这一点的文档:http://www.html5rocks.com/en/tutorials/geolocation/trip_meter/

一旦你完成了这一步,你可以很容易地显示用户的位置。如果你想绘制一条多段线来让它们的路径一致,那么你可以做的就是第一次抓住用户的位置,你可以创建一个多段线对象,其中第一个和第二个坐标是第一个坐标。然后,您可以将折线添加到地图。下一次抓取用户位置时,您可以简单地将此新位置添加到折线的位置数组中。

+0

用户位置不断被抓住。我有一个START按钮和一个END按钮。当用户点击开始时,我开始绘制跟踪,直到用户单击停止。 – 2015-02-06 17:57:40

+0

对,看看我提供的链接的监视器用户位置部分。 – rbrundritt 2015-02-06 21:07:16

+0

该代码是JavaScript,但我使用C#..请参阅第一篇文章中的update1。它可以工作吗? – 2015-02-07 10:46:48