Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.
public void DrawPolyline(ESRI.ArcGIS.Carto.IActiveView activeView)
{
  if(activeView == null) return;
  ESRI.ArcGIS.Display.IScreenDisplay screenDisplay = activeView.ScreenDisplay;

  screenDisplay.StartDrawing(screenDisplay.hDC, (System.Int16)ESRI.ArcGIS.Display.esriScreenCache.esriNoScreenCache); // Explicit Cast
  ESRI.ArcGIS.Display.IRgbColor rgbColor = new ESRI.ArcGIS.Display.RgbColorClass();
  rgbColor.Red = 255;

  ESRI.ArcGIS.Display.IColor color = rgbColor; // Implicit Cast
  ESRI.ArcGIS.Display.ISimpleLineSymbol simpleLineSymbol = new ESRI.ArcGIS.Display.SimpleLineSymbolClass();
  simpleLineSymbol.Color = color;

  ESRI.ArcGIS.Display.ISymbol symbol = (ESRI.ArcGIS.Display.ISymbol)simpleLineSymbol; // Explicit Cast
  ESRI.ArcGIS.Display.IRubberBand rubberBand = new ESRI.ArcGIS.Display.RubberLineClass();
  ESRI.ArcGIS.Geometry.IGeometry geometry = rubberBand.TrackNew(screenDisplay, symbol);
  screenDisplay.SetSymbol(symbol);
  screenDisplay.DrawPolyline(geometry); //I WANT TO CREATE THIS FROM LAT / LONGs!
  screenDisplay.FinishDrawing();
}

The above code snippet works but the problem is I want to create the IGeometry from lat \ longs not from clicking the screen. How can I create an polyline IGeometry object for the drawPolyline function using standard lat \ long coordinates?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

Tested this on a form with a mapcontrol.

private void testToolStripMenuItem_Click(object sender, EventArgs e)
{
    try
    {
        Test(axMapControl1.ActiveView.ScreenDisplay, new SimpleLineSymbolClass());
    }
    catch (Exception ex)
    {
        MessageBox.Show(this, ex.Message);
    }
}

public static void Test(IScreenDisplay screenDisp, ILineSymbol lineSym)
{
    IProjectedCoordinateSystem pcs = screenDisp.DisplayTransformation.SpatialReference as IProjectedCoordinateSystem;
    IPolyline polyline = null;
    if (pcs != null)
    {
        polyline = MakePolyline(pcs.GeographicCoordinateSystem);
        polyline.Project(pcs);
    }
    else
    {
        polyline = MakePolyline(new UnknownCoordinateSystemClass());
    }
    if(((IRelationalOperator)polyline).Disjoint(screenDisp.DisplayTransformation.VisibleBounds))
    {
        System.Windows.Forms.MessageBox.Show("not within visible bounds");
        return;
    }
    screenDisp.StartDrawing(screenDisp.hDC,(short) esriScreenCache.esriNoScreenCache );
    screenDisp.SetSymbol((ISymbol)lineSym);
    screenDisp.DrawPolyline(polyline);
    screenDisp.FinishDrawing();
}
public static IPolyline MakePolyline(ISpatialReference sr)
{
    IPointCollection pc = new PolylineClass();
    // deal with legacy COM cruft ...
    object missing = Type.Missing; // (not needed with VB.NET)
    pc.AddPoint(MakePoint(-98.0, 29.0, sr), ref missing, ref missing);
    pc.AddPoint(MakePoint(-97.0, 28.0, sr), ref missing, ref missing);
    pc.AddPoint(MakePoint(-96.0, 27.0, sr), ref missing, ref missing);
    pc.AddPoint(MakePoint(-95.0, 26.0, sr), ref missing, ref missing);

    IPolyline polyline = (IPolyline)pc;
    polyline.SpatialReference = sr;
    return polyline;
}
public static IPoint MakePoint(double x, double y, ISpatialReference sr)
{
    IPoint p = new PointClass();
    p.PutCoords(x, y);
    p.SpatialReference = sr;
    return p;
}
share|improve this answer
    
@Kirk Thanks for the code, unfortunately it won't draw the lines. It doesn't give an error or warning, just no lines. –  patrick Aug 31 '10 at 16:50
    
In your code, is screenDisplay.DisplayTransformation.SpatialReference null? If not what is screenDisplay.DisplayTransformation.SpatialReference.Name? –  Kirk Kuykendall Aug 31 '10 at 17:03
    
@Kirk Kuykendall It is set to this:"USA_Contiguous_Albers_Equal_Area_Conic" –  patrick Aug 31 '10 at 18:36
1  
I've edited the code to show how you can project your polyline into the projection used by the screendisplay. –  Kirk Kuykendall Aug 31 '10 at 19:18
    
@Kirk Kuykendall thanks again, but I get a COM Error when I try to pass the polyline object into DrawPolyline –  patrick Aug 31 '10 at 19:55
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.