How to read a kml file (utf-8)

darbid

Registered User.
Local time
Today, 20:36
Joined
Jun 26, 2008
Messages
1,428
[RESOLVED]How to read a kml file (utf-8)

I need to read a kml file putting the contents into a string in order to send it to the google earth plugin.
eg
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Placemark>
<name>Simple placemark</name>
<description>Attached to the ground. Intelligently places itself
at the height of the underlying terrain.</description>
<Point>
<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
</Point>
</Placemark>
</kml>
I thought something like this would do it
Code:
Dim oFSO As New FileSystemObject
Dim oFS
Set oFS = oFSO.OpenTextFile("C:\Documents and Settings\mch02080\Desktop\GoogleMapsVBexample\test.txt")

Do Until oFS.AtEndOfStream
KMLstring = KMLstring & vbCr & oFS.ReadLine
Loop
KMLstring = Right(KMLstring, Len(KMLstring) - 4)
But it is not being accepted. IT should be the same as reading a HTML file i think.
 
Last edited:
im not 100% sure

but i think maybe you need to include a google key in the string to use the google map facilities - they are free but you have to register.

you certainly to do change a zip code/post code into lat longs
 
but i think maybe you need to include a google key in the string to use the google map facilities
my understanding was that for google earth i do not need to use a key.
 
it was just a thought - ive only used it so far to dereference some postcodes into lat longs - but while doing this i came across loads of examples of code for setting points and drawing maps
 
I can find a lot of examples (one in this forum of writing to kml from VBA) but not much on reading it. So going back a step to check everything else is right this should work right

Code:
KMLstring = "<?xml version=""1.0"" encoding=""UTF-8""?>" & vbCr _
& "<kml xmlns=""http://www.opengis.net/kml/2.2"">" & vbCr _
& "<Placemark>" & vbCr _
& "<name>Floating placemark</name>" & vbCr _
& "<visibility>1</visibility>" & vbCr _
& "<description>Floats a defined distance above the ground.</description>" & vbCr _
& "<LookAt>" & vbCr _
& "<longitude>-122.084075</longitude>" & vbCr _
& "<latitude>37.4220033612141</latitude>" & vbCr _
& "<altitude>45</altitude>" & vbCr _
& "<heading>0</heading>" & vbCr _
& "<tilt>90</tilt>" & vbCr _
& "<range>100</range>" & vbCr _
& "<altitudeMode>relativeToGround</altitudeMode>" & vbCr _
& "</LookAt>" & vbCr & "<Style>" & vbCr & "<IconStyle>" & vbCr & "<Icon>" & vbCr _
& "<href>http://maps.google.com/mapfiles/kml/pal4/icon28.png</href>" & vbCr _
& "</Icon>" & vbCr & "</IconStyle>" & vbCr & "</Style>" & vbCr & "<Point>" & vbCr _
& "<altitudeMode>relativeToGround</altitudeMode>" & vbCr _
& "<coordinates>-122.084075,37.4220033612141,50</coordinates>" & vbCr _
& "</Point>" & vbCr & "</Placemark>" & vbCr & "</kml>"

I have to put some lines together as you cannot have an unlimited number of line continuations.
 
I have tried MSXML to load the file but I am unsure how to get a string from this.

after the XMLDOC.Load(Myfileparth) I am not sure how to get the XMLDOC into a string.

I have also tried this which I found in another forum

Code:
Dim fname As String
Dim sVal As String
fname = "C:\Documents and Settings\mch02080\Desktop\GoogleMapsVBexample\test.kml"
sVal = OpenTextFileToString2(fname)

Function OpenTextFileToString2(ByVal strFile As String) As String
' RB Smissaert - Author
Dim hFile As Long
hFile = FreeFile
Open strFile For Input As #hFile
OpenTextFileToString2 = Input$(LOF(hFile), hFile)
Close #hFile
End Function
This produces a string but I get nothing. I do not get an error message or anything when I try to use it.

