DataField and GPS

Dear all,

I would like to develop a DataField which need to use the GPS. For this I use Visual Studio Code.

I use the following code in compute() function. It works perfectly with timerTime, altitude, currentSpeed, currentHeartRate. However info.currentLocation always returns null.

Where am I wrong?

Thank you in advanced,

Pascal

function compute(info as Activity.Info) as Void {

        // See Activity.Info in the documentation for available information.

       

        if(info has :currentLocation){

            if(info.currentLocation != null)

            {

                var myLocation = info.position.toDegrees();

                mLat = myLocation[0];

                mLon = myLocation[1];                

                if (mLat0 == 180)     // On considère dans ce cas que le GPS n'est toujours pas initialisé

                {

                    mLat0 = mLat;

                    mLon0 = mLon;

                }

 

            } else

            {

               

            }

        }

        System.println(info.timerTime);

        System.println(info.altitude);

        System.println(info.currentSpeed);

        System.println(info.currentLocation);

        System.println(info.currentHeartRate);

        System.println(mLat0);

       

    }

  • Doing my taxes today and I shouldn't post when I'm distracted!  Slight smile

  • Hi Subra,

    I tried to use info.currentLocation.toDegrees()

    However it crashes... I think it is due to the fact that info.currentLocation is null

  • Null is a valid value for this and you need to check for it.

        function compute(info as Activity.Info) as Numeric or Duration or String or Null {
            // See Activity.Info in the documentation for available information.
            if(info.currentLocation!=null) {
            	System.println(info.currentLocation.toDegrees());
            }
            return 0.0;
        }

    Do you have the positioning permission set?  From the API Doc for currentLocation-

    This member will always provide a null value unless the Positioning Permission is enabled.

  • I dont know how to get the positioning permission set...

    I have found enableLocationEvents

    However it is specified that : Request a Location event.

    Using this API requires enabling the Positioning Permission. Only Device Apps and Widgets may use this API.

    Here I am working with DataField

  • It's in the manifest.xml file.  If you are using eclipse, click on the manifest.xml and the tab with permissions.

    In VS Code, it's in the command palette

    Monkey C: Edit Permissions

    You can set the permission for a data field.  You just can't turn on/off GPS - that's what enableLocationEvents is used for.

  • Thanks Jim. I tried (I have VS Code). Now i have another error, However it is not related...

    Error occurred during initialization of VM
    Could not reserve enough space for 1048576KB object heap

    So I will try to fix this new error, but I am on the way. Thanks a lot

  • Sorry, but I don't understand the logic of your code. Maybe you can enlighten me.

    function compute(info as Activity.Info) as Void {

            // See Activity.Info in the documentation for available information.

           

            if(info has :currentLocation){

                if(info.currentLocation != null)

                {

                    var myLocation = info.position.toDegrees();

                    mLat = myLocation[0];

                    mLon = myLocation[1];                

                    if (mLat0 == 180)     // On considère dans ce cas que le GPS n'est toujours pas initialisé

                    {

                        mLat0 = mLat;

                        mLon0 = mLon;

                    }

     

                } else

                {

                   

                }

            }

            System.println(info.timerTime);

            System.println(info.altitude);

            System.println(info.currentSpeed);

            System.println(info.currentLocation);

            System.println(info.currentHeartRate);

            System.println(mLat0);

           

        }

    The compute function has the parameter info which is defined as Activity.Info. The next line checks if info.currentLocation exists, and then the next line checks if info.currentLocation isn't null. The effect is that the if-clause is only executed if info.currentLocation isn't null. So far I have understood this.

    But in the next line var myLocation = info.position.toDegrees() I don't understand.

    1. where does info.position come from? Activity.info has no variable position.
    2. why is info.currentLocation checked for availability and not null, if another variable is used in the next line that isn't checked for null?
    3. why doesn't the compiler complain that info.position is not defined in Activity.info?



    However it crashes... I think it is due to the fact that info.currentLocation is null

    4. if it crashes because info.currentLocation is null, the if-clause was not executed. So it may crash in the else-clause or after that. However, no code is displayed for this section.

  • You only enabled positioning and not everything, correct?  Things like the background permission has side effects if you're not set up for backgrounding.

  • Thank you all. It works !!!!

    To summarize, for novices like me, to develop a DataField using GPS position (under Visual Studio Code)

       A) Monkey C: Edit Permissions > Select Positioning

       B) In the compute function

        function compute(info as Activity.Info) as Void {

            if(info has :currentLocation){

                if(info.currentLocation != null)

                {

                    mLat = info.currentLocation.toDegrees()[0];

                    mLon = info.currentLocation.toDegrees()[1];               

                } else

                {

                }

            }

            System.println(info.timerTime);

            System.println(info.altitude);

            System.println(info.currentSpeed);

            System.println(info.currentHeartRate);

            System.println(mLat);

            System.println(mLon);  

        }

       C) In Connect IQ device Simulator

     

    Simulation > FIT data >  Playback File or Simulate Data

     

    Then info.currentLocation <> null and it is possible to get the Latitude and Longitude.