in SupportingScripts/Runtime/Scripts/MapRenderer/MapPin/MapPinSpatialIndex.cs [63:133]
internal void AddMapPin(MapPin mapPinToAdd)
{
mapPinToAdd.LocationChanged += MapPinLocationChanged;
var latLon = mapPinToAdd.Location;
// Insert into max LOD.
TileId maxLodTileId;
{
var lodIndex = _maxLod.Value - 1;
maxLodTileId = new TileId(latLon, _maxLod);
if (!_tiledSpatialIndex[lodIndex].TryGetValue(maxLodTileId.Value, out TileData maxLodTileData))
{
maxLodTileData =
new TileData
{
MapPins = new List<MapPin> { mapPinToAdd }
};
_tiledSpatialIndex[lodIndex].Add(maxLodTileId.Value, maxLodTileData);
}
else
{
maxLodTileData.MapPins.Add(mapPinToAdd);
}
maxLodTileData.MapPinCount++;
maxLodTileData.TotalLat += latLon.LatitudeInDegrees;
maxLodTileData.TotalLon += latLon.LongitudeInDegrees;
}
// Bubble up tile into parent LODs.
maxLodTileId.TryGetParent(out var parentTileId);
var parentLodIndex = parentTileId.CalculateLevelOfDetail().Value - 1;
while (parentLodIndex >= 0)
{
if (!_tiledSpatialIndex[parentLodIndex].TryGetValue(parentTileId.Value, out var parentTileData))
{
// This is a new tile. Create and add a new TileData for this LOD.
parentTileData =
new TileData
{
MapPins = new List<MapPin> { mapPinToAdd },
MapPinCount = 1,
TotalLat = latLon.LatitudeInDegrees,
TotalLon = latLon.LongitudeInDegrees
};
_tiledSpatialIndex[parentLodIndex].Add(parentTileId.Value, parentTileData);
}
else
{
// We already have a tile with points or clusters.
// In either case, track the LatLong.
parentTileData.MapPinCount++;
parentTileData.TotalLat += latLon.LatitudeInDegrees;
parentTileData.TotalLon += latLon.LongitudeInDegrees;
var isCluster = _isClusteringEnabled && parentTileData.MapPinCount > ClusterThreshold;
if (isCluster)
{
parentTileData.MapPins = null;
}
else
{
parentTileData.MapPins.Add(mapPinToAdd);
}
}
parentLodIndex--;
parentTileId.TryGetParent(out parentTileId);
}
}