The API itself doesn't offer this functionality, however there is a workaround:
- Get the coordinates for the country border. You can download the coordinates from
http://www.gadm.org/[
^]
- Put them in a PointF array
PointF[] selectedArea = {
new PointF(1.12345f, 3.45678f),
...
}
- Translate the coordinates in the array to points on your visible tile, this will give you a Point[]. I don't know that part by hard, but
this page[
^] has a section on Pixel Coordinates showing you the exact lines of code to translate from coordinates to pixels. I paste it here for reference (in VB):
sinLatitude = sin(latitude * pi/180)
pixelX = ((longitude + 180) / 360) * 256 * (2^level)
pixelY = (0.5 – log((1 + sinLatitude) / (1 – sinLatitude)) / (4 * pi)) * 256 * (2^level)
- Create a polygon path using these points
GraphicsPath polygonPath = new GraphicsPath();
polygonPath.AddPolygon(points);
- Create a region for the entire tile, something like this (the tile size depends on the level of detail, there is a table in the link above for tile sizes for different 'zoom levels'
Region region = new Region(new Rectangle(0, 0, 256, 256));
- Exclude the area you just created.
region.Exclude(polygonPath);
- Fill the region with a color. This will mask out everything outside the country borders
graphics.FillRegion(Brushes.Black, region);
To make it look nicer, you may decide to draw a line using the points array to clearly mark the country border.
graphics.DrawLines(
new System.Drawing.Pen(System.Drawing.Color.FromArgb(255, 255, 0, 0), 3),
points);