Xamarin android Google Maps API计算并显示路线并计算路线距离 [英] Xamarin android Google Maps API Calculate and display route and calculate routes distance

查看:141
本文介绍了Xamarin android Google Maps API计算并显示路线并计算路线距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#开发移动android应用,需要显示路线并计算距离.

I'm working on mobile android app in C# where I need to display a route and calculate it's distance.

我需要类似此图片的结果

推荐答案

要显示路线,首先需要对其进行计算.您可以使用Google Direction API来做到这一点.很酷的事情是,它也将返回您的总距离.因此,满足您的所有需求只是一个请求.

To display a route, you need at first calculate it. You can use google Direction API to do that. The cool thing is, that it will return you a total distance as well. So it's only one request to satisfy all your requirements.

Google Directions API有很多请求示例,您可以通过按名称引用来计算两个地方之间的道路.但最直接的方法是使用经度和纬度.例如:

Google Directions API have a lot of requests examples, you can calculate road between two places by referencing them by name. But the most straightforward is to use a latitude and longitude. For example:

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

您可以从Google控制台获取API密钥.

You can get the API key from a Google console.

您可以播放此链接中的变量并只是对其进行更改,然后在浏览器中将其打开以查看返回的对象.

You can play and just change variables in this link and open it in browser, to see the returning object.

当您收到返回对象时,您将需要对其进行解析.

When you receive the return object, you will need to parse it.

距离将为googleApiRouteObject.routes[0].legs[0].distance; 在那里,您将在聚会者中找到一个int表示形式,以及2.3km之类的字符串表示形式.

The distance will be at googleApiRouteObject.routes[0].legs[0].distance; There you will find a int representation in meeters, and string representation like 2.3km.

航点将以折线进行编码,您需要对它们进行解析.您可以在此处找到代码示例的实现方法: https://developers.google. com/maps/documentation/utilities/polylineutility

The waypoints will be coded in Polylines, you will need to parst them. You can find how to do that with code examples here: https://developers.google.com/maps/documentation/utilities/polylineutility

有一个例子:

List<Android.Locations.Location> FnDecodePolylinePoints(string encodedPoints) 
        {
            if ( string.IsNullOrEmpty ( encodedPoints ) )
                return null;
            var poly = new List<Android.Locations.Location>();
            char[] polylinechars = encodedPoints.ToCharArray();
            int index = 0;

            int currentLat = 0;
            int currentLng = 0;
            int next5bits;
            int sum;
            int shifter;

            try
            {
                while (index < polylinechars.Length)
                {
                    // calculate next latitude
                    sum = 0;
                    shifter = 0;
                    do
                    {
                        next5bits = (int)polylinechars[index++] - 63;
                        sum |= (next5bits & 31) << shifter;
                        shifter += 5;
                    } while (next5bits >= 32 && index < polylinechars.Length);

                    if (index >= polylinechars.Length)
                        break;

                    currentLat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);

                    //calculate next longitude
                    sum = 0;
                    shifter = 0;
                    do
                    {
                        next5bits = (int)polylinechars[index++] - 63;
                        sum |= (next5bits & 31) << shifter;
                        shifter += 5;
                    } while (next5bits >= 32 && index < polylinechars.Length);

                    if (index >= polylinechars.Length && next5bits >= 32)
                        break;

                    currentLng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
                    Android.Locations.Location p = new Android.Locations.Location("");
                    p.Latitude = Convert.ToDouble(currentLat) / 100000.0;
                    p.Longitude = Convert.ToDouble(currentLng) / 100000.0;
                    poly.Add(p);
                } 
            }
            catch 
            {


            }
            return poly;
        }

现在,您将需要在地图上绘制它们.我建议您为此使用Google地图.

Now, you will need to draw them on a map. I'll advice you to use google maps for that.

    // Decode the points
    var lstDecodedPoints = FnDecodePolylinePoints(encodedPoints);
    //convert list of location point to array of latlng type
    var latLngPoints = new LatLng[lstDecodedPoints.Count];
    int index = 0;
    foreach (Android.Locations.Location loc in lstDecodedPoints){
      latLngPoints[index++] = new LatLng(loc.Latitude, loc.Longitude);}
    // Create polyline 
    var polylineoption = new PolylineOptions();
    polylineoption.InvokeColor(Android.Graphics.Color.GRREN);
    polylineoption.Geodesic(true);
    polylineoption.Add(latLngPoints);
    // Don't forget to add it to the main quie, if you was doing the request for a cordinate in background
   // Add polyline to map
    this.Activity.RunOnUiThread(() =>
        _map.AddPolyline(polylineoption));
    }

基本上,您将获得与img上非常接近的结果.

And that basically it, you will get a very close result as on the img.

这篇关于Xamarin android Google Maps API计算并显示路线并计算路线距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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