在运行时绘制路线
Draw the route at runtime
我有一个带有 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);
}));
}
要做到这一点,第一步是获得用户的位置。如果您使用 JavaScript,您可以使用地理定位 API 来获取用户位置并在他们移动时监控他们。以下是有关如何执行此操作的一些文档:http://www.html5rocks.com/en/tutorials/geolocation/trip_meter/
完成此操作后,您可以很容易地显示用户位置。如果你想在他们的路径上绘制一条多段线,那么你可以做的是第一次抓住用户位置时,你可以创建一个多段线对象,其中第一个和第二个坐标是第一个坐标。然后您可以将多段线添加到地图中。下次抓取用户位置时,您只需将这个新位置添加到折线的位置数组中即可。
我有一个带有 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);
}));
}
要做到这一点,第一步是获得用户的位置。如果您使用 JavaScript,您可以使用地理定位 API 来获取用户位置并在他们移动时监控他们。以下是有关如何执行此操作的一些文档:http://www.html5rocks.com/en/tutorials/geolocation/trip_meter/
完成此操作后,您可以很容易地显示用户位置。如果你想在他们的路径上绘制一条多段线,那么你可以做的是第一次抓住用户位置时,你可以创建一个多段线对象,其中第一个和第二个坐标是第一个坐标。然后您可以将多段线添加到地图中。下次抓取用户位置时,您只需将这个新位置添加到折线的位置数组中即可。