GMap.NET路线返回空值

5

我正在使用C#编写一个程序,需要进行路径计算,但是返回的结果为null。

这是我的代码:

PointLatLng start = new PointLatLng(38.481858, 27.089006);
PointLatLng end = new PointLatLng(38.468447, 27.113793);

MapRoute route = GMap.NET.MapProviders.GoogleMapProvider
                                      .Instance.GetRoute(start, end, false, false, 15);
GMapRoute r = new GMapRoute(route.Points , "My route");
GMapOverlay routeOverlay = new GMapOverlay("route");
routeOverlay.Routes.Add(r);
gMap.Overlays.Add(routeOverlay);
double distance;
distance = route.Distance;

r.Stroke.Width = 2;
r.Stroke.Color = Color.OrangeRed;

我不知道我犯了哪些错误。非常感谢您的任何帮助。


你尝试过传递地址的字符串吗? - nozzleman
@nozzleman,你能给我举个例子吗? - ilke kalkan
什么意思是“没用”?你又得到了空指针异常吗? - sakir
@sakir MapRoute 只接受 MapRoute(string)。 - ilke kalkan
1
@nozzleman,sakir,我找到问题了。最近谷歌移除了路由服务。 - ilke kalkan
显示剩余6条评论
5个回答

5
问题已经解决了。路由返回 null 的原因是 Google 移除了路由服务。

那么替代方案是什么? - Arie

5
GDirections ss;
var xx = GMapProviders.GoogleMap.GetDirections(out ss, start, end, false, false, false, false, false);
GMapRoute r = new GMapRoute(ss.Route, "My route");

尝试一下这个...

我正在尝试获取路线的距离,我不需要方向,但感谢您的建议。 - ilke kalkan
@Vinicious 这段代码对你有用吗?在 CodePlex 论坛上,他们告诉我 Google 已经移除了路由服务。[链接](http://greatmaps.codeplex.com/discussions/642101) - ilke kalkan
在这行代码上出现了“此对象引用未设置为对象实例”的错误:GMapRoute r = new GMapRoute(ss.Route, "My route"); - Arie
这样做仍然出现相同的错误 - ss 最终为 null。 - Matt

1
你的 API 密钥无效 从 NuGet 添加 GMap 使用以下代码:
 public static double GetDistanceByRoute(double startLat, double startLng, double endLat, double endLng)
    {
        GoogleMapProvider.Instance.ApiKey = "Your Api Key";
        PointLatLng start = new PointLatLng(startLat, startLng);
        PointLatLng end = new PointLatLng(endLat, endLng);
        MapRoute route = GMap.NET.MapProviders.GoogleMapProvider.Instance.GetRoute(start, end, false, false, 15);
        return route.Distance;
    }

0
        PointLatLng startp = new PointLatLng(-25.974134, 32.593042);
        PointLatLng endp = new PointLatLng(-25.959048, 32.592827);
        MapRoute route = BingMapProvider.Instance.GetRoute(startp, endp, false, false, 15);
        GMapRoute r = new GMapRoute(route.Points,"Myroutes");
        GMapOverlay routesOverlay = new GMapOverlay("Myroutes");
        routesOverlay.Routes.Add(r);
        gmap.Overlays.Add(routesOverlay);
        r.Stroke.Width = 2;
        r.Stroke.Color = Color.SeaGreen; 

//使用Bing地图提供程序


0

您的API密钥丢失了,我曾经遇到过同样的问题,添加了API密钥后就可以正常工作了。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接