【问题标题】:How to add direction of the route on the google map?如何在谷歌地图上添加路线的方向?
【发布时间】:2018-02-14 11:29:00
【问题描述】:

我在地图上有两个连接点,我想知道哪一个是起点和终点,所以我想添加路线的方向(箭头)。我怎么能用c#做到这一点? 这是我的代码:

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);

【问题讨论】:

  • 可以使用 javascript 解决方案吗?
  • 在路线上添加方向箭头,你也可以使用javascript,因为google为其地图api提供了非常好的javascript资源。
  • 我不能用 c# 来做吗?
  • 使用 C#,在进行 API 调用后,您将不得不努力解决问题。请参阅-stackoverflow.com/questions/34597229/…。至于 javascript,google javascript api 提供了很多代码和资源
  • 我会展示的。感谢您的帮助。

标签: c# google-maps


【解决方案1】:

您需要发送web request 到 google maps api。

为此,您可以按照以下步骤操作:

在 C# 中创建一个 Web 请求,并将 start 和 endpoint 作为参数传递,看看下面的代码 sn-p(也是 refer docs here

这部分要特别注意(这是你需要用到的):

如果您传递坐标,它们将用于计算 方向。确保纬度和纬度之间没有空格 经度值。原点=41.43206,-81.38992

string gMapsUrl = @"https://maps.googleapis.com/maps/api/directions/json?origin=42.252938,42.680411&destination=42.256321,42.675658&key=YOUR_API_KEY";

WebRequest directionReq = WebRequest.Create(gMapsUrl);

WebResponse directionResponse = directionReq.GetResponse();

Stream data = directionResponse.GetResponseStream();

StreamReader reader = new StreamReader(data);

// get json-formatted string from maps api
string responseFromServer = reader.ReadToEnd();

response.Close();

注意我是如何使用它的:

origin=42.252938,42.680411&destination=42.256321,42.675658

在请求 URL 中。

另请参阅此SO post 以获取示例响应 还要在你的课堂上使用using System.Net; 来使用WebRequest

关注SO post 构建webRequests

【讨论】:

  • 我大约一周前开始使用 c# 编码,所以我不太了解它。我在 WebRequest 和 WebResponse 下有红线,我需要添加一些类吗? “使用 System.NET”或类似的东西?
  • 请查看更新的答案,如果这能解决您的问题,请点赞并接受作为答案。
  • 是的,我知道它是如何工作的。我还有一个问题:我可以在谷歌地图上画出方向吗?这里显示了我想要的i.stack.imgur.com/ZMYB7.jpg
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-23
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
  • 2019-08-25
  • 1970-01-01
相关资源
最近更新 更多