【问题标题】:Google java client api direction api is giving wrong route谷歌 java 客户端 api 方向 api 给出了错误的路线
【发布时间】:2016-05-11 13:28:18
【问题描述】:

我正在使用 Google 的 JAVA 客户端 API 来获取路由,并且我正在发送这样的请求,

   DirectionsRoute[] routes = DirectionsApi.newRequest(context)
                              .mode(TravelMode.DRIVING)
                              .origin(start).destination(end)
                              .waypoints(wayPoints).await();

它也是返回路线,但如果我绘制该路线,它不会绘制在实际路线上,而是如图所示采用直线。 如何解决?

【问题讨论】:

标签: java google-maps google-chrome directions google-directions-api


【解决方案1】:

实际上我在那个请求之后犯了一个错误,我得到了带有路由的结果,但它还包含由 lat lang 数组组成的编码器折线对象,一旦我们解码,我们就会得到所有的点,这样我们可以正确获取路线。 结果由路线数组组成,每个元素由腿数组(两点之间的路线详细信息)组成,每条腿由步骤组成,最后每个步骤都对折线进行了编码,以便获得正确的 latlang,您应该解码该折线并使用它。

【讨论】:

    【解决方案2】:

    非常感谢,你帮了我很多,我目前正在使用xamarin并使用这种方法来解码折线。

            private List<LatLng> DecodePolyline(string encodedPoints)
        {
            if (string.IsNullOrWhiteSpace(encodedPoints))
            {
                return null;
            }
    
            int index = 0;
            var polylineChars = encodedPoints.ToCharArray();
            var poly = new List<LatLng>();
            int currentLat = 0;
            int currentLng = 0;
            int next5Bits;
    
            while (index < polylineChars.Length)
            {
                // calculate next latitude
                int sum = 0;
                int shifter = 0;
    
                do
                {
                    next5Bits = 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 = 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);
    
                var mLatLng = new LatLng(Convert.ToDouble(currentLat) / 100000.0, Convert.ToDouble(currentLng) / 100000.0);
                poly.Add(mLatLng);
            }
    
            return poly;
        }
    

    【讨论】:

      猜你喜欢
      • 2015-08-24
      • 2020-12-03
      • 1970-01-01
      • 2017-10-28
      • 1970-01-01
      • 1970-01-01
      • 2020-05-16
      • 2019-06-21
      • 2017-07-28
      相关资源
      最近更新 更多