-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