【问题标题】:Invoke Google Maps API V3 Javascript API from Windows Forms app?从 Windows 窗体应用程序调用 Google Maps API V3 Javascript API?
【发布时间】:2009-07-29 12:39:57
【问题描述】:

Google 有一个很好的新 Google Maps API v3,它是一个“Javascript API”。

是否有可能从用 Visual C# .net 3.5 编写的 Windows 窗体应用程序调用此 API?

编辑:
目标是使用 Google Maps Geocoder 将地址转换为 lat/long 格式。

编辑 2:
我想使用 API 的 v3,因为它不再需要 API 密钥。 API 密钥应绑定到网络服务器,而 Windows 窗体应用程序则不能。

【问题讨论】:

    标签: c# .net winforms javascript


    【解决方案1】:

    编辑:似乎不再需要 api 密钥。

    您可以使用REST APIs 并解析响应(XML/JSON/CSV)。

    http://maps.google.com/maps/geo?q=State+St,+Troy,+NY&output=csv&oe=utf8&sensor=false
    

    会输出:

    200,6,42.730070,-73.690570
    

    这是:

    • 200 - G_GEO_SUCCESS
    • 6 - 街道级别准确度
    • 42.730070 - 纬度
    • -73.690570 - 经度

    如果您不熟悉 System.Net API,它们会是这样的:

    const string GeoCodeUrlFormat = "http://maps.google.com/maps/geo?q={0}&output=csv&oe=utf8&sensor=false";
    
    string location = "State St, Troy, NY";
    string url = String.Format(GeoCodeUrlFormat, HttpUtility.UrlEncode(location));
    
    HttpRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
    HttpResponse response = (HttpResponse)request.GetResponse(); // Can throw an exception
    Stream responseStream = response.GetResponseStream();
    
    using (StreamReader reader = new StreamReader(responseStream)
    {
        string firstLine = reader.ReadLine();
    
        string[] lineParts = firstLine.Split();
    
        GeolocationResult result = GoogleMapper.MapGeolocationResult(lineParts);
    
        // TADA
    }
    

    显然没有错误处理,它不支持多个返回值,我还没有真正实现 MapGeolocationResult,但它应该给你一个开始。

    【讨论】:

    • 很好的回应,谢谢。但是问题的一部分是我想使用 API 的 V3(而不是 v2),因为它不再需要 API 密钥。该应用程序将向各种客户推出,API 密钥应链接到网络服务器,但 Windows 窗体应用程序并非如此......
    • 看起来他们已经删除了对服务的要求,因为我刚刚删除了关键参数并且它返回了正确的数据。
    【解决方案2】:

    您可以在表单上托管 WebBrowser 控件并使用它与 Google Maps API 进行交互。

    但是更多关于你真正想做的事情的信息会很好。

    【讨论】:

    • 你说得对,我已经编辑了这个问题。我还考虑过使用 WebBrowser 控件。但除了保存生成的网页并在之后解析它之外,我不知道如何从中检索值......
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多