Help with Toybox.Activity.getActivityInfo().currentLocation in a watchface

Former Member
Former Member
Hi.

I'm trying to use the last known location to calculate sunrise/sunset times for a watchface, as per the thread here.

As far as i can tell i'm doing everything correctly but getting nonsensical results. For example, in a function i have the following code+check ...

var loc = Toybox.Activity.getActivityInfo().currentLocation;
Sys.println("Location: " + loc);


... which i would have expected to return a two element array containing latitude and longitude (e.g. [-37.81400, 144.96332]), as per the comment here. Instead this prints "Location: Obj: 93", and attempting to use loc[0] and loc[1] in a calculation cause an UnexpectedTypeException: Expected Symbol, given Number error. So clearly i have no idea what currentLocation is actually returning, and the docs online aren't really much help.

When i put this on the watch and find GPS the whole thing crashes.

Does anyone know what's going on here? Happy to provide more information or code.
  • Former Member
    Former Member over 8 years ago
    Maybe use this code

    https://forums.garmin.com/showthread.php?360798-Current-Location&p=900064#post900064


    And like magic, it runs. Thanks berglauf1234! Now if i could only get the damn sunrise and sunset values to make sense ... :(
  • One thing to remember is the value in Activity.Info for currentLocation goes stale (that's why you see the null checks... It might not have one, or did, but it went stale).
  • Former Member
    Former Member over 8 years ago
    One thing to remember is the value in Activity.Info for currentLocation goes stale (that's why you see the null checks... It might not have one, or did, but it went stale).


    Yeah, nah i'm pretty sure i have that covered using a GPS fix and the object store. And i can put my lat/long in by hand and get the same (wrong) rise/set times (on the watch and in the simulator).

    I'm wondering if it has something to do with being in the Southern Hemisphere, so i might need to work through the math (i'm using the SunCalc code from GitHub as a base). Or perhaps there's another .toFloat() or .toNumber() or whatever that's needed that i'm just unaware of.

    Debugging is fun!
  • One thing to look at is to make sure that lat/lon is passed as they type expected. For example, it might be expecting doubles, but you are passing floats. I ran into that with some code I got off the net.

    You can change the OS with "file>edit Object Store" in the sim, and app settings with App Settings editor in Eclipse. What's the "type" of the value when you read it? (Probably a float)
  • Former Member
    Former Member over 8 years ago
    One thing to look at is to make sure that lat/lon is passed as they type expected. For example, it might be expecting doubles, but you are passing floats. I ran into that with some code I got off the net.

    You can change the OS with "file>edit Object Store" in the sim, and app settings with App Settings editor in Eclipse. What's the "type" of the value when you read it? (Probably a float)


    You are a magician Jim! A simple switch to doubles and it all works, just like that. Thanks!
  • Former Member
    Former Member over 8 years ago
    In case you're interested, here's a working version of the watchface now. Shows a HR graph as well as all the info you could want (date, optional 2nd time - here set to Munich, steps+goal, sunrise/sunset, current/min/max HR, notifications+battery+BT+DND icons).



    I coded it up for myself so not sure yet if i'll make it public in the store. A fun exercise though!

    EDIT: I might put the code up on GitHub if anyone's interested.
  • Looks great!
    You should submit it, why not ;)
  • I was struggling to get this working in the simulator, even when playing back an activity. The fix for me was to enable the position permission in the manifest.

  • Any idea of how long the value is valid for before it goes stale on an actual device? Thanks :)