OWM weather location?

Hi, I need advice on how to get current location for weather information via OWM provider?

I have used this in the past:

function getCurrentLocation() {
		var currentLocation = Activity.getActivityInfo().currentLocation;
		
		if(currentLocation != null) {
			return currentLocation.toDegrees();
		}
		return currentLocation;
	}


but if the watch activity was not started, the weather was not available.

Do you have other ways to always get accurate weather location?

Thanks for the ideas...

  • If you are doing a widget or device app, you can just start up GPS if you need the current location.

    If you are doing a watch face, the basics are

    Check Activity.Info.currentLocation.

    if it's not null, save it to Storage and use it.

    if it is null, check storage and if you have a location saved, use it

    if both are null, let the user know.  Using observationLocationPosition can be tricky as Garmin can be set to use a location that's not your phone.

    If both are null, the user needs to start an activity that uses GPS to "prime" currentLocation.  Wait for good GPS and exit the activity.  You'll see a valid value in currentLocation.  currentLocation will go null again after a period of time but will be good for a bit.

  •     var location = null;
    
        if (Toybox has :Weather) {
            var curConditions = Weather.getCurrentConditions();
            if (curConditions != null) {
                location = curConditions.observationLocationPosition;
            }
        }
        if (location == null) {
            location = Activity.getActivityInfo().currentLocation;
        }

  • Thanks for the examples and advice. I'm making a watch face and will try it out...

  • With Weather and currentConsidtions, it can be set to use the phone's location or any fixed location you want, so it may not be your actual location.  That's why if currentLocation==null, you want to start an activity that uses GPS to get your actual location.  It takes less than a minute..  And that's also why you want to cache the location in Store. so you can handle the null case.

    You also want to handle devices that don't have Toybox.Weather.

  • I have a question.

    In the simulator, it happens that when I repeatedly load my project - WF, it shows me an error with the drawTemperature(dc) function.
    Well, if I delete the line: 80, do a refresh, and then insert it again, the WatchFace will load.

    Any ideas what I'm doing wrong or what this function should look like correctly?

    Thanks

    function initialize() {
    		Drawable.initialize({ :identifier => "WeatherDrawable" });
    		
    	}
    
    	public function draw(dc as Graphics.Dc)
    	{
    	 drawTemperature(dc);
    	}

     71    function drawTemperature(dc) 
     72	   {
     73		
     74     var temp = Application.getApp().getProperty("Temp");
     75     var unit = "C";
     76	    if (System.getDeviceSettings().temperatureUnits == System.UNIT_STATUTE){
     77		  unit = "F";
     78		}
     79      	dc.setColor(Graphics.COLOR_WHITE , Graphics.COLOR_TRANSPARENT);
     80      	dc.drawText(50, 100, Graphics.FONT_XTINY,temp.format("%d")+"°"+unit, Graphics.TEXT_JUSTIFY_CENTER);
     81	    }

  • What I suspect is that when you do the getProperty on line 74. you get back null, and then on line 80, when you do the format() you are doing that on a null.  Add a null check.

  • Zero control? now I don't know exactly what you mean, something like temp = null? An example please..?

    Thank you very much

  • After line 74, you could do something like

    temp=(temp!=null) ? temp : -100;

    if the temp value is null, it will show as -100

  • I added line 74 and you're right, I was getting a null value.
    But now the problem is that when the value is null, I see -100, can it be replaced somehow so that I don't see the value, but for example "--".

    Thank you
    I don't have much experience with this yet.

  • if you change the line I posted to

    temp=(temp!=null) ? temp.format(%d") : "--";

    and change line 80 to be

    dc.drawText(50, 100, Graphics.FONT_XTINY,temp+"°"+unit, Graphics.TEXT_JUSTIFY_CENTER); 

    (take the format() out there,), you'll see "--" if it's null.