如何使用ImageList修改GMMarker图标?代码调用失效求助
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
Iconproperty ofGMMarker.Itemslikely expects either a file path string (which is why assigning'C:\Test\icon01.png'works flawlessly) or aTIconobject instance. TImageList.GetBitmappulls out aTBitmapobject, which is a completely different type thanTIcon—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

