在运行时绘制路线

时间:2015-02-05 16:31:40

标签: bing-maps

我有一个Bing地图的应用程序,我需要绘制用户的路线。 现在我只能用图钉跟踪用户位置。是否有一些绘制路线的方法?

更新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);
        }));
    }

1 个答案:

答案 0 :(得分:0)

要做到这一点,第一步是获得用户的位置。如果您使用的是JavaScript,则可以使用Geolocation API获取用户位置,并在移动时对其进行监控。以下是有关如何执行此操作的一些文档:http://www.html5rocks.com/en/tutorials/geolocation/trip_meter/

完成此操作后,您可以非常轻松地显示用户位置。如果你想绘制一条折线,那么你可以做的就是第一次抓住用户的位置你可以创建一个折线对象,其中第一个和第二个坐标是第一个坐标。然后,您可以将折线添加到地图中。下次抓取用户位置时,您只需将此新位置添加到折线的位置阵列即可。