LESSON 25: Display Your GPS Data as Track on Google Earth

In this lesson we will learn how to take the data we are logging from our GPS, and display it properly on Google Earth. This lesson builds on Lesson 22Lesson 23 and Lesson 24. You will need to complete those lessons before undertaking this lesson. In this series of lessons we are using the Arduino,  Adafruit Ultimate GPS, Virtuabotix SD card reader, and a Battery Clip to power the arduino during your mobile jaunts.  You will need a 9V battery. If you are going to be doing a lot of mobile work, it is probably worth it to invest in a good 9v Rechargable battery system.

Adafruit Ultimate GPS
Adafruit Ultimate GPS

When you get your gear gathered up, you will be ready to get this project going. First, hook the GPS and Card Reader up as we did in Lesson 23. We will start with the software that we used in Lesson 23. This software correctly reads data from the GPS and then logs the data into two data files on the SD card. The problem, though, we did not really do any fine tuning of the data file we were creating.

GPS Track
Displaying Data from Adafruit Ultimate GPS as track on Google Earth

In this lesson we want to create a data file that we can display as a track in Google Earth. In order for this to work, we have to save the data in the manner a KML file wants to see. We have to save the data as:

Decimal Longitude, Decimal latitude, Altitude

I find it works to not use a line ending, but put a single white space after the Altitude.  That is, delimit with commas as shown above, but then use one white space to delimit between successive lines of data.

Lesson 24 explained in great detail how to interpret the NMEA sentences. The challenge here is that the Arduino is very poor at parsing strings. The NMEA sentence would be easy to parse in Python, because Python is so good at easily manipulating strings. Since it is tedious to manipulate strings in Arduino, we are going to try and parse the data using numbers and math, not strings. To explain this, I will give an example for Latitude, and Longitude will work in a similar manner. The latitude data we get from the parsed data from the Adafruit GPS looks like this:

3051.8007

From lesson 24, we know that this represents 30 degrees and 51.8007 minutes. In order to parse this in Arduino using just numbers I do the following. First create three variables deg, degWhole, and degDec. The variable deg will hold the final answer, degWhole holds the whole part of the number, and degDec whole the part to the right of the decimal. Adding degWhole to degDec gives you deg, your final answer. So, lets assume x = 3051.8007, our Latitude from the GPS.

 Dividing by 100 moves the decimal to the left two spots, and taking the int removes anything to the right of the decimal. Then converting back to float gives us a round number with no values to the right of the decimal. For 3051.8007 it turns it into 30.0, the whole number part of degrees. Now to get the fractional part, we need to take minutes and divide by 60. This will always be a number less than one. To get fractional part of the latitude in degrees, we do the following:

 We have to multiply degWhoe by 100 to get the decimal moved back to the right by 2. Then we subtract that number from the original latitude, and we are left with minutes. Then divide by 60 and we have converted minutes to a fraction of a degree. We now have the whole part and the fractional part of the answer, so we just add those together:

 We now have a nice decimal number, deg, which is the decimal representation of our Latitude. We have one more thing to deal with. If the GPS is reporting ‘N’, leave the number positive. If the GPS is reporting ‘S’ (that we are in the Southern Hemisphere),  you need to make your latitude number negative. Similarly, when you are doing your longitude, you need to make the longitude negative if you are in the Western Hemisphere.

Finally, when you write the file, Google Earth will want you to store the data as:

Longitude, Latitude,altitude

It is curious that this is reverse from what you would do if you were entering coordinates into the Google Earth search bar. That wants Latitude first.

In any event, with the parsing and formatting described above, you can create a file that is almost ready to read into Google Earth. The code below will create your coordinates in the correct format for KML, which Google Earth will read. Please watch the video for a complete description of the code.

 The final thing we have to do is to put a “wrapper” around the coordinates to turn the coordinates into a .kml file Google Earth will like. I just do this manually. I open the text file on the SD card created by the code above, and then just paste it into this KML wrapper, and save the file with a .kml extension. The KML wrapper is as follows:

 Just paste your coordinate data in the file above between <coordinates> and </coordinates>, save with a .kml extension, and you should be able to open it with Google Earth and see your track. Good luck!

