public ExtendedVehicle(dynamic returnedVehicle, VehiclesProvider vehiclesProvider, int index)
            {
                lastUpdatingMomentOnThisBase = DateTime.Now;
                OptimalRoute.GeoCoords coords = new OptimalRoute.GeoCoords(Math.Round((double)returnedVehicle.lat / 1000000, 4), Math.Round((double)returnedVehicle.lon / 1000000, 4));
                DateTime lastTimeMoment       = DateTime.Parse((string)returnedVehicle.lasttime);
                double   speed   = (double)returnedVehicle.speed;
                int      routeId = (int)returnedVehicle.rid;

                //Route route = null;
                foreach (ExtendedRoute r in extendedRoutes[index])
                {
                    if (r.idForvard == routeId || r.idReverse == routeId)
                    {
                        id = (string)returnedVehicle.id;//!!!!!!!!!!!! было int
                        bool ok = true;
                        foreach (ExtendedVehicle v in extendedVehicles[index])
                        {
                            if (v.id == id)
                            {
                                ok = false;
                                break;
                            }
                        }
                        if (ok)
                        {
                            Vehicle addingVehicle = new Vehicle(r.route, vehiclesProvider);
                            vehicle = addingVehicle;

                            vehicles.TryAdd(id, addingVehicle);
                        }
                        break;
                    }
                }
            }
            public ExtendedStation(dynamic returnedStation)
            {
                id     = (int)returnedStation.id;
                name   = (string)returnedStation.name;
                descr  = (string)returnedStation.descr;
                coords = new OptimalRoute.GeoCoords(Math.Round((double)returnedStation.lat / 1000000, 4), Math.Round((double)returnedStation.lng / 1000000, 4));
                double  findedMinDistance = 1000000000;
                Station candidate         = null;

                foreach (Station s in allStations)
                {
                    double tmpDist = OptimalRoute.GeoCoords.Distance(coords, s.Coords);
                    if (tmpDist < findedMinDistance)
                    {
                        findedMinDistance = tmpDist;
                        candidate         = s;
                    }
                }
                if (candidate != null && findedMinDistance < 50)
                {
                    station       = candidate;
                    distanceError = findedMinDistance;
                    if (!stations.Contains(candidate))
                    {
                        stations.Add(candidate);
                    }
                }
                else
                {
                    //...
                }
            }
 public SimpleStation(Station s, int intId)
 {
     hashcode = s.hashcode;
     //nameRus = s.nameRus;
     //nameBy = s.nameBy;
     //nameEn = s.nameEn;
     name = s.name;
     //xCoord = s.lat;
     //yCoord = s.lng;
     coords = s.Coords;
     routesCodes = new List<string>();
     if (s.routes != null) foreach (Route r in s.routes) if (r != null) routesCodes.Add(r.hashcode);
 }
            public WayPoint(TimeSpan time, Station station, Route route, OptimalRoute.GeoCoords coords)
            {
                /*try
                 * {*/
                this.time    = time.ToString();
                this.station = station == null ? null : new Station(station.hashcode, station.nameRus, station.nameEn, station.nameBy, (int)(10000 * station.lat), (int)(10000 * station.lng), null, station.name);                                                                                                                                                                                           //station;
                this.route   = route == null ? null : new Route(route.hashcode, JsonConvert.SerializeObject(new string[] { route.from, route.to }), JsonConvert.SerializeObject(new string[] { route.from, route.to }), JsonConvert.SerializeObject(new string[] { route.from, route.to }), JsonConvert.SerializeObject(new string[] { route.from, route.to }), null, route.number, route.type, route.owner); //route;
                this.coords  = coords;

                /*}
                 * catch (Exception ex)
                 * {
                 *
                 * }*/
            }
        public override void Update()
        {
            if (routes == null)
            {
                throw new NullReferenceException();
            }
            Thread[] tmpThreads = new Thread[ApiRequest.apiRequests.Length];
            for (int i = 0; i < ApiRequest.apiRequests.Length; i++)
            {
                List <ExtendedRoute> extendedRoutesCurrentPart = extendedRoutes[i];


metka:
                foreach (ExtendedVehicle ev in extendedVehicles[i])
                {
                    if (DateTime.Now - ev.LastUpdatingMomentOnThisBase > TimeSpan.FromMinutes(3))
                    {
                        RemoveVehicle(ev.vehicle);
                        extendedVehicles[i].Remove(ev);
                        goto metka;
                    }
                }
                List <ExtendedVehicle> extendedVehiclesCurrentPart = extendedVehicles[i];


                ApiRequest currentApiRequest = ApiRequest.apiRequests[i];
                int        index             = i;
                tmpThreads[i] = new Thread(delegate() {
                    StringBuilder tmp = new StringBuilder();
                    foreach (ExtendedRoute r in extendedRoutesCurrentPart)
                    {
                        tmp.Append(r.idForvard + "-0," + r.idReverse + "-0,");
                    }
                    tmp.Remove(tmp.Length - 1, 1);
                    string str = tmp.ToString();
                    //str = "159-0,210-0,208-0,209-0,161-0,160-0,163-0,164-0,211-0,162-0,1-0,2-0,4-0,3-0,140-0,139-0,142-0,141-0,6-0,5-0,8-0,7-0,10-0,9-0,11-0,12-0,14-0,13-0,144-0,143-0,16-0,15-0,18-0,17-0,61-0,62-0,19-0,20-0,73-0,74-0,184-0,183-0,165-0,167-0,166-0,168-0,169-0,170-0,22-0,21-0,146-0,145-0,28-0,27-0,26-0,25-0,147-0,148-0,30-0,29-0,32-0,31-0,136-0,135-0,133-0,134-0,138-0,137-0,34-0,33-0,214-0,173-0,212-0,171-0,213-0,172-0,215-0,175-0,176-0,174-0,97-0,95-0,94-0,99-0,96-0,107-0,106-0,150-0,149-0,189-0,188-0,38-0,37-0,119-0,120-0,121-0,122-0,40-0,39-0,178-0,207-0,204-0,205-0,206-0,182-0,181-0,42-0,41-0,93-0,90-0,89-0,91-0,92-0,124-0,123-0,44-0,43-0,100-0,102-0,101-0,108-0,109-0,151-0,152-0,126-0,125-0,46-0,45-0,128-0,127-0,48-0,47-0,104-0,105-0,103-0,110-0,111-0,50-0,49-0,52-0,51-0,153-0,154-0,54-0,53-0,156-0,155-0,55-0,56-0,157-0,158-0,58-0,57-0,130-0,129-0,60-0,59-0,66-0,65-0,67-0,68-0,132-0,131-0,186-0,185-0,69-0,70-0,72-0,71-0,76-0,75-0,77-0,187-0,78-0,80-0,79-0,82-0,81-0,84-0,83-0,86-0,85-0,87-0,88-0,309-0,308-0";

                    dynamic returnedInfo = Request.SendRequest(currentApiRequest.markersRequest + str);
                    //if (returnedInfo == null) //throw new Exception();
                    //{
                    //    Thread.Sleep(5000);
                    //    returnedInfo = Request.SendRequest(currentApiRequest.markersRequest + str);
                    //    if (returnedInfo == null) throw new Exception();
                    //}
                    while (returnedInfo == null)
                    {
                        Thread.Sleep(GlobalVehiclesProvider.UpdatingPeriodMilliseconds);
                        returnedInfo = Request.SendRequest(currentApiRequest.markersRequest + str);
                    }

                    dynamic objects = returnedInfo.anims;
                    //ExtendedVehicle extendedVehicle = null;
                    DateTime lastUpdatingMoment;
                    OptimalRoute.GeoCoords coords = null;

                    //MessageBox.Show((objects as IEnumerable<dynamic>).Count().ToString());

                    foreach (dynamic obj in objects)
                    {
                        ExtendedVehicle newExtendedVehicle = new ExtendedVehicle(obj, this, index);
                        bool ok            = true;
                        lastUpdatingMoment = DateTime.Parse((string)obj.lasttime);
                        coords             = new OptimalRoute.GeoCoords(Math.Round((double)obj.lat / 1000000, 4), Math.Round((double)obj.lon / 1000000, 4));
                        foreach (ExtendedVehicle v in extendedVehiclesCurrentPart)
                        {
                            if (v.id == newExtendedVehicle.id)
                            {
                                ok = false;
                                //foreach (ExtendedVehicle extendedVehicle in extendedVehiclesCurrentPart)
                                //{
                                //    if (extendedVehicle.id == int.Parse((string)obj.id))
                                //    {
                                Vehicle veh = v.vehicle;        // extendedVehicle.vehicle;
                                if (veh != null)
                                {
                                    vehiclesInfo[veh] = new Group(coords, lastUpdatingMoment);
                                }
                                v.Updated();
                                break;
                                //    }
                                //}
                                //break;
                            }
                        }
                        if (ok)
                        {
                            extendedVehiclesCurrentPart.Add(newExtendedVehicle);
                            newExtendedVehicle.Updated();
                            vehiclesInfo.TryAdd(newExtendedVehicle.vehicle, new Group(coords, lastUpdatingMoment));
                            //vehiclesInfo.Add(newExtendedVehicle.vehicle, new Group(coords, lastUpdatingMoment));
                        }
                    }
                });
                //tmpThreads[i].Start();
            }
            foreach (Thread t in tmpThreads)
            {
                t.Start();
            }
            foreach (Thread t in tmpThreads)
            {
                t.Join();
            }
        }
 public Group(OptimalRoute.GeoCoords coords, DateTime moment)
 {
     this.coords = coords;
     this.moment = moment;
 }