示例#1
0
        /// <summary>计算距离和驾车时间</summary>
        /// <remarks>
        /// http://lbsyun.baidu.com/index.php?title=webapi/route-matrix-api-v2
        /// </remarks>
        /// <param name="origin"></param>
        /// <param name="destination"></param>
        /// <param name="type">路径计算的方式和方法</param>
        /// <returns></returns>
        public async Task <Driving> GetDistanceAsync(GeoPoint origin, GeoPoint destination, Int32 type = 13)
        {
            if (origin == null || origin.Longitude < 1 && origin.Latitude < 1)
            {
                throw new ArgumentNullException(nameof(origin));
            }
            if (destination == null || destination.Longitude < 1 && destination.Latitude < 1)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (type <= 0)
            {
                type = 13;
            }
            var coord = CoordType;

            if (!coord.IsNullOrEmpty() && coord.Length > 6)
            {
                coord = coord.TrimEnd("ll");
            }
            var url = _distanceUrl.F(origin.Latitude, origin.Longitude, destination.Latitude, destination.Longitude, type, coord);

            var list = await InvokeAsync <IList <Object> >(url, "result");

            if (list == null || list.Count == 0)
            {
                return(null);
            }

            var geo = list.FirstOrDefault() as IDictionary <String, Object>;

            if (geo == null)
            {
                return(null);
            }

            var d1 = geo["distance"] as IDictionary <String, Object>;
            var d2 = geo["duration"] as IDictionary <String, Object>;

            var rs = new Driving
            {
                Distance = d1["value"].ToInt(),
                Duration = d2["value"].ToInt()
            };

            return(rs);
        }
示例#2
0
        /// <summary>计算距离和驾车时间</summary>
        /// <remarks>
        /// http://lbs.amap.com/api/webservice/guide/api/direction
        ///
        /// type:
        /// 0:直线距离
        /// 1:驾车导航距离(仅支持国内坐标)。
        /// 必须指出,当为1时会考虑路况,故在不同时间请求返回结果可能不同。
        /// 此策略和driving接口的 strategy = 4策略一致
        /// 2:公交规划距离(仅支持同城坐标)
        /// 3:步行规划距离(仅支持5km之间的距离)
        ///
        /// distance    路径距离,单位:米
        /// duration    预计行驶时间,单位:秒
        /// </remarks>
        /// <param name="origin"></param>
        /// <param name="destination"></param>
        /// <param name="type">路径计算的方式和方法</param>
        /// <returns></returns>
        public async Task <Driving> GetDistanceAsync(GeoPoint origin, GeoPoint destination, Int32 type = 1)
        {
            if (origin == null || origin.Longitude < 1 && origin.Latitude < 1)
            {
                throw new ArgumentNullException(nameof(origin));
            }
            if (destination == null || destination.Longitude < 1 && destination.Latitude < 1)
            {
                throw new ArgumentNullException(nameof(destination));
            }

            if (type <= 0)
            {
                type = 1;
            }
            var url = _distanceUrl.F(origin.Longitude, origin.Latitude, destination.Longitude, destination.Latitude, type);

            var list = await InvokeAsync <IList <Object> >(url, "results");

            if (list == null || list.Count == 0)
            {
                return(null);
            }

            var geo = list.FirstOrDefault() as IDictionary <String, Object>;

            if (geo == null)
            {
                return(null);
            }

            var rs = new Driving
            {
                Distance = geo["distance"].ToInt(),
                Duration = geo["duration"].ToInt()
            };

            return(rs);
        }