-1
我在我的用户界面中添加了一张地图,并希望显示从设备当前位置到某个位置(例如餐厅)的路线。我怎样才能做到这一点?如何在地图控件上绘制路线?
我在我的用户界面中添加了一张地图,并希望显示从设备当前位置到某个位置(例如餐厅)的路线。我怎样才能做到这一点?如何在地图控件上绘制路线?
我不知道你在看什么技术,但是如果你想在Windows上看到类似的东西 - UWP平台提供了计算路线的方法,然后将其绘制在地图上。
您需要的是MapControl
和MapRouteFinder
来计算从开始GPS位置到结束GPS位置的路线。从结果中,您可以创建一个MapRouteView
,可以将其添加到MapControl
。请记住检查appmanifest
中的Location
功能。你可以这样做:
// get the user's consent to determine the location
var status = await Geolocator.RequestAccessAsync();
if (status == GeolocationAccessStatus.Allowed)
{
// get the current position
Geolocator locator = new Geolocator() { DesiredAccuracy = PositionAccuracy.High };
Geoposition pos = await locator.GetGeopositionAsync();
BasicGeoposition current = new BasicGeoposition()
{ Latitude = pos.Coordinate.Latitude,
Longitude = pos.Coordinate.Longitude };
// set the lat/long of the restaurant
BasicGeoposition restaurant = new BasicGeoposition()
{ Latitude = 51.91997, Longitude = 4.486515 };
// get the driving route
MapRouteFinderResult route =
await MapRouteFinder.GetDrivingRouteAsync(new Geopoint(current),
new Geopoint(restaurant),
MapRouteOptimization.Time,
MapRouteRestrictions.None);
// get the view and add it to the map
MapRouteView routeView = new MapRouteView(route.Route);
routeView.RouteColor = Windows.UI.Colors.Red;
TheMap.Routes.Add(routeView);
// change the view of the map to see the complete route
await TheMap.TrySetViewBoundsAsync(route.Route.BoundingBox,
null, MapAnimationKind.Default);
}
请分享你试过的什么 – cavpollo
什么地图?你在做什么? iOS版? Android的? HTML网页? Apple Watch?请具体说明。 – Pang