简体   繁体   English

调用Google Directions API表单Xamarin.Forms应用

[英]Call Google Directions API form Xamarin.Forms app

I'm trying to set up a basic app to call the Google Directions API from a Xamarin.Forms app in Android. 我正在尝试建立一个基本应用程序,以从Android中的Xamarin.Forms应用程序调用Google Directions API。 I'm new to Xamarin & C# generally, so have struggled searching for the right terms, but am familiar with the directions service from a javascript angle. 我通常是Xamarin&C#的新手,因此一直在努力寻找正确的术语,但是从javascript角度熟悉了路线服务。

I'm using this basic app as a test bed: https://docs.microsoft.com/en-us/xamarin/android/platform/maps-and-location/maps/maps-api 我正在使用此基本应用程序作为测试平台: https : //docs.microsoft.com/zh-cn/xamarin/android/platform/maps-and-location/maps/maps-api

I can add the button and associate a process with it, but how do i call the API & catch the results? 我可以添加按钮并将其与流程关联,但是如何调用API并捕获结果?

I'll worry about plotting etc later, i just want to write out the results to console for now. 我以后会担心绘图等问题,我只想暂时将结果写出到控制台。

Thanks for your help in advance! 谢谢您的帮助!

1.First of all, you can used following URL to get Json data.(Please notice:You need to enable the Directions API in Google console,otherwise, you will get error message This API project is not authorized to use this API. ) 1.首先,您可以使用以下URL获取Json数据。(请注意:您需要在Google控制台中启用Directions API,否则,您将收到错误消息, This API project is not authorized to use this API.

https://maps.googleapis.com/maps/api/directions/json?origin=lat1,lon1&destination=lat2,lon2&key=yourApiKey

In this URL, you need to provide the latitude and longitude of Origin position and destination postion.API key(You can get the API key from a Google console) 在此网址中,您需要提供原始位置和目标位置的纬度和经度。API密钥(您可以从Google控制台获取API密钥)

2.You can play and just change variables in this link and open it in browser, to see the returning object like this screenshot. 2.您可以播放并只需更改此链接中的变量,然后在浏览器中将其打开,即可看到返回的对象,如此屏幕截图。 在此处输入图片说明

3.When you receive the return object, you will need to parse it.(Prase json data: http://bsubramanyamraju.blogspot.com/2017/04/xamarinforms-consuming-rest-webserivce_17.html ) 3,当收到返回对象时,您需要对其进行解析(解析json数据: http ://bsubramanyamraju.blogspot.com/2017/04/xamarinforms-using-rest-webserivce_17.html)

4.The distance will be at googleApiRouteObject.routes[0].legs[0].distance; 4.距离为googleApiRouteObject.routes [0] .legs [0] .distance; There you will find a int representation in meeters, and string representation like 2km. 在那里,您将在聚会者中找到一个int表示形式,以及2km之类的字符串表示形式。

5.The waypoints will be coded in Polylines, you will need to parse them. 5.航点将以折线进行编码,您将需要解析它们。 You can find how to do that with code examples here: https://developers.google.com/maps/documentation/utilities/polylineutility 您可以在此处找到代码示例的实现方法: https//developers.google.com/maps/documentation/utilities/polylineutility

If you want to get the sample code, you can refer to this link. 如果要获取示例代码,可以参考此链接。

https://agileapp.co/xamarin-forms-maps-polyline-route-highlighted-google-api https://agileapp.co/xamarin-forms-maps-polyline-route-highlighted-google-api

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM