用于 C# 的 Google 地图 API [英] Google Maps API for C#

查看:41
本文介绍了用于 C# 的 Google 地图 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对使用 API 真的很陌生,所以在查看 Google Maps API 页面后,我不确定是否有设计用于 C# 的 API.我不需要在我的应用中显示 Google 地图;我只需要知道我是否可以在 C# 上使用 Google Maps API.这是我想要使用的.

I'm really new to using APIs so after looking on Google Maps API page, I'm not sure if there are APIs designed to be used for C#. I don't need a Google map to be shown on my app; all I need to know is if I can use the Google Maps API on C#. This is the one I'd like to use.

我在很多地方寻找过它,但我能找到的只是使用 Gmaps 的替代方法,但这不是我想要的.

I've looked for it on many places but all I could found was alternatives to using Gmaps but that is not what I want.

可以使用吗?

推荐答案

1.自行创建对 Google Maps API 的请求

您只需将 HTTP 请求发送到谷歌地图 API,然后处理结果即可.您可以使用 WebRequest 来伪造请求到谷歌API.为此,您需要一个 Maps API 密钥.

You can just send HTTP-Requests to the google maps API and then work with the results. You could use WebRequest to forge requests to the google api. You need a Maps API Key for that.

您必须建立 GET 参数,例如.origin=...&destination=...&key=YOUR_API_KEY + 解析 JSON 响应以对其进行处理.

You would have to build up the GET parameters eg. origin=...&destination=...&key=YOUR_API_KEY + parse the JSON response to do something with it.

Google Maps Api Documentation(在 Web Service APIs 下)列出了请求参数和示例回复.

Google Maps Api Documentation (under Web Service APIs) lists the request parameters and example responses.

string url = @"https://maps.googleapis.com/maps/api/directions/json?origin=75+9th+Ave+New+York,+NY&destination=MetLife+Stadium+1+MetLife+Stadium+Dr+East+Rutherford,+NJ+07073&key=YOUR_API_KEY";

WebRequest request = WebRequest.Create(url);

WebResponse response = request.GetResponse();

Stream data = response.GetResponseStream();

StreamReader reader = new StreamReader(data);

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

response.Close();

JSON 响应看起来像这样(来自 Directions API)

A JSON response will look something like this (from Directions API )

{
   "geocoded_waypoints" : [
      {
         "geocoder_status" : "OK",
         "place_id" : "ChIJRVY_etDX3IARGYLVpoq7f68",
         "types" : [
            "bus_station",
            "transit_station",
            "point_of_interest",
            "establishment"
         ]
      },
      {
         "geocoder_status" : "OK",
         "partial_match" : true,
         "place_id" : "ChIJp2Mn4E2-woARQS2FILlxUzk",
         "types" : [ "route" ]
      }
   ],
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : 34.1330949,
               "lng" : -117.9143879
            },
            "southwest" : {
               "lat" : 33.8068768,
               "lng" : -118.3527671
            }
         },
         "copyrights" : "Map data ©2016 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "35.9 mi",
                  "value" : 57824
               },
               "duration" : {
                  "text" : "51 mins",
                  "value" : 3062
               },
               "end_address" : "Universal Studios Blvd, Los Angeles, CA 90068, USA",
               "end_location" : {
                  "lat" : 34.1330949,
                  "lng" : -118.3524442
               },
               "start_address" : "Disneyland (Harbor Blvd.), S Harbor Blvd, Anaheim, CA 92802, USA",
               "start_location" : {
                  "lat" : 33.8098177,
                  "lng" : -117.9154353
               },

  ... Additional results truncated in this example[] ...


         "overview_polyline" : {
            "points" : "knjmEnjunUbKCfEA?_@]@kMBeE@qIIoF@wH@eFFk@WOUI_@?u@j@k@`@EXLTZHh@Y`AgApAaCrCUd@cDpDuAtAoApA{YlZiBdBaIhGkFrDeCtBuFxFmIdJmOjPaChDeBlDiAdD}ApGcDxU}@hEmAxD}[tt@yNb\yBdEqFnJqB~DeFxMgK~VsMr[uKzVoCxEsEtG}BzCkHhKWh@]t@{AxEcClLkCjLi@`CwBfHaEzJuBdEyEhIaBnCiF|K_Oz\
            {MdZwAbDaKbUiB|CgCnDkDbEiE|FqBlDsLdXqQra@kX|m@aF|KcHtLm@pAaE~JcTxh@w\`v@gQv`@}F`MqK`PeGzIyGfJiG~GeLhLgIpIcE~FsDrHcFfLqDzH{CxEwAbBgC|B}F|DiQzKsbBdeA{k@~\oc@bWoKjGaEzCoEzEwDxFsUh^wJfOySx[uBnCgCbCoFlDmDvAiCr@eRzDuNxC_EvAiFpCaC|AqGpEwHzFoQnQoTrTqBlCyDnGmCfEmDpDyGzGsIzHuZzYwBpBsC`CqBlAsBbAqCxAoBrAqDdDcNfMgHbHiPtReBtCkD|GqAhBwBzBsG~FoAhAaCbDeBvD_BlEyM``@uBvKiA~DmAlCkA|B}@lBcChHoJnXcB`GoAnIS~CIjFDd]A|QMlD{@jH[vAk@`CoGxRgPzf@aBbHoB~HeMx^eDtJ}BnG{DhJU`@mBzCoCjDaAx@mAnAgCnBmAp@uAj@{Cr@wBPkB@kBSsEW{GV}BEeCWyAWwHs@qH?
            cIHkDXuDn@mCt@mE`BsH|CyAp@}AdAaAtAy@lBg@pCa@jE]fEcBhRq@pJKlCk@hLFrB@lD_@xCeA`DoBxDaHvM_FzImDzFeCpDeC|CkExDiJrHcBtAkDpDwObVuCpFeCdHoIl\uBjIuClJsEvMyDbMqAhEoDlJ{C|J}FlZuBfLyDlXwB~QkArG_AnDiAxC{G|OgEdLaE`LkBbEwG~KgHnLoEjGgDxCaC`BuJdFkFtCgCnBuClD_HdMqEzHcBpB_C|BuEzCmPlIuE|B_EtDeBhCgAdCw@rCi@|DSfECrCAdCS~Di@jDYhA_AlC{AxCcL`U{GvM_DjFkBzBsB`BqDhBaEfAsTvEmEr@iCr@qDrAiFnCcEzCaE~D_@JmFdGQDwBvCeErEoD|BcFjC}DbEuD~D`@Zr@h@?d@Wr@}@vAgCbEaHfMqA`Cy@dAg@bAO`@gCi@w@W"
         },
         "summary" : "I-5 N and US-101 N",
         "warnings" : [],
         "waypoint_order" : []
      }
   ],
   "status" : "OK"
}


2....或使用专用库

  • gmaps-api-net (github) 为 Google 地图提供了 api 接口.您可以通过 nuget 获取它.我还没有测试它,但它看起来非常易于使用.
  • gmaps-api-net (github) offers an api interface for Google Maps. You can get it via nuget. I haven't testet it, but it looks very easy to use.

此库已过时,请参阅https://stackoverflow.com/a/61531795/2881450

这篇关于用于 C# 的 Google 地图 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