Syntax of how to get speed?

Hello, I am struggling with correct format of following commands to get speed. May I please ask for help? Following does not work:

Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(:onPosition));
var speed = Position.Info()speed;

Thanks, Jan

  • The first line tells the system to call the onPosition function every second. There is a partially flushed-out example here that shows how to use it. This is a more complete version of that code...

    using Toybox.Position;
    using Toybox.System;
    
    class PositionView extends WatchUi.View
    {
        hidden var mSpeed;
    
        function initialize() {
            View.initialize();
        }
        
        function onShow() {
            Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(:onPosition));
        }
        
        function onHide() {
            Position.enableLocationEvents(Position.LOCATION_DISABLE);
        }
    
        function onPosition(info) {
            var myLocation = info.position.toDegrees();
            System.println("Latitude: " + myLocation[0]); // e.g. 38.856147
            System.println("Longitude: " + myLocation[1]); // e.g. -94.800953
            
            mSpeed = info.speed;
            WatchUi.requestUpdate();
        }
        
        function onUpdate(dc) {
            // display mSpeed here
        }
    }

    If you don't want to get called every second with position updates, you can query the position info from the system by calling Position.getInfo()...

    using Toybox.Position;
    using Toybox.System;
    
    class PositionView extends WatchUi.View
    {
        function initialize() {
            View.initialize();
        }
        
        function onShow() {
            Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(:onPosition));
        }
        
        function onHide() {
            Position.enableLocationEvents(Position.LOCATION_DISABLE);
        }
    
        function onPosition(info) {
            var myLocation = info.position.toDegrees();
            System.println("Latitude: " + myLocation[0]); // e.g. 38.856147
            System.println("Longitude: " + myLocation[1]); // e.g. -94.800953
            
            WatchUi.requestUpdate();
        }
        
        function onUpdate(dc) {
            var positionInfo = Position.getInfo();
            
            // display positionInfo.speed here
        }
    }