如何使用c#在谷歌地图上绘制彩色路线?

时间:2017-09-01 07:27:06

标签: c# google-maps routes

我正在制作c#应用程序。我使用谷歌地图,我想画两条不同颜色的路线。例如:第一个我想要红色,第二个是绿色,但它们都是绿色的。 我想更改第二种颜色,以便第一种颜色 stay the same

这是我的代码:

        PointLatLng start1 = new PointLatLng(42.252938, 42.680411);
        PointLatLng end1 = new PointLatLng(42.256321, 42.675658);
        GDirections dir1;
        var path1 = GMapProviders.GoogleMap.GetDirections(out dir1, start1, end1, false, false, true, true, true);
        GMapRoute route1 = new GMapRoute(dir1.Route, "path1");
        route1.Stroke.Color = Color.Red;
        GMapOverlay lay1 = new GMapOverlay("route1");
        lay1.Routes.Add(route1);
        map.Overlays.Add(lay1);


        PointLatLng start2 = new PointLatLng(42.259188, 42.670733);
        PointLatLng end2 = new PointLatLng(42.259617, 42.673362);
        GDirections dir2;
        var path2 = GMapProviders.GoogleMap.GetDirections(out dir2, start2, end2, false, false, true, true, true);
        GMapRoute route2 = new GMapRoute(dir2.Route, "path2");
        route2.Stroke.Color = Color.Green;
        GMapOverlay lay2 = new GMapOverlay("route2");
        lay2.Routes.Add(route2);
        map.Overlays.Add(lay2);

1 个答案:

答案 0 :(得分:1)

不要试图将'route.Stroke.Color'设置为颜色,而是将'route.Stroke'设置为新的Pen(Color.Red)(例如)。

route1.Stroke = new System.Drawing.Pen(Color.Red);

这应该可行。

注意**(我认为它必须是一个'新'对象,因为绘制它的图形对象会引用您指向它的静态颜色对象,然后在更改路径时更改了此引用。 Stroke.Color再次使他们都画了绿色?!)