Search code examples
c#visual-studio-2012google-earth-plugin

Google Earth plugin in C#


In this the tutorial says you can customize the LINESTRING https://developers.google.com/earth/documentation/geometries?hl=en

but when i try to C# VS2012 the line of code

var lineStyle = lineStringPlacemark.getStyleSelector().getLineStyle();

i cant find the method getLineStyle() so i get the error;

error: Error1 GEPlugin.IKmlStyleSelector' does not contain a definition for 'getLineStyle' and no extension method 'getLineStyle' accepting a first argument of type 'GEPlugin.IKmlStyleSelector' could be found (are you missing a using directive or an assembly reference?)

Here is my full code:

    var lineStringPlaceMark = m_ge.createPlacemark("");
    var lineString = m_ge.createLineString("");
    lineStringPlaceMark.setGeometry(lineString);
    lineString.setExtrude(5);
    lineString.setTessellate(500);
    lineString.setAltitudeOffset(5);
    lineString.setAltitudeMode(m_ge.ALTITUDE_RELATIVE_TO_GROUND);

    for (int x = 0; x < arraylat.Length; x++)
    {
        lineString.getCoordinates().pushLatLngAlt(Convert.ToDouble(arraylat[x]), Convert.ToDouble(arraylong[x]), 0);
    }
    //lineString.getCoordinates().pushLatLngAlt(Convert.ToDouble(arraylat[counter]), Convert.ToDouble(arraylong[counter]), 0);



     lineStringPlaceMark.setStyleSelector(m_ge.createStyle("").getLineStyle());

    if (null == lineStringPlaceMark.getStyleSelector())
    {
        lineStringPlaceMark.setStyleSelector(m_ge.createStyle("").getLineStyle());
    }
    var lineStyle =  m_ge.createStyle("").getLineStyle();
    lineStyle.setWidth(100);
    lineStyle.getColor().set("9900ffff");

   /* var lineStyle = lineStringPlaceMark.getStyleSelector().getLineStyle();
     lineStyle.setWidth(lineStyle.getWidth() + 2);
         lineStyle.getColor().set("6600ffff");
    // The Style of a Feature is retrieved as feature.getStyleSelector().
    // The Style itself contains a LineStyle, which is what we manipulate
    // to change the color and width of the line.

    */
    m_ge.getFeatures().appendChild(lineStringPlaceMark);

Solution

  • You would first need to create the actual style object via GEPlugin.createStyle() before you can use it.

    There are a few errors in your code. The lines

    lineStringPlaceMark.setStyleSelector(m_ge.createStyle("").getLineStyle());

    and

    var lineStyle = m_ge.createStyle("").getLineStyle();

    looking at it you would need to do something like.

    var lineStringPlaceMark = m_ge.createPlacemark(string.Empty);
    var lineString = m_ge.createLineString(string.Empty);
    lineStringPlaceMark.setGeometry(lineString);
    lineString.setExtrude(5);
    lineString.setTessellate(500);
    lineString.setAltitudeOffset(5);
    lineString.setAltitudeMode(m_ge.ALTITUDE_RELATIVE_TO_GROUND);
    
    for (int x = 0; x < arraylat.Length; x++)
    {
        lineString.getCoordinates().pushLatLngAlt(
          Convert.ToDouble(arraylat[x]),
          Convert.ToDouble(arraylong[x]), 0);
    }
    
    // we know the placemark has no style, so create one for it
    lineStringPlaceMark.setStyleSelector(m_ge.createStyle(string.Empty));
    
    // now you can access the style object 
    var lineStyle = lineStringPlaceMark.getStyleSelector().getLineStyle();
    lineStyle.setWidth(100);
    lineStyle.getColor().set("9900ffff");
    
    m_ge.getFeatures().appendChild(lineStringPlaceMark);
    

    You could also make a simple static method that returns the style selector for any feature, creating it if it doesn't exist.

    // getStyleSelector wrapper, creates a style object if non is present
    static dynamic GetStyleSelector(dynamic ge, dynamic feature) {
      if(null == feature.getStyleSelector()) {
          feature.setStyleSelector(ge.createStyle(string.Empty));
      }
    
      return feature.getStyleSelector();
    }