我真的很新用于使用API所以在查看谷歌地图Api页面后我不确定是否有设计用于C#的API.如果我可以在C#上使用谷歌地图API,我不需要谷歌地图在我的应用程序上显示所有我需要知道. 这是我想要使用的那个
我在很多地方寻找它,但我能找到的只是使用Gmaps的替代品,但那不是我想要的.
¿有可能使用它吗?
1.自己创建对Google Maps API的请求
您只需将HTTP请求发送到Google Maps API,然后使用结果即可.您可以使用WebRequest伪造对google api的请求.您需要 Maps API密钥.
你必须建立GET参数,例如.origin=...&destination=...&key=YOUR_API_KEY
+解析JSON响应以对其执行某些操作.
Google Maps Api文档(在Web服务API下)列出了请求参数和示例响应.
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 )
{ "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.9154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kFtCgCnBuClD_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 Maps提供api界面.你可以通过nuget获得它.我没有测试它,但它看起来很容易使用.