public Image GetImageFromCache(GMapType type, Point pos, int zoom, string language) { Image ret = null; try { StringBuilder dir = new StringBuilder(tileCache); dir.AppendFormat("{0}{1}{2}{3}{4}{5}{6}{7}", zoom, Path.DirectorySeparatorChar, language, Path.DirectorySeparatorChar, pos.X, Path.DirectorySeparatorChar, pos.Y, Path.DirectorySeparatorChar); { dir.AppendFormat("{0}.png", type.ToString()); } if(File.Exists(dir.ToString())) { FileInfo f = new FileInfo(dir.ToString()); if(f.Length == 0) { f.Delete(); } else { using(FileStream s = f.Open(FileMode.Open, FileAccess.Read, FileShare.Read)) { ret = Image.FromStream(s); s.Close(); } } } } catch { ret = null; } return ret; }
public void CacheImage(Image tile, GMapType type, Point pos, int zoom, string language) { try { using(tile) { StringBuilder dir = new StringBuilder(tileCache); dir.AppendFormat("{0}{1}{2}{3}{4}{5}{6}{7}", zoom, Path.DirectorySeparatorChar, language, Path.DirectorySeparatorChar, pos.X, Path.DirectorySeparatorChar, pos.Y, Path.DirectorySeparatorChar); string d = dir.ToString(); // precrete dir if(!Directory.Exists(d)) { Directory.CreateDirectory(d); } // save { dir.AppendFormat("{0}.png", type.ToString()); using(FileStream s = File.Open(dir.ToString(), FileMode.Create, FileAccess.Write, FileShare.None)) { tile.Save(s, System.Drawing.Imaging.ImageFormat.Png); s.Flush(); s.Close(); } // remove crap FileInfo f = new FileInfo(dir.ToString()); if(f.Length == 0) { File.Delete(dir.ToString()); } } } } catch { } }
/// <summary> /// gets image from google /// </summary> /// <param name="type"></param> /// <param name="lat"></param> /// <param name="lng"></param> /// <param name="zoom"></param> /// <param name="language"></param> /// <param name="cache"></param> /// <returns></returns> internal Image GetImageFrom(GMapType type, double lat, double lng, int zoom, string language, bool cache) { Point t = FromLatLngToPixel(lat, lng, zoom); Point pos = FromPixelToTileXY(t); Image ret = null; ret = Cache.GetImageFromCache(type, pos, zoom, language); if(ret != null) { return ret; } ret = GetImageFromUrl(MakeImageUrl(type, pos, zoom, language)); if(ret != null && cache) { Cache.CacheImage(ret.Clone() as Image, type, pos, zoom, language); } return ret; }
/// <summary> /// gets image from google /// </summary> /// <param name="type"></param> /// <param name="pos"></param> /// <param name="zoom"></param> /// <param name="language"></param> /// <param name="cache"></param> /// <returns></returns> internal Image GetImageFrom(GMapType type, Point pos, int zoom, string language, bool cache) { Image ret = null; ret = Cache.GetImageFromCache(type, pos, zoom, language); if(ret != null) { return ret; } ret = GetImageFromUrl(MakeImageUrl(type, pos, zoom, language)); if(ret != null && cache) { Cache.CacheImage(ret.Clone() as Image, type, pos, zoom, language); } return ret; }
/// <summary> /// makes url for image /// </summary> /// <param name="type"></param> /// <param name="pos"></param> /// <param name="zoom"></param> /// <param name="language"></param> /// <returns></returns> internal string MakeImageUrl(GMapType type, Point pos, int zoom, string language) { string server = string.Empty; string request = string.Empty; string version = string.Empty; switch(type) { case GMapType.Map: server = "mt"; request = "mt"; version = VersionMap; break; case GMapType.Satellite: server = "khm"; request = "kh"; version = VersionSatellite; break; case GMapType.Labels: server = "mt"; request = "mt"; version = VersionLabels; break; /* case GoogleMapType.SatelliteAndLabels: server = "mt"; request = "mt"; version = VersionLabels; return "don't work yet ;}"; break; */ case GMapType.Terrain: server = "mt"; request = "mt"; version = VersionTerrain; break; } int servernum = (pos.X + 2 * pos.Y) % 4; string sec1 = ""; // after &x=... string sec2 = ""; // after &zoom=... int seclen = ((pos.X*3) + pos.Y) % 8; sec2 = SecWord.Substring(0, seclen); if(pos.Y >= 10000 && pos.Y < 100000) { sec1 = "&s="; } return string.Format("http://{0}{1}.google.com/{2}?v={3}&hl={4}&x={5}{6}&y={7}&z={8}&s={9}", server, servernum.ToString(), request, version, language, pos.X.ToString(), sec1, pos.Y.ToString(), zoom.ToString(), sec2); }
/// <summary> /// makes url for image /// </summary> /// <param name="type"></param> /// <param name="lat"></param> /// <param name="lng"></param> /// <param name="zoom"></param> /// <param name="language"></param> /// <returns></returns> internal string MakeImageUrl(GMapType type, double lat, double lng, int zoom, string language) { Point t = FromLatLngToPixel(lat, lng, zoom); Point p = FromPixelToTileXY(t); return MakeImageUrl(type, p, zoom, language); }
/// <summary> /// get images from list and cache it /// </summary> /// <param name="list"></param> /// <param name="type"></param> /// <param name="zoom"></param> /// <param name="language"></param> /// <returns>successfully downloaded tile count</returns> public int PrecacheTiles(List<Point> list, GMapType type, int zoom, string language) { int countOk = 0; GMapControl.Shuffle<Point>(list); foreach(Point p in list) { Image img = GetImageFrom(type, p, zoom, language, true); if(img != null) { countOk++; System.Threading.Thread.Sleep(1000); } } return countOk; }