Reimers.dk

.NET, AJAX and Google APIs brought together
Welcome to Reimers.dk Sign in | Join | Help
in Search

Read attributes from KML file?

Last post 12-14-2009, 22:41 by jjrdk. 8 replies.
Sort Posts: Previous Next
  •  08-19-2008, 15:48 1761

    Read attributes from KML file?

    Is there a way to read the attributes from KML file? Id like to be able to read and assign the color anf fill to polygons created from the KML file, like one below. I can create polygon from it and can assign my colors and fill, but do not know how to assign the ones specified in the file.

    Thanks,

    Jakub

     

    <?xml version="1.0" encoding="UTF-8"?>
    <kml xmlns="http://earth.google.com/kml/2.2">
    <Document>
     <name>USSC Parcel Boundaries.kmz</name>
     <Style id="PolyStyle102">
      <LabelStyle>
       <color>00000000</color>
       <scale>0</scale>
      </LabelStyle>
      <LineStyle>
       <color>ff0000ff</color>
       <width>0.8</width>
      </LineStyle>
      <PolyStyle>
       <color>fffac2be</color>
       <fill>0</fill>
      </PolyStyle>
     </Style>
     <StyleMap id="PolyStyle101">
      <Pair>
       <key>normal</key>
       <styleUrl>#PolyStyle10</styleUrl>
      </Pair>
      <Pair>
       <key>highlight</key>
       <styleUrl>#PolyStyle102</styleUrl>
      </Pair>
     </StyleMap>
     <Style id="PolyStyle10">
      <LabelStyle>
       <color>00000000</color>
       <scale>0</scale>
      </LabelStyle>
      <LineStyle>
       <color>ff0000ff</color>
       <width>0.8</width>
      </LineStyle>
      <PolyStyle>
       <color>fffac2be</color>
       <fill>0</fill>
      </PolyStyle>
     </Style>
     <Placemark>
      <name>SC100-095</name>
      <snippet></snippet>
      <Snippet maxLines="0"></Snippet>
      <styleUrl>#PolyStyle101</styleUrl>
      <MultiGeometry>
       <Polygon>
        <tessellate>1</tessellate>
        <outerBoundaryIs>
         <LinearRing>
          <coordinates>
    -80.808808,26.638871,0 -80.811331,26.638851,0 -80.814387,26.638842,0 -80.819504,26.63883,0 -80.821066,26.638841,0 -80.82130600000001,26.638846,0 -80.821305,26.653005,0 -80.80519,26.653076,0 -80.789027,26.653086,0 -80.788995,26.653086,0 -80.77284899999999,26.653129,0 -80.756653,26.65318,0 -80.756677,26.638939,0 -80.756772,26.638938,0 -80.757598,26.63898800000001,0 -80.76121999999999,26.63901,0 -80.764444,26.638992,0 -80.768063,26.638973,0 -80.77193699999999,26.63897,0 -80.772874,26.638965,0 -80.774337,26.638956,0 -80.777475,26.638933,0 -80.78221000000001,26.638942,0 -80.785484,26.638908,0 -80.786333,26.638916,0 -80.789023,26.638925,0 -80.79051699999999,26.63893,0 -80.794618,26.638895,0 -80.797904,26.638896,0 -80.802537,26.638872,0 -80.805196,26.638872,0 -80.805848,26.638872,0 -80.808808,26.638871,0 </coordinates>
         </LinearRing>
        </outerBoundaryIs>
       </Polygon>
      </MultiGeometry>
     </Placemark>
    </Document>
    </kml>

  •  08-20-2008, 6:00 1763 in reply to 1761

    Re: Read attributes from KML file?

    Same here :

    Solved my own problem with Jacobs brilliant code.

    all my questions answered in two lines (load as many KMLs as needed).

    Do While dtrMark.Read()

    KMLUrl = "http://www.myurl/kml/" & dtrMark("DocName").ToString()

    Dim g As GoogleGeoXml = New GoogleGeoXml(dtrMark("DocName").ToString(), KMLUrl)

    GMap.Overlays.Add(g)

    Loop

     

    so easy when I actually read this site :)

  •  08-20-2008, 11:10 1765 in reply to 1763

    Re: Read attributes from KML file?

    That is the easy way to do it. Sadly however, the KML parsing in the control don't read attributes. I have had trouble reading KML documents, either because Google changes the specs or because the ways to read overlays is very convoluted. It is not until you get several elements into the current placemark tag that you know whether it is a marker or ployline or something else. So I opted for solution that reads only the necessary information. For markers it makes sense for me, since the icon information should be decided by the presenting page and not the KML. The same argument could be made for polylines and polygons, but here the differentiation between overlays also has an important say.
  •  08-21-2008, 3:52 1771 in reply to 1765

    Re: Read attributes from KML file?

    Thanks,

    I noticed that the GoogleGeoXml does not read KMZ files, not even in your example site.  In your blog you mentioned that it should read kmz. Did something change?

    Thanks,

    Jakub

  •  08-21-2008, 10:57 1772 in reply to 1771

    Re: Read attributes from KML file?

    Google changed. Previously the GGeoXml overlay, according to the documentation, supported KML, KMZ, GeoRSS and GPX. Now they only mention KML.
  •  12-14-2009, 10:30 2909 in reply to 1763

    Re: Read attributes from KML file?

    Can you help me how to Dim dtrMark and KMLUrl filetypes as mention below.  Thank you !

    Best

    Johan 

     

    lof:

    Same here :

    Solved my own problem with Jacobs brilliant code.

    all my questions answered in two lines (load as many KMLs as needed).

    Do While dtrMark.Read()

    KMLUrl = "http://www.myurl/kml/" & dtrMark("DocName").ToString()

    Dim g As GoogleGeoXml = New GoogleGeoXml(dtrMark("DocName").ToString(), KMLUrl)

    GMap.Overlays.Add(g)

    Loop

     

    so easy when I actually read this site :)

  •  12-14-2009, 19:27 2915 in reply to 2909

    Re: Read attributes from KML file?

    Can you be a little more specific on what you are trying to do? The code you reference tries to build a URL to the KML file which is to be used in the GoogleGeoXml control. If you already know the absolute public URL then you can pass it in directly.
  •  12-14-2009, 21:58 2916 in reply to 2915

    Re: Read attributes from KML file?

    Ok, what I am trying to do is open and read a KML file located here "http://www.freebords.se/a.kml" or "http://www.freebords.se/b.kml" ( 2 different KML files)  to draw a polyline

    onto the mapcontrol. The problem is that I can't find any matching sample code so that I can figure out how to do it..

     I would love to have a COMPLETE VB.asp working example how to read KML files and fill the map with placemarks or polylines. Code snippets will not help me or anyone else.


    BRGDS

    Johan  



  •  12-14-2009, 22:41 2917 in reply to 2916

    Re: Read attributes from KML file?

    How can I refuse when you put it so politely.

     

    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="freeexample.aspx.cs" Inherits="freeexample" %>

    <%@ Register Assembly="GoogleMap" Namespace="Reimers.Map" TagPrefix="Reimers" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head runat="server">

    <title></title>

    </head>

    <body>

    <form id="form1" runat="server">

    <div>

    <Reimers:GoogleMap runat="server" ID="GMap" Latitude="26.6" Longitude="-80.8" Zoom="11">

    <Overlays>

    <Reimers:GoogleGeoXml ID="a" URL="http://www.freebords.se/a.kml" />

    </Overlays>

    </Reimers:GoogleMap>

    </div>

    </form>

    </body>

    </html> 

     

    If you have a look at the constructors for the GoogleOverlayCollection class, then you will see that there is one that takes a link to a KML file, which it will attempt to parse and let you work with the overlays on the server.

    As you can see there are multiple ways of achieving your goal. Since you don't bother to specify what it is you are trying to do the above "COMPLETE VB.asp working example" may not be very useful to you. This is why I generally prefer that people ask about specific questions after they have made an attempt at solving the problem themselves. Very often you can get far by reading the documentation, readingat the information that is supplied for Intellisense when looking at parameters and return types.

View as RSS news feed in XML