在谷歌静态地图中使用自定义图标

时间:2018-05-26 05:31:05

标签: google-static-maps

我需要在Google地图中使用不同条件下的自定义图标。在我的程序中有一个生成此图像的功能。在某些情况下,来自Google的照片是正确的,在某些情况下,默认图标会放在地图上。

生成的网址:

  

http://maps.googleapis.com/maps/api/staticmap?center=29.627650000000000,52.518450000000000&zoom=18&size=500x500&maptype=roadmap&markers=icon:http://example.com/Testsvg/icons4?angle=45|29.627500000000000,52.518300000000000&markers=icon:http://example.com/Testsvg/icons4?angle=45|29.627800000000000,52.518600000000000&key= myKey

我的应用程序在正确模式下生成的照片( 64 * 34,1.1 KB ):

Photo generated in my application in correct mode (**64*34, 1.1 KB**)

Google以正确模式返回的照片:

Photo returned by Google in correct mode

我的应用程序在错误模式下生成的照片( 64 * 64,3.5 KB ):

Photo generated in my application in wrong mode (**64*64, 3.5 KB**)

Google以错误模式返回的照片:

Photo returned by Google in wrong mode

public FileResult Icons4(float angle)
    {
        try
        {
            //It changes according to the conditions of this route
            string path = "myPath";
            Bitmap orgbmp = (Bitmap)Image.FromFile(path);

            Bitmap bmp = CropTranslate(orgbmp);

            Bitmap tempRotatedImage = new Bitmap((bmp.Width * 2) + 10, (bmp.Height * 2) + 10, PixelFormat.Format32bppArgb);
            using (Graphics g = Graphics.FromImage(tempRotatedImage))
            {
                g.Clear(Color.Transparent);
                g.DrawImage(bmp, new Point(((int)(bmp.Width / 2) + 3), ((int)(bmp.Height / 2)) + 3));
            }

            Bitmap rotatedImage = new Bitmap(tempRotatedImage.Width, tempRotatedImage.Height);
            using (Graphics g = Graphics.FromImage(rotatedImage))
            {
                g.TranslateTransform(tempRotatedImage.Width / 2, tempRotatedImage.Height / 2);
                g.RotateTransform(angle);
                g.TranslateTransform(-tempRotatedImage.Width / 2, -tempRotatedImage.Height / 2);
                g.DrawImage(tempRotatedImage, new Point(0, 0));
            }

            Bitmap croped = CropTranslate(rotatedImage);
            double wid = rotatedImage.Width;
            double hig = rotatedImage.Height;
            if (wid > 64 || hig > 64)
            {

                if (croped.Width > croped.Height)
                {
                    wid = 64;
                    hig = (int)(64 / wid) * 64;
                }
                else
                {
                    wid = (int)((64 / hig) * 64);
                    hig = 64;
                }
            }

            Size newSize = new Size((int)wid , (int)hig );
            Bitmap newBmp = new Bitmap(croped, newSize);

            ImageConverter converter = new ImageConverter();
            var bytes = (byte[])converter.ConvertTo(newBmp, typeof(byte[]));
            return File(bytes, System.Net.Mime.MediaTypeNames.Application.Octet, "test.png");

        }
        catch (Exception x)
        {
            WriteLog("cntrlr" + GetException(x));
        }
        return null;
    }



private Bitmap CropTranslate(Bitmap bm)
    {
        int minX = 0;
        int maxX = 0;
        int minY = 0;
        int maxY = 0;

        int transparent = 0;//Alpha code of translucent is 0
        int translucent = 254;//Alpha code of translucent is 1-254

        for (int i = 0; i < bm.Width; i++)
        {
            bool trnsprnt = true;
            for (int j = 0; j < bm.Height; j++)
            {
                byte alphaValue = bm.GetPixel(i, j).A;
                if (alphaValue == transparent)
                { }
                else if (alphaValue <= translucent)
                { }
                else
                { trnsprnt = false; break; }
            }
            if (trnsprnt)
            {
                minX = i < 6 ? 0 : i - 5;
                //maxX = i;
            }
            else
            {
                break;
            }
        }
        for (int i = bm.Width - 1; i >= 0; i--)
        {
            bool trnsprnt = true;
            for (int j = 0; j < bm.Height; j++)
            {
                byte alphaValue = bm.GetPixel(i, j).A;
                if (alphaValue == transparent)
                { }
                else if (alphaValue <= translucent)
                { }
                else
                { trnsprnt = false; break; }
            }
            if (trnsprnt)
            {
                maxX = i > bm.Width - 6 ? bm.Width : i + 5;
            }
            else
            {
                break;
            }
        }


        for (int i = 0; i < bm.Height; i++)
        {
            bool trnsprnt = true;
            for (int j = 0; j < bm.Width; j++)
            {
                byte alphaValue = bm.GetPixel(j, i).A;
                if (alphaValue == transparent)
                { }
                else if (alphaValue <= translucent)
                { }
                else
                { trnsprnt = false; break; }
            }
            if (trnsprnt)
            {
                minY = i < 6 ? 0 : i - 5;
            }
            else
            {
                break;
            }
        }
        for (int i = bm.Height - 1; i >= 0; i--)
        {
            bool trnsprnt = true;
            for (int j = 0; j < bm.Width; j++)
            {
                byte alphaValue = bm.GetPixel(j, i).A;
                if (alphaValue == transparent)
                { }
                else if (alphaValue <= translucent)
                { }
                else
                { trnsprnt = false; break; }
            }
            if (trnsprnt)
            {
                maxY = i > bm.Height - 6 ? bm.Height : i + 5;
            }
            else
            {
                break;
            }
        }

        int width = maxX - minX;
        int height = maxY - minY;

        Rectangle cropRect = new Rectangle(minX, minY, width, height);
        Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);

        using (Graphics g = Graphics.FromImage(target))
        {
            g.DrawImage(bm, new Rectangle(0, 0, target.Width, target.Height),
                             cropRect,
                             GraphicsUnit.Pixel);
        }

        return target;
    }

0 个答案:

没有答案