【问题标题】:Xamarin android Google Maps API Calculate and display route and calculate routes distanceXamarin android Google Maps API 计算并显示路线并计算路线距离
【发布时间】:2017-11-08 19:05:58
【问题描述】:

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

I need a result similar like in this picture

【问题讨论】:

    标签: c# android google-maps xamarin.android


    【解决方案1】:

    要显示路线,您首先需要计算它。您可以使用 google Direction API 来执行此操作。很酷的是,它也会返回一个总距离。因此,满足您的所有要求只是一项要求。

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

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

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

    您可以在此链接中播放并更改变量并在浏览器中打开它,以查看返回的对象。

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

    距离将在googleApiRouteObject.routes[0].legs[0].distance; 在那里你会在 meeters 中找到一个 int 表示,以及像 2.3km 这样的字符串表示。

    航点将以折线编码,您需要解析它们。您可以在此处通过代码示例找到如何做到这一点: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;
            }
    

    现在,您需要在地图上绘制它们。我会建议你使用谷歌地图。

        // 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 上非常接近的结果。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    • 1970-01-01
    • 2017-11-03
    • 2020-07-17
    • 2018-12-25
    • 1970-01-01
    相关资源
    最近更新 更多