-
Notifications
You must be signed in to change notification settings - Fork 106
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to delete polygons #284
Comments
You are right. It should be same for polygon |
The current BGM wrapper over the JS polygon object is not designed to facilitate fully deleting the object in a way that allows the browser to dispose of the JS object until the map object itself is disposed and deletes all objects attached to it (not sure if an unattached polygon is deleted in that case). So if you're showing a single polygon at a time it's best to merely hide it from the map and then if you want to show another polygon you reuse the object by redefining all the points and adding it back to the map. If you have a // disconnect the JS polygon from the map so that the map stops rendering it.
await userPolygon.SetMap(null); Then to define/redefine the polygon you have something like this: using GoogleMapsComponents.Maps;
private GoogleMap? bgmMap;
private Polygon? userPolygon = null;
// Creates or reuses the userPolygon. Ignores MapPolygonOptions parameter when polygon object already exists.
public async Task SetUserPolygon(MapPolygonOptions mapPolygon, IEnumerable<IEnumerable<LatLngLiteral>> paths)
{
PolygonOptions googlePolygonOptions;
if (mapPolygon != null)
{
googlePolygonOptions = mapPolygon.ToGooglePolygonOptions();
}
else
{
googlePolygonOptions = new PolygonOptions();
}
googlePolygonOptions.Map = bgmMap.InteropObject;
googlePolygonOptions.Paths = paths;
if (userPolygon == null)
{
userPolygon = await Polygon.CreateAsync(JS, googlePolygonOptions);
}
else
{
await userPolygon.SetPaths(paths);
}
} I think actually deleting the JS polygon object and then disposing of the C# reference would involve something like this (untested code): // remove from the map first
await userPolygon.SetMap(null);
// (untested):
// manually invoke the remove function which under the hood eventually calls `delete mapObjects[guid];` in JS.
// Currently it looks like only `EventListeners` will call this on themselves from their
// Dispose(), but there's a TODO comment implying that other managed resources should maybe also do this when disposed.
// It's probably not implemented because there are perf concerns if we would be for example
// calling "remove" for hundreds of map markers at once.
await userPolygon.InvokeAsync("remove") ;
// Dispose the C# JSInterop handle object
await userPolygon.Dispose();
// Optionally set the C# interop object to null so that other code knows it can/should create a new one if needed.
userPolygon = null; |
@christopherdude Does this help for you? @TheAtomicOption i did quick google to investigate how to dispose map object and it related. I am not sure if reusing map object should be library responsibility. It could make other issues. Maybe having as option to reuse could be one way. Note that the above method does not delete the marker. It removes the marker from the map. If instead you wish to delete the marker, you should remove it from the map, and then set the marker itself to null. Closing this one. Discussion related memory could continue on this one |
Is there a way within this interop to delete a polygon after being created? I can't seem to find any implementation of that?
The text was updated successfully, but these errors were encountered: