Sun rise / sunset

Could some kind person provide an actual working example of how to get sunrise/sunset info for a watch face? TIA!
  • Hi

    I'm trying to use this excellent class for calculating sunrise / sunset, and it looks fairly easy thanks to this class.

    But I have a small problem.

    When I add the code below, I get an errormessage: Permission required.
    var info = Position.getInfo();

    But when I try to add permission for positioning in the manifest file, this item is greyed out and can't be selected.

    App minimum SDK version is set to 1.3.X and build target is set to fenix 3 HR

    What am I doing wrong here?

    Thanks a lot in advance

    Kenth

    You are probably trying to use this code in a watch face. Position is not available to watch faces. If you've read the earlier posts in this thread, you would have found your answer. See this post by Jim
  • Former Member
    Former Member over 8 years ago
    Great, now it works as expected, thanks for the support.

    I have added my code below, to help other users.

    var sc = new SunCalc();

    var loc = Toybox.Activity.getActivityInfo().currentLocation;
    if(loc != null)
    {
    var latlon = loc.toRadians();
    var now = new Time.Moment(Time.now().value());

    var sunrise_moment = sc.calculate(now, latlon[0], latlon[1], SUNRISE);
    var sunset_moment = sc.calculate(now, latlon[0], latlon[1], SUNSET);

    var timeInfoSunrise = Greg.info(sunrise_moment, Time.FORMAT_SHORT);
    var timeInfoSunset = Greg.info(sunset_moment, Time.FORMAT_SHORT);

    sunInfoString = timeInfoSunrise.hour.format("%01d") + ":" + timeInfoSunrise.min.format("%02d") + " - " + timeInfoSunset.hour.format("%01d") + ":" + timeInfoSunset.min.format("%02d");
    }


    But as far as I can understand, the Toybox.Activity.getActivityInfo().currentLocation will only store my last GPS location for a period of time, and if the position is available I should save it in the object store, so I can use it later when it's not available anymore.
    But how do I do this?

    Thanks
  • save with a variable, sample : var lastLoc
  • Former Member
    Former Member over 8 years ago
    cuurentLocation in Watch Face !

    How to use

    var loc = Toybox.Activity.getActivityInfo().currentLocation;
    in Watch face !
    doesn't work !
    loc = null !
  • How to use

    var loc = Toybox.Activity.getActivityInfo().currentLocation;
    in Watch face !
    doesn't work !
    loc = null !


    That means you have to first get a GPS fix.
    Do that by going into activity mode and wait for GPS signal, then return to watch face.

    That is why code checks for loc != null

    EDIT:
    ------
    Oh, and btw, it will always be null in simulator. Only works on real device assuming GPS fix was obtained.
  • Former Member
    Former Member over 8 years ago
    That means you have to first get a GPS fix.
    Do that by going into activity mode and wait for GPS signal, then return to watch face.

    That is why code checks for loc != null

    EDIT:
    ------
    Oh, and btw, it will always be null in simulator. Only works on real device assuming GPS fix was obtained.


    What's GPS fix ? i must get a API before !
    i try also to put the apps on my real Watch EPIX and no return value !
    thanks for your help !
  • What's GPS fix ? i must get a API before !
    i try also to put the apps on my real Watch EPIX and no return value !
    thanks for your help !


    You have to manually on the watch acquire a GPS signal in an activity. Once the GPS signal has been acquired / fixed / locked, you can return to the watch face.

    The currentLocation is then available in the watch face for a short while.
    Best to save the location to the object store.
  • Former Member
    Former Member over 8 years ago
    You have to manually on the watch acquire a GPS signal in an activity. Once the GPS signal has been acquired / fixed / locked, you can return to the watch face.

    The currentLocation is then available in the watch face for a short while.
    Best to save the location to the object store.


    It works but i think using activity.getinfo() is not good.
    if you use...
    info.position.toRadians(); is better but not available on Watch face.


    I am here lat and long and if i fixed values; the results is not the real sunrise and sunset

    var lat = 50.283333;
    var long = 2.783333;

    var curLoc = Activity.getActivityInfo().currentLocation;
    if (curLoc != null) {
    long = curLoc.toRadians()[1].toFloat();
    lat= curLoc.toRadians()[0].toFloat();
    Sys.println(lat);
    Sys.println(long);
    }


    var latlon = new [2];
    latlon[0] = lat;
    latlon[1] = long;

    now = new Time.Moment(Time.now().value());

    var sunrise_moment = sc.calculate(now, latlon[0], latlon[1], SUNRISE);
    var sunset_moment = sc.calculate(now, latlon[0], latlon[1], SUNSET);

    var timeInfoSunrise = Calendar.info(sunrise_moment, Time.FORMAT_SHORT);
    var timeInfoSunset = Calendar.info(sunset_moment, Time.FORMAT_SHORT);

    sunInfoString = timeInfoSunrise.hour.format("%01d") + ":" + timeInfoSunrise.min.format("%02d") + " - " + timeInfoSunset.hour.format("%01d") + ":" + timeInfoSunset.min.format("%02d");
  • Using hard code values for my location in lat/lon shows incorrect sunrise and sunset times. Has anyone got a working example to share?

    Ta
  • I made barrel with sunrise / sunset code. Here is simple code how use it

    using SunPhases as Sun;



    function onUpdate(dc) {

    View.onUpdate(dc);

    var width = dc.getWidth();

    var height = dc.getHeight();



    dc.setColor(Graphics.COLOR_BLACK, Graphics.COLOR_BLACK);

    dc.clear();



    var sundata = Sun.vypocet();


    dc.setColor(Graphics.COLOR_WHITE, Graphics.COLOR_TRANSPARENT);

    dc.drawText(width/2, height/2 - 10, Gfx.FONT_XTINY, sundata[1], Gfx.TEXT_JUSTIFY_CENTER | Gfx.TEXT_JUSTIFY_VCENTER);

    dc.drawText(width/2, height/2 + 10, Gfx.FONT_XTINY,sundata[0], Gfx.TEXT_JUSTIFY_CENTER | Gfx.TEXT_JUSTIFY_VCENTER);

    }

    }