private static void SearchResponse(GClientGeocoder geoCoder, Response response)
            {
                MapPoint[] mapPoints = null;
                Exception  ex        = response.GetException();

                if (ex != null || response.GetCode() != HttpStatusCode.OK)
                {
                    if (geoCoder._listener != null)
                    {
                        geoCoder._listener.done(geoCoder._searchAddress, null);
                    }
                    return;
                }
                try
                {
                    Result result = response.GetResult();
                    result.GetAsString("name");
                    int resultCount = result.GetSizeOfArray("Placemark");
                    if (resultCount > 0)
                    {
                        mapPoints = new MapPoint[resultCount];
                        for (int i = 0; i < resultCount; i++)
                        {
                            mapPoints[i] = new MapPoint
                            {
                                Name = result.GetAsString("Placemark[" + i + "].address")
                            };
                            string    location = result.GetAsString("Placemark[" + i + "].Point.coordinates");
                            GeoLatLng latLng   = MapLayer.FromStringToLatLng(location);
                            mapPoints[i].SetPoint(latLng);
                        }
                        if (geoCoder._addressCache.Count > 24)
                        {
                            int         j    = 0;
                            ICollection keys = geoCoder._addressCache.Keys;
                            foreach (string key1 in keys)
                            {
                                geoCoder._addressCache.Remove(key1);
                                j++;
                                if (j > 12)
                                {
                                    break;
                                }
                            }
                        }
                        geoCoder._addressCache.Add(mapPoints[0].Name, mapPoints[0]);
                    }
                }
                catch (Exception)
                {
                }
                if (geoCoder._listener != null)
                {
                    geoCoder._listener.done(geoCoder._searchAddress, mapPoints);
                }
            }
