ThinkGeo.com    |     Documentation    |     Premium Support

DrawTextWithScreenCoordinate problem

I was experimenting with PointStyle trying to draw multiple symbols for each point feature I have and ran into to the following issue.


I have created a class that inherits from PointStyle and overrides the DrawCore method.

When I used GeoCanvas.DrawTextWithWorldCoordinate to draw one character for my single point feature everything worked OK. Then I converted this point to screen coordinates and used GeoCanvas.DrawTextWithScreenCoordinate expecting the second symbol to appear at exactly the same position. But strangely it appeared to the left of the original symbol.


To single out the problem I simply used DrawTextWithScreenCoordinate to draw some character at the predefined screen position (500,500). And again it was placed to the left and above of the expected position. Moreover, after each pan the character moved compared to its previous position! It seems that in this case the center of the character could appear anywhere between 245 and 500 for x, same about y. And when I changed the coordinates to (200,200) the symbol even started to disappear sometimes.

Am I doing something wrong or is it a bug ?



Michael, 
  
 Welcome to the community, hope you enjoy the learning and sharing here. 
  
 DrawWithScreenCoordinate doesn’t work right because the canvas in that DrawCore doesn’t stand for the whole map. For example, the map is 800 * 800, I panned 200 pixels to left and the map will only draw the extra 200 * 800 piece in the next refresh, and the canvas is just for that piece. That’s why Screen Coordinate cannot work right in the DrawCore method.  
  
 If you want to draw a text (or anything) in a constant position, please try AdornmentLayer.  Here is a sample how to use it. 
     
private void DisplayMap_Load(object sender, EventArgs e)
        {
            winformsMap1.MapUnit = GeographyUnit.DecimalDegree;

            myAdornmentLayer myAdornmentLayer = new myAdornmentLayer();
            winformsMap1.AdornmentOverlay.Layers.Add(myAdornmentLayer);

            winformsMap1.Refresh();
        }

// …
  class myAdornmentLayer : AdornmentLayer
    {
// The canvas here stands for the whole map
        protected override void DrawCore(GeoCanvas canvas, System.Collections.ObjectModel.Collection<SimpleCandidate> labelsInAllLayers)
        {
            canvas.DrawTextWithScreenCoordinate(“Hello”, new GeoFont(“Arial”, 10), new GeoSolidBrush(GeoColor.SimpleColors.Black), 500, 500, DrawingLevel.LevelFour);
        }
    }
 
  
 Hope that helps. 
  
 Thanks, 
  
 Ben

Thank you ,

 

That pretty much solved my problem.

In order to use DrawWithScreenCoordinate I must convert any point to screen coordinates using extent coming from canvas, not  from the map control. And I have to pass in height and width of the canvas again as parameters for the method.


Thanks again,


Michael



Michael, 
  
 You are right. To convert a World Coordinate to a Screen Coordinate, we need the current extent and map’s width / height. As we cannot access the map control in our custom Style / Adornment Layer, (for example in the above myAdornmentLayer, we cannot get MapControl’s extent in the DrawCore method), we can only use the GeoCanvas which is passed in as a parameter. 
  
 Can you let me know why you want to customize your text in a pointStyle class instead of simply using the textStyle, so a textStyle can’t meet your requirements? Can you let us know your scenario and maybe we can provide a better solution. 
  
 Thanks, 
  
 Ben