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

  • Here's a very simple example of how to do this:

    using Toybox.WatchUi;
    using Toybox.Graphics as Gfx;
    
    class SpeedView extends WatchUi.View {
    	var width,height;
    	var speed=0.0;
    	
        function initialize() {
            View.initialize();
            
            Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(:onPosition));
            
    		var timer= new Timer.Timer();
    		timer.start( method(:onTimer), 1000, true );        
        }
        
        function onPosition(info) {
        	if(info.speed!=null) {speed=info.speed;}
        }
        
        function onTimer() {
        	WatchUi.requestUpdate();
        }
    
        // Load your resources here
        function onLayout(dc) {
    		width=dc.getWidth();
    		height=dc.getHeight();
        }
    
        function onShow() {
        }
    
        // Update the view
        function onUpdate(dc) {
    		dc.setColor(Gfx.COLOR_BLACK,Gfx.COLOR_BLACK);
    		dc.clear();
    		dc.setColor(Gfx.COLOR_BLUE,Gfx.COLOR_TRANSPARENT);
    		dc.drawText(width/2,height/2,Gfx.FONT_SMALL,"speed="+speed,Gfx.TEXT_JUSTIFY_CENTER|Gfx.TEXT_JUSTIFY_VCENTER);
        }
    
        function onHide() {
        }
    
    }

    You need the positioning permission.  In initialize, I start GPS as well as a one second time.  The onPosition callback occurs every second, and the timer is used to update the display every second.

    At the end of onPosition, WatchUi.requestUpdate could be called and the timer wouldn't be needed.

  • Hi, I'm curious is it able to get speed by accel? Cause I want speed when I'm on a treadmill

    After I turned off the Position.enableLocationEvents(Position.LOCATION_CONTINUOUS, method(onPosition));

    my build app speed on my watch forerunner 95 is always null.

    I've tried Sensor.getInfo() and Activity.getActivityInfo() of the speed 

    how can I get the speed indoor? thx in advance!