Ejemplo n.º 1
0
        public static DrivingResult Parse(string jsonText)
        {
            dynamic jsonResult = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonText);
            dynamic result     = jsonResult.result;

            DrivingResult rlt = new DrivingResult();

            rlt.StartPoint = GetLngLat(result.orig.ToString());
            rlt.EndPoint   = GetLngLat(result.dest.ToString());

            double distance = 0;

            if (result.distance != null)
            {
                double.TryParse(result.distance.ToString(), out distance);
            }
            rlt.Distance = "约" + Math.Round(distance, 2, MidpointRounding.AwayFromZero) + "公里";

            double time = 0;

            if (result.duration != null)
            {
                double.TryParse(result.duration.ToString(), out time);
            }
            rlt.Time = GetTime(time);

            rlt.RoutePath = GetRoutePath(result.routelatlon.ToString());

            rlt.Routes = new List <string>();

            foreach (var item in result.routes.item)
            {
                rlt.Routes.Add(item.strguide.ToString().Trim(',', ',', '。'));
            }

            rlt.CenterInfo = new MapInfo()
            {
                Center = GetLngLat(result.mapinfo.center.ToString()),
                Scale  = (int)result.mapinfo.scale
            };
            return(rlt);
        }
Ejemplo n.º 2
0
        public static List <BusRoute> Parse(string jsonText)
        {
            dynamic         json = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonText);
            List <BusRoute> rlts = new List <BusRoute>();

            try
            {
                dynamic lines = json.results[0].lines;
                foreach (var line in lines)
                {
                    BusRoute busRoute = new BusRoute();
                    busRoute.LineName = line.lineName.ToString();
                    busRoute.BusLines = new List <BusLine>();
                    double totalTime     = 0;
                    double totalDistance = 0;

                    foreach (var segment in line.segments)
                    {
                        BusLine busLine = new BusLine();
                        busLine.Type = (int)segment.segmentType;

                        busLine.StartStation = new Station()
                        {
                            ID    = segment.stationStart.uuid.ToString(),
                            Name  = segment.stationStart.name.ToString(),
                            Point = DrivingResult.GetLngLat(segment.stationStart.lonlat.ToString())
                        };

                        busLine.EndStation = new Station()
                        {
                            ID    = segment.stationEnd.uuid.ToString(),
                            Name  = segment.stationEnd.name.ToString(),
                            Point = DrivingResult.GetLngLat(segment.stationEnd.lonlat.ToString())
                        };

                        var         segmentLine = segment.segmentLine[0];
                        SegmentLine segLine     = new SegmentLine();

                        segLine.ID        = segmentLine.byuuid.ToString();
                        segLine.Direction = segmentLine.direction.ToString();
                        double distance = 0;
                        if (segmentLine.segmentDistance != null)
                        {
                            double.TryParse(segmentLine.segmentDistance.ToString(), out distance);
                        }
                        totalDistance   += distance;
                        segLine.Distance = "约" + (distance > 1000 ? Math.Round(distance / 1000, 2, MidpointRounding.AwayFromZero) + "公里" : Math.Round(distance, 0, MidpointRounding.AwayFromZero) + "米");

                        double segmentTime = 0;
                        if (segmentLine.segmentTime != null)
                        {
                            double.TryParse(segmentLine.segmentTime.ToString(), out segmentTime);
                        }
                        totalTime   += segmentTime;
                        segLine.Time = GetTime(segmentTime);

                        double transferTime = 0;
                        if (segmentLine.segmentTransferTime != null)
                        {
                            double.TryParse(segmentLine.segmentTransferTime.ToString(), out transferTime);
                        }
                        totalTime           += transferTime;
                        segLine.TransferTime = GetTime(transferTime);

                        segLine.WorkTime = segmentLine.SEndTime == null ? string.Empty : segmentLine.SEndTime.ToString();
                        segLine.LineName = segmentLine.lineName == null ? string.Empty : segmentLine.lineName.ToString();

                        var stationCount = segmentLine.segmentStationCount.ToString();
                        segLine.StationCount = string.IsNullOrWhiteSpace(stationCount) ? 0 : int.Parse(stationCount);
                        segLine.RoutePath    = DrivingResult.GetRoutePath(segmentLine.linePoint.ToString());

                        busLine.Line = segLine;
                        busRoute.BusLines.Add(busLine);
                    }
                    busRoute.TotalTime     = GetTime(totalTime);
                    busRoute.TotalDistance = "约" + (totalDistance >= 1000 ? Math.Round(totalDistance / 1000, 2, MidpointRounding.AwayFromZero) + "公里" : Math.Round(totalDistance, 0) + "米");

                    rlts.Add(busRoute);
                }
            }
            catch
            {
                rlts = new List <BusRoute>();
            }
            return(rlts);
        }