//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 18JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * Computes the pixel coordinates of the given geographical point . * @param latLng latitude,longitude pair of give point * @param zoomLevel current zoom level * @return the pixel coordinates. */ public static GeoPoint FromLatLngToPixel(GeoLatLng latLng, int zoomLevel) { //double latitude = latLng.Lat(); //double longitude = latLng.Lng(); //double power = 8 + zoomLevel; //double mapsize = MathEx.Pow(2, power); //double origin = mapsize / 2; //double longdeg = MathEx.Abs(-180 - longitude); //double longppd = mapsize / 360; //double longppdrad = mapsize / (2 * Math.PI); //double pixelx = longdeg * longppd; //double e = MathEx.Sin(latitude * (1 / 180.0 * MathEx.PI)); //if (e > 0.9999) //{ // e = 0.9999; //} //if (e < -0.9999) //{ // e = -0.9999; //} //double pixely = origin + 0.5 * MathEx.Log2((1 + e) / (1 - e)) * (-longppdrad); //return new GeoPoint(pixelx, pixely); int pixelX, pixelY; TileSystem.LatLongToPixelXY(latLng.Lat(), latLng.Lng(), zoomLevel, out pixelX, out pixelY); return(new GeoPoint(pixelX, pixelY)); }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 18JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * return screen boundary in geo coordinates. * @param pt the center of the screen. * @return screen boundary in geo coordinates. */ public GeoLatLngBounds GetScreenBounds(GeoLatLng pt) { lock (_syncObject) { GeoPoint center = FromLatLngToPixel(pt, _mapZoomLevel); int shiftWidth = _screenSize.Width; GeoPoint topLeft = new GeoPoint(center.X - _screenSize.Width / 2.0 - shiftWidth, center.Y - _screenSize.Height / 2.0 - _screenSize.Height); GeoPoint bottomRight = new GeoPoint(center.X + _screenSize.Width / 2.0 + shiftWidth, center.Y + _screenSize.Height / 2.0 + _screenSize.Height); GeoLatLng topLeftLatLng = FromPixelToLatLng(topLeft, _mapZoomLevel); GeoLatLng bottomRightLatLng = FromPixelToLatLng(bottomRight, _mapZoomLevel); double minY = Math.Min(bottomRightLatLng.Lat(), topLeftLatLng.Lat()); double maxY = Math.Max(bottomRightLatLng.Lat(), topLeftLatLng.Lat()); double minX = Math.Min(bottomRightLatLng.Lng(), topLeftLatLng.Lng()); double maxX = Math.Max(bottomRightLatLng.Lng(), topLeftLatLng.Lng()); return(new GeoLatLngBounds(minX, minY, maxX - minX, maxY - minY)); } }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 18JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * Get the image at given X,Y zoom level. * @param X X index of the map tile * @param Y Y index of the map tile. * @param zoomLevel zoom level of the map tile * @return the given image. */ public IImage GetImage(int x, int y, int zoomLevel) { MapDirection mapDirection = GetMapDirection(); if (mapDirection != null) { try { const int shiftWidth = 4; GeoPoint pt1 = new GeoPoint(x * MapLayer.MAP_TILE_WIDTH - shiftWidth, y * MapLayer.MAP_TILE_WIDTH - shiftWidth); GeoPoint pt2 = new GeoPoint((x + 1) * MapLayer.MAP_TILE_WIDTH + shiftWidth, (y + 1) * MapLayer.MAP_TILE_WIDTH + shiftWidth); GeoLatLng latLng1 = MapLayer.FromPixelToLatLng(pt1, zoomLevel); GeoLatLng latLng2 = MapLayer.FromPixelToLatLng(pt2, zoomLevel); double minY = Math.Min(latLng1.Lat(), latLng2.Lat()); double maxY = Math.Max(latLng1.Lat(), latLng2.Lat()); double minX = Math.Min(latLng1.Lng(), latLng2.Lng()); double maxX = Math.Max(latLng1.Lng(), latLng2.Lng()); GeoLatLngBounds geoBounds = new GeoLatLngBounds(minX, minY, maxX - minX, maxY - minY); GeoLatLng centerPt = geoBounds.GetCenter(); _mapDirectionLayer.SetCenter(centerPt, zoomLevel); _mapDirectionLayer._screenBounds = geoBounds; _mapTileGraphics.SetColor(TRANSPARENCY); _mapTileGraphics.FillRect(0, 0, MapLayer.MAP_TILE_WIDTH, MapLayer.MAP_TILE_WIDTH); _mapDirectionLayer.Paint(_mapTileGraphics); IsImagevalid = true; if (_readListener != null) { _readListener.readProgress(16, 16); } return(_mapTileImage.ModifyAlpha(160, TRANSPARENCY)); } catch (Exception) { } } return(null); }
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); } }
//////////////////////////////////////////////////////////////////////////// //--------------------------------- REVISIONS ------------------------------ // Date Name Tracking # Description // --------- ------------------- ------------- ---------------------- // 21JUN2009 James Shen Initial Creation //////////////////////////////////////////////////////////////////////////// /** * @inheritDoc */ public override void GetImage(int mtype, int x, int y, int zoomLevel) { lock (VectorMapAbstractCanvas.GRAPHICS_MUTEX) { int shiftWidth = 32; GeoPoint pt1 = new GeoPoint(x * MapLayer.MAP_TILE_WIDTH - shiftWidth, y * MapLayer.MAP_TILE_WIDTH - shiftWidth); GeoPoint pt2 = new GeoPoint((x + 1) * MapLayer.MAP_TILE_WIDTH + shiftWidth, (y + 1) * MapLayer.MAP_TILE_WIDTH + shiftWidth); GeoLatLng latLng1 = MapLayer.FromPixelToLatLng(pt1, zoomLevel); GeoLatLng latLng2 = MapLayer.FromPixelToLatLng(pt2, zoomLevel); double minY = Math.Min(latLng1.Lat(), latLng2.Lat()); double maxY = Math.Max(latLng1.Lat(), latLng2.Lat()); double minX = Math.Min(latLng1.Lng(), latLng2.Lng()); double maxX = Math.Max(latLng1.Lng(), latLng2.Lng()); double width = 0.00; double height = 0.00; //width = width < 0.06 ? 0.06 : width; //height = height < 0.06 ? 0.06 : height; GeoLatLngBounds geoBounds = new GeoLatLngBounds(minX - width / 2.0, minY - height / 2.0, maxX - minX + width, maxY - minY + height); try { Hashtable[] mapFeatures = _geoSet.Search(geoBounds); int totalSize = 0; for (int i = 0; i < mapFeatures.Length; i++) { Hashtable mapFeaturesInLayer = mapFeatures[i]; totalSize += mapFeaturesInLayer.Count; } totalSize += 1; int mapObjectIndex = 0; _vectorMapCanvas.ClearCanvas(0xffffff); for (int i = 0; i < mapFeatures.Length; i++) { int zOrder = mapFeatures.Length - 1 - i; Hashtable mapFeaturesInLayer = mapFeatures[zOrder]; ICollection enuKeys = mapFeaturesInLayer.Keys; MapFeatureLayer mapLayer = _geoSet.GetMapFeatureLayer(zOrder); foreach (var o in enuKeys) { int mapInfoID = (int)o; MapFeature mapFeature = mapLayer .GetMapFeatureByID(mapInfoID); mapObjectIndex++; _vectorMapCanvas.SetFont(GetFont(mapLayer.FontName)); _vectorMapCanvas.SetFontColor(mapLayer.FontColor); _vectorMapCanvas.DrawMapObject(mapFeature.MapObject, geoBounds, zoomLevel); if (_readListener != null) { _readListener.readProgress(mapObjectIndex, totalSize); } } } _vectorMapCanvas.DrawMapText(); ImageArray = PNGEncoder.GetPngrgb(MapLayer.MAP_TILE_WIDTH, MapLayer.MAP_TILE_WIDTH, _vectorMapCanvas.GetRGB()); ImageArraySize = ImageArray.Length; IsImagevalid = true; if (ImageArraySize == 1933) { ImageArray = null; IsImagevalid = false; ImageArraySize = 0; } if (_readListener != null) { _readListener.readProgress(totalSize, totalSize); } } catch (Exception e) { } } }