我在地图上有两个连接点,我想知道哪个是起点和终点,所以我想添加路线的方向(箭头)。我如何使用 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);
Run Code Online (Sandbox Code Playgroud)
您需要发送一个web request到谷歌地图API。
为此,您可以按照以下步骤操作:
在 C# 中创建一个 Web 请求并将起始点和端点作为参数传递,请查看下面的代码片段(另请参阅此处的文档)
特别注意这部分(这是你需要使用的):
如果您传递坐标,它们将不加改变地用于计算方向。确保纬度和经度值之间不存在空格。原点=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();
Run Code Online (Sandbox Code Playgroud)
注意我是如何使用这个的:
origin=42.252938,42.680411&destination=42.256321,42.675658
Run Code Online (Sandbox Code Playgroud)
在请求 URL 中。
另请参阅此SO 帖子以获取示例响应也可using System.Net;在您的班级中使用WebRequest
按照此SO 帖子进行构建webRequests
| 归档时间: |
|
| 查看次数: |
952 次 |
| 最近记录: |