Just in case anyone is playing with the Google Earth plugin here is my code snippet and I will also add the java script method just in case anyone here is feeling really nice.
Code:
Dim GE_Features As GEFeatureContainerCoClass
Dim KMLstring As String
Dim GE_View As GEViewCoClass
Dim edna As KmlObjectCoClass
Set GE_Features = Me.GEPluginCoClass3.getFeatures
[COLOR=Red]'I need to do something here to get KMLString formated properly[/COLOR]
Set edna = Me.GEPluginCoClass3.parseKml(KMLString) [COLOR=Red]'maybe this is wrong[/COLOR]
GE_Features.appendChild edna

Code:
function addKmlFromString(kmlString) {
  [B]var kmlObject = ge.parseKml(kmlString);[/B]

  ge.getFeatures().appendChild(kmlObject);
}

...

addKmlFromString(
  '<?xml version="1.0" encoding="UTF-8"?>' +
  '<kml xmlns="http://www.opengis.net/kml/2.2">' +
  '  <Placemark>' +
  '    <name>Test Placemark</name>' +
  '    <Point>' +
  '      <coordinates>' +
  '        -122,37' +
  '      </coordinates>' +
  '    </Point>' +
  '  </Placemark>' +
  '</kml>');
 
Last edited:
i think if you load the xml document, you then have to find the tag you want, to read the tags value. There are probably methods to find a tag built into the msxml utilities.

however, when i used google earth, i actually requested a csv reply rather than kml, which i found easier to manage. (just use split, based on a comma separator)

i used msxml to load an xml file of invoices, and i couldnt work out how to turn these into tables automatically, so its possibly not the best way, but i did it explicitly by treewalking the xmldocument completely.

i recently posted a template for this in one of the reference forums, but it hasnt appeared yet.
 
however, when i used google earth, i actually requested a csv reply rather than kml, which i found easier to manage. (just use split, based on a comma separator)
Whilst I am not 100% sure what you are talking about here I am only using the Google Earth API from my post here http://www.access-programmers.co.uk/forums/showthread.php?t=167295

For some reason Google does not allow you to open a file from your local disk with the API you can only call a kml file with the Google Earth Application. Thus my need to understand why it is not working. I can using call these same kml files with google earth and they work fine, thus the frustration, but I have a feeling that my problem has more to do with my other code and not this string.

The API fits nicely on a form and you do not have all the API stuff to get the google earth window on a form but if I cannot use kml the API will be useless.
 
i'm struggling really then - i havent tried to do anything with maps directly, although i saw lots of code examples.
 
Yeh Google maps and the examples that are here is where I started. But I wanted to add multiple markers. To do this you have to use kml But to give google maps a kml file you need an internet site to host the kml file.

Thus come to Google Earth. Flying around to the markers is great. I have managed to get the Google Earth window onto a form but it is (with about 6 API Calls) a mess, starts a mess, flickers, minimise and maximise are a mess and closing it down also is a mess. IMO you are just not supposed to play with VBA/MSAccess and windows this way as it breaks.

I would really prefer to use the Google Earth API as I then have a nice ActiveX control on the form and there is no "mess".

Oh and GTH you have helped me a thousand times so far so I will let this one slip. Thank you so much.
 
ok this is working - I was helped from another forum VB forum.

you do have to change the .kml to txt and this below seems to do the trick.

You need to keep track of all the spaces and put them into the string

Code:
Dim x, i As Integer
   Dim xstr As String
   Dim ts, s, count
   Dim fso As FileSystemObject

   Const ForReading = 1
   
   Set fso = CreateObject("Scripting.FileSystemObject")
   x = 1
  
     Set ts = fso.OpenTextFile("C:\Documents and Settings\mch02080\Desktop\GoogleMapsVBexample\test.txt", ForReading) 'replace with your file save it as a txt or an rtf file

   Do While ts.AtEndOfStream = False
 
   s = ts.ReadLine
   
   ' count spaces
    For i = 1 To Len(s)
        If Mid(s, i, 1) = " " Then count = count + " "
        If Mid(s, i, 1) = "<" Then Exit For
        
    Next
       
  If x = 1 Then
  xstr = s
  Else
  xstr = xstr & vbCrLf & count & s
    End If
 
   count = ""
   x = x + 1
   Loop
   
   ts.Close
Also when passing the result into the API I use

Code:
Set edna = Me.GEPluginCoClass3.parseKml([COLOR=Red]CStr[/COLOR](xstr))
 

Users who are viewing this thread

Back
Top Bottom