You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何使用ImageList修改GMMarker图标?代码调用失效求助

Fixing GMMarker Icon Assignment from TImageList

Got it, let's break down why your imagelist.GetBitmap(1, GMMarker1.Items[markers].Icon); line isn't working, then fix it step by step:

  • The Icon property of GMMarker.Items likely expects either a file path string (which is why assigning 'C:\Test\icon01.png' works flawlessly) or a TIcon object instance.
  • TImageList.GetBitmap pulls out a TBitmap object, which is a completely different type than TIcon—you can't directly assign a bitmap to an icon property, hence the failure.

Here are three reliable solutions tailored to different scenarios:

Solution 1: Convert Bitmap to TIcon and Assign Directly

If your GMMarker accepts TIcon instances for its Icon property, convert the bitmap from your ImageList first:

var
  TempBitmap: TBitmap;
  TempIcon: TIcon;
begin
  TempBitmap := TBitmap.Create;
  TempIcon := TIcon.Create;
  try
    // Extract the target bitmap from the ImageList
    ImageList1.GetBitmap(1, TempBitmap);
    // Convert the bitmap to a usable icon
    TempIcon.Assign(TempBitmap);
    // Assign the icon to your GMMarker item
    GMMarker1.Items[markers].Icon := TempIcon;
  finally
    // Clean up resources to avoid memory leaks
    TempBitmap.Free;
    TempIcon.Free;
  end;
end;

Solution 2: Extract Icon Directly from ImageList (Most Efficient)

Skip the bitmap conversion entirely by using TImageList.GetIcon to pull a TIcon directly from your ImageList:

var
  TempIcon: TIcon;
begin
  TempIcon := TIcon.Create;
  try
    // Grab the icon directly from the ImageList
    ImageList1.GetIcon(1, TempIcon);
    // Assign it to your GMMarker item
    GMMarker1.Items[markers].Icon := TempIcon;
  finally
    TempIcon.Free;
  end;
end;

Solution 3: Save Icon to Temporary File (If GMMarker Only Accepts File Paths)

If your GMMarker only supports file paths for the Icon property, save the extracted icon to a temporary file and assign that path:

uses
  System.IOUtils;

var
  TempIcon: TIcon;
  TempIconPath: string;
begin
  TempIcon := TIcon.Create;
  try
    ImageList1.GetIcon(1, TempIcon);
    // Generate a temporary .ico file path
    TempIconPath := TPath.Combine(TPath.GetTempPath, 'temp_marker_icon.ico');
    // Save the icon to the temporary file
    TempIcon.SaveToFile(TempIconPath);
    // Assign the file path to your GMMarker item
    GMMarker1.Items[markers].Icon := TempIconPath;
    // Optional: Delete the temporary file later if you don't need it anymore
    // DeleteFile(TempIconPath);
  finally
    TempIcon.Free;
  end;
end;

A quick check of your GMMarker component's documentation will confirm which approach fits best—most modern map marker components accept TIcon instances directly, so Solution 2 is probably your fastest fix.

内容的提问来源于stack exchange,提问作者Marcelo Pomarolli

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:30:48