screwfacecapone 0 Newbie Poster

I am working on a web application that builds a KML file using a list of coordinates and is supposed to display them on Google maps when you click a button. When I'm testing it on desktop it works, as it brings up google earth with no problem. However, when I try to test it on an android device, I get "This community map can not be displayed due to errors". Funny thing is, is that I navigated to google's KML samples page, and I was able to open them up without incident. I added this to my web.config file:

<staticContent>
      <mimeMap fileExtension=".kml" mimeType="application/vnd.google-earth.kml+xml" />
      <mimeMap fileExtension=".kmz" mimeType="application/vnd.google-earth.kmz" />
    </staticContent>

This is the code that generates the KML file:

Public Sub BuildKML()
        Dim latList As Generic.List(Of String) = Session("latList")
        Dim longList As Generic.List(Of String) = Session("longList")

        My.Response.Clear()
        My.Response.ContentType = "application/vnd.google-earth.kml+xml"
         My.Response.AddHeader("Content-Disposition", "attachment; filename=OrderMap.kml")

        'uncomment following line to view raw xml
        'My.Response.ContentType = "plain/text"
        My.Response.ContentEncoding = System.Text.Encoding.UTF8
        Dim stream As New System.IO.MemoryStream
        Dim XMLwrite As New XmlTextWriter(stream, System.Text.Encoding.UTF8)
        XMLwrite.Formatting = Formatting.Indented
        XMLwrite.Indentation = 3

        XMLwrite.WriteStartDocument()
        XMLwrite.WriteWhitespace(Environment.NewLine)

        XMLwrite.WriteStartElement("kml")
        XMLwrite.WriteAttributeString("xmlns", "http://www.opengis.net/kml/2.2")
        XMLwrite.WriteAttributeString("xmlns:gx", "")
        XMLwrite.WriteWhitespace(Environment.NewLine)
        XMLwrite.WriteStartElement("Folder")
        XMLwrite.WriteWhitespace(Environment.NewLine)
        For i As Integer = 0 To latList.Count - 1
            XMLwrite.WriteStartElement("Placemark")
            XMLwrite.WriteWhitespace(Environment.NewLine)
            XMLwrite.WriteElementString("name", "Point" & i.ToString)
            XMLwrite.WriteWhitespace(Environment.NewLine)
            XMLwrite.WriteElementString("description", "This is a point in the order")
            XMLwrite.WriteWhitespace(Environment.NewLine)
            Dim longitude As String = longList(i)
            Dim latitude As String = latList(i)

            XMLwrite.WriteStartElement("Point")
            XMLwrite.WriteWhitespace(Environment.NewLine)

            XMLwrite.WriteElementString("coordinates", longitude & "," & latitude & ",0")
            XMLwrite.WriteWhitespace(Environment.NewLine)
            XMLwrite.WriteEndElement()
            XMLwrite.WriteWhitespace(Environment.NewLine)
            XMLwrite.WriteEndElement()

        Next
        XMLwrite.WriteWhitespace(Environment.NewLine)
        XMLwrite.WriteEndElement()
        XMLwrite.WriteWhitespace(Environment.NewLine)
        XMLwrite.WriteEndDocument()

        XMLwrite.Flush()
        Dim reader As IO.StreamReader
        stream.Position = 0
        reader = New IO.StreamReader(stream)
        Dim bytes() As Byte = System.Text.Encoding.UTF8.GetBytes(reader.ReadToEnd())
        My.Response.BinaryWrite(bytes)
        My.Response.End()
    End Sub

So, what exactly am I doing wrong here?

Dani AI

Generated

Quick expert summary for : your generator looks syntactically plausible (and the desktop Google Earth success strongly suggests the KML itself is valid), but Android’s KML/Maps handlers are sensitive to how the file is served — MIME, Content‑Disposition, filename/URL and encoding/BOM are the usual suspects. KML is an XML format and must follow the KML 2.2 rules (tags are case‑sensitive), so validating the output against the KML reference is a good baseline. (developers.google.com)

What to check first (fast, high ROI):

  • Confirm the server really returns Content‑Type = application/vnd.google‑earth.kml+xml and that the URL presented to the phone ends in “.kml” (IIS staticContent/mimeMap settings are the right place to do this). (learn.microsoft.com)
  • Try removing the Content-Disposition: attachment header (or use inline) — attachment tells clients “save this” instead of “process/display this”, and some Android handlers will fail to load an attached payload. (developer.mozilla.org)

Quick troubleshooting commands and a simple workflow:

curl -I "https://yourserver/OrderMap.kml"
# expect: Content-Type: application/vnd.google-earth.kml+xml

If header tweaking doesn’t help, use a simple two‑step approach: write the generated bytes to a real .kml file on disk and redirect the client to that static URL (so the phone GETs a normal .kml resource). Example pattern:

Dim p = Server.MapPath("~/tmp/OrderMap.kml")
System.IO.File.WriteAllBytes(p, bytes)
Response.Redirect("/tmp/OrderMap.kml")

That often isolates whether the problem is streaming/headers vs the KML content itself.

Last checks: try opening the same generated file with the Google Earth / Maps KML importer on Android (the SDK/utilities do support KML layers) and compare with a Google sample that works — a byte diff will show hidden BOM/encoding or stray characters. (developers.google.com)

If none of the above fixes it, capture the raw HTTP response (headers + body) from the phone/browser and post that snippet — the header/body difference vs a working sample is usually where the error hides.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.