Data in text files
If you happen to have your data stored in some application that doesn’t export XML files, you might prefer to output it as a plain text file.
It’s also possible to have an script running on your server which returns plain text data containing a different selection from the data depending on query information derived from what your users input. I’m not going to cover the server side of things in this tutorial.
Here’s a simple example
You can see the file that I used here
The code defines a function “process_it()” which processes the retrieved data.
The entire data file is passed to process_it() as a single string, which we can then split into lines using newline (\n) as a separator. Each line can then be split into parts using some separator. I’m using “|” as the separator since it’s unlikely to occur in the data.
Care needs to be taken to avoid trying to split empty lines. With some text editors it’s not possible to avoid a final newline character at the end of the file, and split(“\n”) will consider there to be an empty line after the final newline.
Once all the data has been parsed, we can create the markers and the sidebar as in the previous examples.
Note that the code to insert the assembled sidebar information into its <div> must be inside the process_it() function.
Unlike XML, plain text files can contain < and >, and there’s no problem with quote character confusion when using things like links or images. This may well make plain text more suitable for map data in many situations.
There’s no problem with MIME types either. This code works even if the server sets the MIME type to (text/xml) or (text/html) instead of (text/plain), but it’s very unlikely that any server wouldn’t use (text/plain) for a file with a .txt extension.
If you’re used to programming languages that wait for i/o to complete, you might tend to put code that uses the data read from the file after the “GDownloadUrl()” statement. That would be wrong because code placed there would get executed immediately rather than waiting for the data to arrive. Any code that acts on the retrieved data should be placed inside the process_it() function.
- All data is considered to be strings of characters. You need to convert your latitude and longitude from strings to floating point numbers by using “parseFloat()”.
In some circumstances, it might seem that you can get away without doing that, but then things can go horribly wrong later if the Google code tries to perform arithmetic on values that are not numbers.
- Don’t choose a separator character that occurs in your data.