ComponentOne Maps for WinForms
Working with Map Control / Adding Vector Data / Displaying Shapes using a KML File
In This Topic
    Displaying Shapes using a KML File
    In This Topic

    You can add shapes on the map using zipped KML files (KMZ). The following code uses a KMZ file to add shapes on the map:

    Dim vl = MapReader.LoadKmlFile("WorldMap.kmz", Function(vector, data) 
    Dim name = TryCast(data("name"), String)
    vector.Tag = name
    
    '  customizing the vector data
    Dim fillColor = TryCast(data(KmlReader.StyleFillColor), String)
    If fillColor IsNot Nothing Then
            vector.Style.BackColor = MapReader.GetKmlStyleColor(fillColor)
    End If
    
    Dim placemark = TryCast(vector, C1.Win.Map.VectorPlacemark)
    If placemark IsNot Nothing Then
            placemark.Marker.Caption = name
            placemark.Lod = New LOD(0, 0, 2, 20)
    End If
    
    End Function)
    vl.LabelVisibility = LabelVisibility.AutoHide
    
    ' can set default style for vectors and labels, instead of set the style through each vector
    vl.Style.Stroke.Color = Color.Blue
    vl.Style.Stroke.Width = 1
    vl.LabelStyle.ForeColor = Color.Green
    
    c1Map1.Layers.Add(vl)
    
    var vl = MapReader.LoadKmlFile("WorldMap.kmz", (vector, data) =>
    {
        var name = data["name"] as string;
        vector.Tag = name;
    
        //  customizing the vector data
        var fillColor = data[KmlReader.StyleFillColor] as string;
        if (fillColor != null)
        {
            vector.Style.BackColor = MapReader.GetKmlStyleColor(fillColor);
        }
    
        var placemark = vector as C1.Win.Map.VectorPlacemark;
        if (placemark != null)
        {
            placemark.Marker.Caption = name;
            placemark.Lod = new LOD(0, 0, 2, 20);
        }
    });
    vl.LabelVisibility = LabelVisibility.AutoHide;
    
    // can set default style for vectors and labels, instead of set the style through each vector
    vl.Style.Stroke.Color = Color.Blue;
    vl.Style.Stroke.Width = 1;
    vl.LabelStyle.ForeColor = Color.Green;
    
    c1Map1.Layers.Add(vl);
    

    In the above code, we have referred a class named MapReader that we have created. This class reads the vector data from the KMZ file.

    On adding shapes from a KMZ file, the map will look similar to the image given below:

    Map after adding kml file