Exemplo n.º 1
0
 private void Work()
 {
     Tools.DebugLog("StaticMapService:Work() queue:" + queue.Count);
     if (queue.TryDequeue(out Tuple <int, int, int, int, StaticMapType, StaticMapMode> request))
     {
         Tools.DebugLog($"StaticMapService:Work() request:{request.Item5} {request.Item1}->{request.Item2}");
         int padding_x = 12;
         int padding_y = 12;
         int width     = request.Item3 > 0 ? request.Item3 : 240;
         int height    = request.Item4 > 0 ? request.Item4 : (int)(width / 1.618033);
         int tileSize  = 256;
         List <Tuple <double, double> >         coords = TripToCoords(request);
         Tuple <double, double, double, double> extent = DetermineExtent(coords);
         // calculate center point of map
         double lat_center = (extent.Item1 + extent.Item3) / 2;
         double lng_center = (extent.Item2 + extent.Item4) / 2;
         int    zoom       = 19; // max zoom
         double x_center   = LonToTileX(lng_center, zoom);
         double y_center   = LatToTileY(lat_center, zoom);
         if (request.Item5 == StaticMapType.Trip && coords.Count > 1)
         {
             // trip
             zoom     = CalculateZoom(coords, padding_x, padding_y, width, height, tileSize);
             x_center = LonToTileX(lng_center, zoom);
             y_center = LatToTileY(lat_center, zoom);
         }
         using (Bitmap map = DrawMap(request, width, height, tileSize, coords, zoom, x_center, y_center))
         {
             if (coords.Count == 1 && (request.Item5 == StaticMapType.Park || request.Item5 == StaticMapType.Charge))
             {
                 // park or charge
                 StaticMapIcon icon = StaticMapIcon.Charge;
                 if (request.Item5 == StaticMapType.Park)
                 {
                     icon = StaticMapIcon.Park;
                 }
                 DrawIcon(map, coords.First(), icon, zoom, x_center, y_center, tileSize);
             }
             else if (request.Item5 == StaticMapType.Trip && coords.Count > 1)
             {
                 // trip
                 DrawTrip(map, coords, zoom, x_center, y_center, tileSize);
             }
             else
             {
                 Tools.DebugLog("StaticMapService:Work() request unknown type: " + request.Item5);
                 return;
             }
             try
             {
                 map.Save(FileManager.GetMapCachePath() + $"map_{request.Item1}_{request.Item2}.png.tmp");
                 File.Move(FileManager.GetMapCachePath() + $"map_{request.Item1}_{request.Item2}.png.tmp", FileManager.GetMapCachePath() + $"map_{request.Item1}_{request.Item2}.png");
             }
             catch (Exception ex)
             {
                 Logfile.Log(ex.ToString());
             }
         }
     }
 }
Exemplo n.º 2
0
        private void DrawIcon(Bitmap image, Tuple <double, double> coord, StaticMapIcon icon, int zoom, double x_center, double y_center, int tileSize)
        {
            SolidBrush brush;
            int        scale = 1;

            switch (icon)
            {
            case StaticMapIcon.Charge:
                brush = new SolidBrush(Color.OrangeRed);
                scale = 3;
                break;

            case StaticMapIcon.End:
                brush = new SolidBrush(Color.Green);
                break;

            case StaticMapIcon.Park:
                brush = new SolidBrush(Color.Blue);
                scale = 3;
                break;

            case StaticMapIcon.Start:
                brush = new SolidBrush(Color.Red);
                break;

            default:
                brush = new SolidBrush(Color.White);
                break;
            }
            int       x    = XtoPx(LonToTileX(coord.Item2, zoom), x_center, tileSize, image.Width);
            int       y    = YtoPx(LatToTileY(coord.Item1, zoom), y_center, tileSize, image.Height);
            Rectangle rect = new Rectangle(x - 4 * scale, y - 10 * scale, 8 * scale, 8 * scale);

            Point[] triangle = new Point[] { new Point(x - 4 * scale, y - 6 * scale), new Point(x, y), new Point(x + 4 * scale, y - 6 * scale) };
            using (Graphics g = Graphics.FromImage(image))
            {
                g.SmoothingMode = SmoothingMode.AntiAlias;
                using (Pen whitePen = new Pen(Color.White, 1))
                {
                    g.PixelOffsetMode = PixelOffsetMode.Half;
                    g.FillPie(brush, rect, 180, 180);
                    g.FillPolygon(brush, triangle);
                    g.DrawArc(whitePen, rect, 180, 180);
                    g.DrawLine(whitePen, triangle[0], triangle[1]);
                    g.DrawLine(whitePen, triangle[1], triangle[2]);
                }
                if (icon == StaticMapIcon.Park || icon == StaticMapIcon.Charge)
                {
                    string text = icon == StaticMapIcon.Park ? "P" : "\u26A1";
                    using (Font drawFont = new Font(FontFamily.GenericSansSerif, 12, FontStyle.Bold))
                    {
                        SizeF size = g.MeasureString(text, drawFont);
                        using (SolidBrush textBrush = new SolidBrush(Color.White))
                        {
                            using (StringFormat drawFormat = new StringFormat())
                            {
                                g.DrawString(text, drawFont, textBrush, x - size.Width / 2, y - 6 * scale - size.Height / 2);
                            }
                        }
                    }
                }
            }
            brush.Dispose();
        }