Truncated GPS Coordinates

Hi Particle Community,
I am currently working on a program using the google-maps-device-locator library and ultimately want to store it in a file. However I currently only see two significant figures when I use serial output to the screen thru Putty. Ideally I’d like to see all my significant figures thru the Serial port. I was thinking it was originally a variable definition problem but I’m not sure if it’s solved my problem yet. Here is my code… any help is appreciated!

#include "google-maps-device-locator.h"

GoogleMapsDeviceLocator locator;
double globalLongit =0.000000000; //mtp added
void setup() {
	Serial.begin(9600);

  // Scan for visible networks and publish to the cloud every 30, in this case,15 seconds
  // Pass the returned location to be handled by the locationCallback() method
  locator.withSubscribe(locationCallback).withLocatePeriodic(30);
}

void locationCallback(float lat, float lon, float accuracy) {
  // Handle the returned location data for the device. This method is passed three arguments:
  // - Latitude
  // - Longitude
  // - Accuracy of estimated location (in meters)
  globalLongit = lon; //mtp added
  Serial.print("output from callback:  ");
  Serial.println(globalLongit);
}

void loop() {
  locator.loop();
  Serial.print("output to screen  ");
  Serial.println(globalLongit);
  delay(1000);
}

Can you check on console.particle.io/events what response you get from the Google server?

Have you tried declaring this as float globalLongit = 0.0;?

When you print it out try this:
Serial.printlnf("Output from callback: %.6f", globalLongit);

If the Longitude is only recorded as 62.22 say (or at the accuracy that can be recorded with a float data type) that is what will be shown.

The float variable can actually store more than 2 decimal places. The truncation occurs with you print a float or double using Serial.print.

This would print 5 decimal places:

Serial.println(globalLongit, 5);

But you don’t need to cast it to a double. You can also pass a float and it will work fine, you just need to tell Serial.println how many decimal places to show; the default is 2.

1 Like

ScruffR, Armor, and rickkas7,
Thanks for the quick response, you were all super helpful. I didn’t have any issues with the google callback at all. The root cause was the truncation with the Println statement and was super easy to fix. Thanks for your help!
-Mike