36 thoughts on “LESSON 25: Display Your GPS Data as Track on Google Earth”

  1. how to integrate Real time tracking on google earth ?

    cos i wanna track my Quadcopter in real timing do u know how to do that ?

    Please help me , in need of help to integrate in my college final year project !

    thanks in advance 🙂

    1. Sir I need ur help..I want to make Arduino gps tracker.The components which i’m using is : arduino uno,neo-6m gps and gsm 900.Im stuck in programming.

  2. Sir,
    I would like to thank the videos, they helped me a lot and are very didactic. I have only one doubt it would be possible to send the SD card data to Google Maps in real time to see the position in real time for example to make the monitoring of a car, domestic animals and others. Sorry but my englsh is poor.
    Cheers

    1. Thanks for the kind words. To send data in real time to google earth, you would send over ethernet (with arduino ethernet shield) to python running on desktop computer. Python can parse and build the KML file in real time for live google earth map updates. We have done this and it works.

  3. Hi Paul, i’ve been learning your tutorials for the GPS tracking. When I tested the setup and brought up the file from Sd card all I got was NMEA with no data in it and GPS.txt never appeared. Could this be an issue with my SD card? My data streams in very slowly even at 115200 baud. Regardless of what setting I place, nothing speeds up. My Arduino tells me I have low memory, but seems to work fine. Thank you for your tutorials. I hope to complete a project soon.

  4. sir i want to display the latitude,longitude,date,time,speed and distance on 40×2 lcd. but when i edit this program and write serial.begin(40,2) in void setup upper side the serial.begin(9600) it shows the both logged file blank.

  5. Hi my teacher,
    I have a problem when I use arduino with the “SD Card”, I see this message “Low memory available, stability problems may occur.”
    can you help me please ?
    Thanks in advance

    1. That means your arduino is running out of memory. You will need to cut your code to bare minimum, and make sure you are not loading any libraries that are not needed. If this does not fix it, there is no easy way to add memory, you would have to get one of the arduino variants that has more memory.

      1. My teacher, thank you so much for your answer but when I try to use the code in lesson 23, it is read very well without any problem related to memory and this problem appears only when I’m tried to use the code in the lesson 25. So do you think that the problem is still related to the memory? I’m so sorry for bothering you my teacher and once again thank you so much.

  6. Hello!
    First of all thank you for your very useful and very well explained videos, they helped me a lot! you’re brilliant!

    I need to do a project very similiar to this one with your gps. Instead, I’m using an Arduino MEGA because I need to also put the latitude and longitude along with the time and date on an lcd. I have a question:
    How can I make the arduino transform the NMEA files directly into KML files, without opening it manualy and save it into a kml file with the wrapper, as you did in this video?
    Sorry for bad english ^_^

    1. That is a pretty hard thing to do. You would have to go in and learn all the string manipulation commands in Arduino, and figure out how to get it to do the conversion. Python is much better and easier to manipulate strings and work with files, which is why I did it in Python on these lessons. What you want to do is probably possible, but you will have to work hard and learn how to manipulate strings.

      1. does that imply introducing XML code into my C++ arduino code? do a have to put a wrapper in the code, somehow?

        1. Ok so it seems i got it all wrong, Found out that what I wanted to do is almost impossible to do but surely extremly difficult. So I used your technique all the way and it works just fine. Now all it’s left for me to do is to also show temperature along with the traking. I have the temperature sensor but I don’t know how to implement it into the kml file. I searched all over the web for an example of tags, or anything like that but had no results. Please, can you help me? or anyone who has any idea..

    2. This question is pretty old now. But i’ve found this Lessons a few days ago so i think some other people will do so aswell. I managed to Wrap the Data directly into the Wrapper with Standard SD Lib functions. First i write the wrapper to file in the setup() method. Then i use sdfile.seek(sdfile(read) – ), write the Data and the close tags to file. So you’ll always append Data behind the last Data and then got the close tag. The only thing that can possibly ruin your file, is when you turn off or pull out the sdcard within one of those cycles, but this has not happend to me so far.

      Regards
      Dominik

  7. Hi Paul,
    I really enjoy the material on your Web site.
    On final steps “Display GPS on Google Earth” I have problems.
    If I type one coordinate, Google Earth show it without error, but when I mix the coordinates and KML wrapper goes to wrong location and there aren’t polygons.
    Can be the Google Earth or KML version? I will appreciate your comments.

  8. Hi,

    First off, great site.

    I’d like to point out that the Arduino really hates division. It takes almost an order of magnitude longer to do division as opposed to multiplication. So, I would advise to avoid the use of division in Arduino code. Instead, consider multiplying by the inverse. Provided sufficient decimals to achieve the required accuracy. It should speed up the code dramatically.

  9. Sir,
    I have an idea of implementing this gps tracker program using the xbees. Is it possible?? What do you think my challenges would be?

  10. Hello, I have your sketch working on Arduino Mega along with SIM900 GSM. All works well apart from what you will think is a simple point. I am trying to show where on Google Earth the unit is when inquired by an SMS, I have tried to use deg as per sketch but is always some way off true position. I have also tried to use the maths in a seperate section, but still no use. I must have listened to your page a dozen times but my 70+ year old brain can’t get it right. I would be most grateful for any help as according to my map I am in the North Sea instead of in Kent UK. Best regards

    1. If you have the numbers done correctly, it is possible that you are mixing up the sign of your Longitude. A positive would be the eastern hemisphere, and a negative number the western hemisphere. Make sure your sign is correct on the longitude for where you are.

  11. Sir,
    I am doing a project in which utc time must be converted to local standard time..could you give me one example how to convert this gmt or utc time to any of the standards available..or IST

  12. Hi Paul,

    First of all, thank you for the great tutorials. I am new to Arduino. I’m trying to map my path as I walk inside of a building.

    Is there a relatively simple way to do that?

    Thanks again!

  13. This is a great tutorial, as a retired person I decided I would teach my grandchildren programing, it would not be possible without people like you I would not be able to help them, so thank you.

  14. Sir,
    Loved your tutorial and implemented it right away.But,
    the serial monitor and SD card remain blank.
    -The sketch complied successfully
    -The sketch uploaded successfully.
    -The code and connections are the same as yours.
    -components used are:
    -ARDUINO UNO
    -SIM28ML GPS module(top gps)
    -SD card module having the same pins as the one you used.

    1. OK, sounds like a SD problem. JUst do something very simple like make a counter, and save the count to SD card. Get the SD card working for a simple program like count first.

  15. Another great tutorial. Worked OK, but I would like to share what I found that you and others may find useful. When I used your “wrapper” (cut & paste the GPS.txt data) and opened the “kml” file in Google Earth there were some breaks in the track and the track line was angular and not smooth. So, I took the GPS.txt data and generated a kml file from GPSVisualizer.com and that showed a nice smooth, unbroken track line on Google Earth. I can now use that file as a template “wrapper” and plug in new GPS.txt data between as you instructed in your tutorial. Even better, under Tools>GPS in Google Earth you can import a file with an nmea extension. So, I took the NMEA.txt file generated from your tutorial, renamed the extension to nmea and imported it to Google Earth…very slick, no problem and the track line even shows arrows to indicate the track direction. Thanks for prompting my interest in improvements!

  16. Great set of Arduino videos. Thank you. I am unable to download lessons 21, 24 & 25. Please guide me how I can retrieve them.

  17. Hi Paul,

    I took your code and mashed it with the code I was using . I have a project where I will be sending a model rocket to space but would like to keep track of all the telemetry data in real time.

    I have my arduinos connected with the HC-12 adapter and one to the gps adapter. For some reason when I send my nmea rmc & gga strings, I only get just one string unless I delay but then I get random orders. Instead of RMC, GGA, C, A, C, etc, I get something like RMC,GGA, C, A , A , A , C, A, C, C, C, C, C, C etc…

    Any info on how to either clear the buffers or maybe I am sending the data wrong.

    Thanks for any info and you can see my v2 code here which incorporates your 2 nmea string declaration.

    V2 of code – https://gist.github.com/tokenwill/96e5f35ace0e2a2863497394d80e76d2#file-arduino-tx-v2-ino

    Full gist repository
    https://gist.github.com/tokenwill/96e5f35ace0e2a2863497394d80e76d2

  18. Hello mr paul
    i am one of the biggest funs for your works…
    please this year i will graduate and i need to finish my final project , i want to make a gps tracker , and see the tracking in a map in an android app .
    please can you guide me to do that
    your fun amar

  19. Hello Admin
    I have followed all your lessons love them.
    I can get the GPSDATA to SD card no problems.
    But I can’t get the minus to come up in the GPSDATA file
    eg
    146.5496,38.2083,45.10 146.5496,38.2083,44.60 146.5496,38.2082,44.20 146.5496,38.2082,44.60 146.5496,38.2082,43.00 146.5495,38.2081,42.70
    Should be
    146.5496,-38.2083,45.10

    I’m in Australia so Lon is “E” and Lat is “S”

    degWhole=float(int(GPS.latitude/100));
    degDec = (GPS.latitude – degWhole*100)/60;
    deg = degWhole + degDec;
    if (GPS.lat==”S”) {
    deg= (-1)*deg;
    }
    I can manually put the negative in and Google goes to the correct place.
    Any suggestion ?
    thanks

Comments are closed.