Google Maps API Tutorial© 2006, 2007, 2008, 2009 Mike Williams
|Translate this page:|
Data in text filesIf 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.