Esempio n. 2
0
            private void SearchResponse(GDirections gDirection, Response response)
            {
                Exception ex = response.GetException();

                if (ex != null || response.GetCode() != HttpStatusCode.OK)
                {
                    if (gDirection._listener != null)
                    {
                        gDirection._listener.Done(gDirection._routeQuery, null);
                    }
                    return;
                }
                try
                {
                    Result result = response.GetResult();
                    gDirection._mapDirection.Name     = result.GetAsString("name");
                    gDirection._mapDirection.Status   = result.GetAsInteger("Status.code");
                    gDirection._mapDirection.Duration = result.GetAsInteger("Directions.Duration.seconds");
                    gDirection._mapDirection.Distance = result.GetAsInteger("Directions.Distance.meters");
                    gDirection._mapDirection.Summary  = _html2Text.Convert(result.GetAsString("Directions.summaryHtml"));
                    string points     = result.GetAsString("Directions.Polyline.points");
                    string levels     = result.GetAsString("Directions.Polyline.levels");
                    int    zoomFactor = result.GetAsInteger("Directions.Polyline.ZoomFactor");
                    int    numLevels  = result.GetAsInteger("Directions.Polyline.NumLevels");
                    gDirection._mapDirection.Polyline = GeoPolyline.FromEncoded(0x00FF00, 4, 1, points,
                                                                                zoomFactor, levels, numLevels);
                    int numOfGeocodes = result.GetSizeOfArray("Placemark");
                    if (numOfGeocodes > 0)
                    {
                        gDirection._mapDirection.GeoCodes = new MapPoint[numOfGeocodes];
                        for (int i = 0; i < numOfGeocodes; i++)
                        {
                            gDirection._mapDirection.GeoCodes[i]      = new MapPoint();
                            gDirection._mapDirection.GeoCodes[i].Name = result.GetAsString("Placemark[" + i + "].address");
                            string    location = result.GetAsString("Placemark[" + i + "].Point.coordinates");
                            GeoLatLng latLng   = MapLayer.FromStringToLatLng(location);
                            gDirection._mapDirection.GeoCodes[i].SetPoint(latLng);
                        }
                    }
                    int numOfRoutes = result.GetSizeOfArray("Directions.Routes");
                    if (numOfRoutes > 0)
                    {
                        gDirection._mapDirection.Routes = new MapRoute[numOfRoutes];
                        for (int i = 0; i < numOfRoutes; i++)
                        {
                            string routeString = "Directions.Routes[" + i + "]";
                            gDirection._mapDirection.Routes[i]          = MapDirection.NewRoute();
                            gDirection._mapDirection.Routes[i].Summary  = _html2Text.Convert(result.GetAsString(routeString + ".summaryHtml"));
                            gDirection._mapDirection.Routes[i].Distance = result.GetAsInteger(routeString + ".Distance.meters");
                            gDirection._mapDirection.Routes[i].Duration = result.GetAsInteger(routeString + ".Duration.seconds");
                            string lastLatLng = result.GetAsString(routeString + ".End.coordinates");
                            gDirection._mapDirection.Routes[i].LastLatLng = MapLayer.FromStringToLatLng(lastLatLng);
                            int numOfSteps = result.GetSizeOfArray(routeString + ".Steps");
                            if (numOfSteps > 0)
                            {
                                gDirection._mapDirection.Routes[i].Steps = new MapStep[numOfSteps];
                                for (int j = 0; j < numOfSteps; j++)
                                {
                                    string stepString = routeString + ".Steps[" + j + "]";
                                    gDirection._mapDirection.Routes[i].Steps[j]                    = MapRoute.NewStep();
                                    gDirection._mapDirection.Routes[i].Steps[j].Description        = _html2Text.Convert(result.GetAsString(stepString + ".descriptionHtml"));
                                    gDirection._mapDirection.Routes[i].Steps[j].Distance           = result.GetAsInteger(stepString + ".Distance.meters");
                                    gDirection._mapDirection.Routes[i].Steps[j].Duration           = result.GetAsInteger(stepString + ".Duration.seconds");
                                    gDirection._mapDirection.Routes[i].Steps[j].FirstLocationIndex = result.GetAsInteger(stepString + ".polylineIndex");
                                    string firstLocation = result.GetAsString(stepString + ".Point.coordinates");
                                    gDirection._mapDirection.Routes[i].Steps[j].FirstLatLng = MapLayer.FromStringToLatLng(firstLocation);
                                }
                            }
                        }
                    }
                }
                catch (Exception)
                {
                    if (gDirection._listener != null)
                    {
                        gDirection._listener.Done(gDirection._routeQuery, null);
                    }
                    return;
                }
                if (gDirection._listener != null)
                {
                    MapDirection mapDirection = gDirection._mapDirection;
                    if (mapDirection.GeoCodes.Length == mapDirection.Routes.Length + 1)
                    {
                        for (int i = 0; i < mapDirection.Routes.Length; i++)
                        {
                            mapDirection.Routes[i].StartGeocode = mapDirection.GeoCodes[i];
                            mapDirection.Routes[i].EndGeocode   = mapDirection.GeoCodes[i + 1];
                        }
                    }

                    for (int i = 0; i < mapDirection.Routes.Length; i++)
                    {
                        MapRoute mapRoute = mapDirection.Routes[i];
                        for (int j = 0; j < mapRoute.Steps.Length - 1; j++)
                        {
                            MapStep mapStep = mapRoute.Steps[j];

                            mapStep.LastLocationIndex = mapRoute.Steps[j + 1].FirstLocationIndex;
                            mapStep.LastLatLng        = mapDirection.Polyline.GetVertex(mapRoute.Steps[j + 1].FirstLocationIndex);
                        }
                        mapRoute.Steps[mapRoute.Steps.Length - 1].LastLocationIndex =
                            mapDirection.Polyline.GetVertexCount() - 1;
                        mapRoute.Steps[mapRoute.Steps.Length - 1].LastLatLng =
                            mapDirection.Polyline.GetVertex(mapDirection.Polyline.GetVertexCount() - 1);
                    }
                    GeoPolyline polyline = mapDirection.Polyline;
                    if (polyline.GetVertexCount() > 1)
                    {
                        GeoLatLng latLngTemp = polyline.GetVertex(0);
                        mapDirection.Bounds = new GeoLatLngBounds(latLngTemp, latLngTemp);

                        for (int i = 0; i < mapDirection.Routes.Length; i++)
                        {
                            MapRoute mapRoute = mapDirection.Routes[i];
                            latLngTemp      = polyline.GetVertex(mapRoute.Steps[0].FirstLocationIndex);
                            mapRoute.Bounds = new GeoLatLngBounds(latLngTemp, latLngTemp);
                            for (int j = 0; j < mapRoute.Steps.Length; j++)
                            {
                                latLngTemp = polyline.GetVertex(mapRoute.Steps[j].FirstLocationIndex);
                                MapStep mapStep = mapRoute.Steps[j];
                                mapStep.Bounds = new GeoLatLngBounds(latLngTemp, latLngTemp);
                                for (int k = mapStep.FirstLocationIndex; k <= mapStep.LastLocationIndex; k++)
                                {
                                    GeoLatLng latLng = polyline.GetVertex(k);
                                    mapStep.Bounds.Add(latLng.Lng(), latLng.Lat());
                                    mapRoute.Bounds.Add(latLng.Lng(), latLng.Lat());
                                    mapDirection.Bounds.Add(latLng.Lng(), latLng.Lat());
                                }
                            }
                        }
                    }

                    gDirection._listener.Done(gDirection._routeQuery, mapDirection);
                }
            }