谷歌地图集成,获取测量距离



你好,我正在WinForms中开发运行日志。我想知道是否有人有一些好的资源来集成谷歌地图。最终目标是让用户双击"距离"列中的单元格,它将打开一个新表单。新表单上会有一个谷歌地图,用户可以使用谷歌的距离测量工具追踪路线。接受该路线将关闭窗口,并在单元格中输入追踪路线的距离(英里)。

我对谷歌地图的集成是完全陌生的,如果它真的可以与C#应用程序集成的话。请告诉我正确的方向,在那里我可以找到一些示例代码来实现我的目标。

提前谢谢。

在项目中使用此类来获取两个位置之间的距离您可以从获取Newtonsoft.Json.Linqhttps://www.nuget.org/packages/newtonsoft.json/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json.Linq;
namespace Management
{
    class DeliveryMapInfo
    {
    string origin = @""; //Write the address of the origin here
    public DeliveryMapInfo() { }
    public int getDistanceTo(string destination)
    {
        System.Threading.Thread.Sleep(1000);
        int distance = -1;
        string url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false";
        string requesturl = url;string content = fileGetJSON(requesturl);
        JObject _Jobj = JObject.Parse(content);
        try
        {
            distance = (int)_Jobj.SelectToken("routes[0].legs[0].distance.value");
            return distance / 1000;// Distance in KMS
        }
        catch
        {
            return distance / 1000;
        }
    }
    protected string fileGetJSON(string fileName)
    {
        string _sData = string.Empty;
        string me = string.Empty;
        try
        {
            if (fileName.ToLower().IndexOf("http:") > -1)
            {
                System.Net.WebClient wc = new System.Net.WebClient();
                byte[] response = wc.DownloadData(fileName);
                _sData = System.Text.Encoding.ASCII.GetString(response);
            }
            else
            {
                System.IO.StreamReader sr = new System.IO.StreamReader(fileName);
                _sData = sr.ReadToEnd();
                sr.Close();
            }
        }
        catch { _sData = "unable to connect to server "; }
        return _sData;
    }
}

}

您的想法很大程度上取决于您想要使用什么API,即距离矩阵、方向、地理编码等。注意:"/directions/"我发现方向是我使用的最完整的api,因为它可以进行地理编码、查找距离、查找持续时间。。。等但是,directions JSON文件的大小比其他JSON文件大。运用你的判断。

string url = "http://maps.googleapis.com/maps/api/directions/json?origin=" + origin + "&destination=" + destination + "&sensor=false";

distance = (int)_Jobj.SelectToken("routes[0].legs[0].distance.value");

例如:

Directions JSON文件中的结构是:

-Routes
    - Bound
        - NorthEast
        - SouthWest
    - Copyrights
    - Legs
        - Steps
            - duration
            - distance
            - endlocation
            - maneuver
            - htmlinstructions
            - polyline
            - startlocation
            - travelmode
        - Distance
            - text
            - value
        - Duration
            - text
            - value
        - StartAddress
        - EndAddress
        - EndLocation
            - lat
            - lng
        - StartLocation
            - lat
            - lng
    - Overview Polyline
    - Summary
    - Warnings
    - Waypoint Order
- Status

您将需要使用谷歌地图API。值得庆幸的是,这非常简单,并且使用了JSON,它得到了很好的支持。

看看:http://code.google.com/apis/maps/documentation/distancematrix/

不幸的是,谷歌会在你输入的两点之间选择最佳路线,如果你没有运行最快的路线,这就没有多大用处。

要解决此问题,请查看此处:http://code.google.com/apis/maps/documentation/directions/

这允许您添加路点,因此可能更有用,但您可能需要用户自己创建这些路点。

最后要记住的是,如果你希望收到大量请求,谷歌现在正在向使用这些服务的开发者收费。作为非商业用户,您每天只能收到2500个请求,每个请求最多8个路点。

最新更新