路径规划 最后更新时间: 2021年01月22日
驾车路径规划
该功能实现在两点之间驾车路径的计算,origin、destination 为起点和终点,是必选项,同时可以设置途径点、避免区域、提供多种驾车策略、还可以将兴趣点id作为起点和终点等,详细的参数说明参见参考手册。AMapNavigationSearch 中提供了三个 DrivingNavigation 的重载函数,示例代码(详见demo中CarDrivingNavigation.xaml.cs)如下:
private async void GetNavigationDriving(LatLng start, LatLng end)
{
//请求查询,origin、destination为起点和终点,是必选项
AMapRouteResults rts = await AMapNavigationSearch.DrivingNavigation(start.longitude, start.latitude, end.longitude, end.latitude, Extensions.All);
if (rts.Erro == null)
{
if (rts.Count == 0)
{
MessageBox.Show("无查询结果");
return;
}
AMapRoute route = rts.Route;
List paths = route.Paths.ToList();
List lnglats = new List();
foreach (AMapPath item in paths)
{
Debug.WriteLine("起点终点距离:" + item.Distance);
Debug.WriteLine("预计耗时:" + item.Duration / 60 + "分钟");
Debug.WriteLine("导航策略:" + item.Strategy);
//画路线
List steps = item.Steps.ToList();
foreach (AMapStep st in steps)
{
Debug.WriteLine(st.Instruction);
lnglats = latLagsFromString(st.Polyline);
amap.AddPolyline(new AMapPolylineOptions()
{
Points = latLagsFromString(st.Polyline),
Color = Color.FromArgb(255, 0, 0, 255),
Width = 4,
});
}
}
}
else
{
MessageBox.Show(rts.Erro.Message);
}
}
步行路径规划
该功能实现在两点之间步行路径的计算,origin、destination 为起点和终点,是必选项。AMapNavigationSearch 中提供了两个 WalkingNavigation 的重载函数,示例代码(详见demo中WalkingNavigation.xaml.cs)如下:
private async void GetNavigationWalking(LatLng start, LatLng end)
{
//请求查询,origin、destination为起点和终点,是必选项
AMapRouteResults rts = await AMapNavigationSearch.WalkingNavigation(start.longitude, start.latitude, end.longitude, end.latitude,0);
if (rts.Erro == null)
{
if (rts.Count == 0)
{
MessageBox.Show("无查询结果");
return;
}
AMapRoute route = rts.Route;
List paths = route.Paths.ToList();
List lnglats = new List();
foreach (AMapPath item in paths)
{
Debug.WriteLine("起点终点距离:" + item.Distance);
Debug.WriteLine("预计耗时:" + item.Duration / 60 + "分钟");
Debug.WriteLine("导航策略:" + item.Strategy);
//画路线
List steps = item.Steps.ToList();
foreach (AMapStep st in steps)
{
Debug.WriteLine(st.Instruction);
lnglats = latLagsFromString(st.Polyline);
amap.AddPolyline(new AMapPolylineOptions()
{
Points = latLagsFromString(st.Polyline),
Color = Color.FromArgb(255, 0, 0, 255),
Width = 4,
});
}
}
}
else
{
MessageBox.Show(rts.Erro.Message);
}
}
公交路径规划
该功能实现在两点之间公交路径的计算,起点(origin)、终点(destination)以及城市(city),是必选项。AMapNavigationSearch 中提供了两个 BusNavigation 的重载函数,示例代码(详见demo中BusIntegratedNavigation.xaml.cs)如下:
private async void GetIntegratedNavigation(LatLng start, LatLng end, string city)
{
//请求查询
AMapRouteResults rts = await AMapNavigationSearch.BusNavigation(start.longitude, start.latitude, end.longitude, end.latitude, 0, false, city);
if (rts.Erro == null)
{
if (rts.Count == 0)
{
MessageBox.Show("无查询结果");
return;
}
AMapRoute route = rts.Route;
List transits = route.Transits.ToList();
int i = 0;
foreach (AMapTransit tr in transits)
{
i++;
}
Debug.WriteLine("Transit返回结果数:" + i);
List segments = transits.FirstOrDefault(p => p.Segments != null).Segments.ToList();
AMapWalking walking = segments.FirstOrDefault(p => p.Walking != null).Walking;
int s = 0;
foreach (AMapStep sp in walking.Steps)
{
s++;
amap.AddPolyline(new AMapPolylineOptions()
{
Points = latLagsFromString(sp.Polyline ),
Color = Color.FromArgb(255, 0, 0, 255),
Width = 4,
});
}
Debug.WriteLine("Step返回结果数:" + s);
int b = 0;
foreach (AMapBusLine buslines in segments.FirstOrDefault().BusLine)
{
b++;
amap.AddPolyline(new AMapPolylineOptions()
{
Points = latLagsFromString(buslines.Polyline ),
Color = Color.FromArgb(255, 120, 43, 255),
Width = 4,
});
}
Debug.WriteLine("BusLine返回结果数:" + b);
}
else
{
MessageBox.Show(rts.Erro.Message);
}
}