private void RemoveMapPin()

in SupportingScripts/Runtime/Scripts/MapRenderer/MapPin/MapPinSpatialIndex.cs [140:221]


        private void RemoveMapPin(MapPin mapPinToRemove, LatLon locationOverride)
        {
            // Find the MapPin in the spatial index at the max LOD.
            var lodIndex = _maxLod.Value - 1;
            var maxLodTileId = new TileId(locationOverride, _maxLod);
            var indexChanged = false;
            if (_tiledSpatialIndex[lodIndex].TryGetValue(maxLodTileId.Value, out TileData maxLodTileData))
            {
                if (maxLodTileData.MapPins.Remove(mapPinToRemove))
                {
                    indexChanged = true;

                    mapPinToRemove.LocationChanged -= MapPinLocationChanged;

                    maxLodTileData.MapPinCount--;
                    if (maxLodTileData.MapPinCount == 0)
                    {
                        // Remove tile if now empty.
                        _tiledSpatialIndex[lodIndex].Remove(maxLodTileId.Value);
                    }
                    else
                    {
                        maxLodTileData.TotalLat -= locationOverride.LatitudeInDegrees;
                        maxLodTileData.TotalLon -= locationOverride.LongitudeInDegrees;
                    }
                }
            }

            // Bubble up change to parent tiles.
            if (indexChanged)
            {
                maxLodTileId.TryGetParent(out var parentTileId);
                var parentLodIndex = parentTileId.CalculateLevelOfDetail().Value - 1;
                while (parentLodIndex >= 0)
                {
                    _tiledSpatialIndex[parentLodIndex].TryGetValue(parentTileId.Value, out var parentTileData);
                    parentTileData.MapPinCount--;

                    if (parentTileData.MapPinCount == 0)
                    {
                        // No more pins left in this tile. Remove it from the spatial index.
                        _tiledSpatialIndex[parentLodIndex].Remove(parentTileId.Value);

#if DEBUG
                        // It shouldn't be clustered.
                        Assert.IsTrue(parentTileData.ClusterMapPin == null);
#endif
                    }
                    else
                    {
                        parentTileData.TotalLat -= locationOverride.LatitudeInDegrees;
                        parentTileData.TotalLon -= locationOverride.LongitudeInDegrees;

                        var isCluster = _isClusteringEnabled && parentTileData.MapPinCount > ClusterThreshold;
                        if (!isCluster)
                        {
                            if (parentTileData.MapPins != null)
                            {
                                parentTileData.MapPins.Remove(mapPinToRemove);
                            }
                            else
                            {
                                // When we remove the MapPin, this tile will fall under the cluster threshold so we will need to repopulate
                                // the children list.
                                parentTileData.MapPins = GatherChildren(parentTileId);

                                // Destroy the ClusterMapPin game object if it exists.
                                if (parentTileData.ClusterMapPin != null)
                                {
                                    _clusterMapPins.Remove(parentTileData.ClusterMapPin);
                                    UnityEngine.Object.Destroy(parentTileData.ClusterMapPin.gameObject);
                                    parentTileData.ClusterMapPin = null;
                                }
                            }
                        }
                    }

                    parentLodIndex--;
                    parentTileId.TryGetParent(out parentTileId);
                }
            }
        }