Click here to Skip to main content
15,886,362 members
Posted
Comments
Member 10812259 3-Sep-14 6:31am    
First I would like to tell you that i am using windows application so do not suggest me for web ideas,jquery and javascript, I only require c# back end code
thanks
kbrandwijk 3-Sep-14 7:51am    
In that case please add the relevant tags to the question to reflect this.

1 solution

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
C#
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):
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
C#
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'
C#
Region region = new Region(new Rectangle(0, 0, 256, 256));

- Exclude the area you just created.
C#
region.Exclude(polygonPath);

- Fill the region with a color. This will mask out everything outside the country borders
C#
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.
C#
graphics.DrawLines(
new System.Drawing.Pen(System.Drawing.Color.FromArgb(255, 255, 0, 0), 3),
points);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900