private bool IsInRegion(Map map, Envelope region)
{
if (region == null)
return true; // region not defined --> we can load the layer without waiting for map extent
if (map == null)
return false;
Envelope extent = map.Extent;
if (extent == null || extent.SpatialReference == null)
return false;
// KML LODS are based on the diagonal size
double regionSize = Math.Sqrt(region.Width * region.Width + region.Height * region.Height);
// Test that current map extent intersects the KML region
if (!region.Intersects(extent))
return false;
// Test the LOD level
double lod = regionSize / map.Resolution;
return !(lod < RegionInfo.MinLodPixels || (lod > RegionInfo.MaxLodPixels && RegionInfo.MaxLodPixels != -1)); // Keep ! to take care of NaN
}