C# 从 Windows Forms 应用程序调用 Google Maps API V3 Javascript API?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1199973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 10:44:09  来源:igfitidea点击:

Invoke Google Maps API V3 Javascript API from Windows Forms app?

c#.netwinformsjavascript

提问by Marc

Google has this fine new Google Maps API v3which is a "Javascript API".

谷歌有这个很好的新谷歌地图 API v3,它是一个“Javascript API”。

Is there any possibility to invoke this API from a Windows Forms application written in Visual C# .net 3.5?

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

EDIT:
The goal is to convert addresses to lat/long format using the Google Maps Geocoder.

编辑:
目标是使用 Google Maps Geocoder 将地址转换为经纬度格式。

EDIT 2:
I'd like to use v3 of the API as it doesn't require an API key anymore. API keys should be bound to a webserver which can't be the case for a Windows Forms app.

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

采纳答案by Richard Szalay

Edit:Looks like the api key is no longer required.

编辑:看起来不再需要 api 密钥。

You can use the REST APIsand parse the response (XML/JSON/CSV).

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

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

Would output:

会输出:

200,6,42.730070,-73.690570

Which is:

这是:

  • 200- G_GEO_SUCCESS
  • 6- Street level accuracy
  • 42.730070- Latitude
  • -73.690570- Longitude
  • 200- G_GEO_SUCCESS
  • 6- 街道水平精度
  • 42.730070- 纬度
  • -73.690570- 经度

If you are not familiar with the System.Net APIs, they would go something like this:

如果您不熟悉 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
}

Obviously there's no error handling, it doesn't support multiple return values and I haven't actually implemented MapGeolocationResult, but it should give you a start.

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

回答by mlarsen

You could host the WebBrowser control on your form and use that to interact with the Google Maps API.

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

But some more information about what you are really trying to do would be nice.

但是,有关您真正想做的事情的更多信息会很好。