Picking a map location within a flow

Post your questions and help other users.

Moderator: Martin

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Picking a map location within a flow

Post by Martin » 19 Jul 2014 07:38

No, you can not use the other data contained in a location object to determine entering/exiting so you have to track this flag on your own. t contains the time when the fix was determined (only set by GPS, Network provider, but not when a script creates the location). et contains the elapsed milliseconds since system boot, speed would be shown as vel.

There are a few possibilities to track the state of the location. You could create a global list and add a location as soon as you detect that you are inside the location.
Something like this could work (assume global_locations is a list with all locations you are interested in, global_inside_locations is initially empty and contains the locations you are inside now):
-trigger Periodic Location Update
-action Script

Code: Select all

//initialize missing lists to prevent errors
if (global_locations==null)
{
    global_locations = newList();
}
if (global_locations==null)
{
    global_inside_locations = newList();
}

// entering location check
for (loc in global_locations)
{
    if (distance(loc, location) < 1000)
    {
        if (containsElement(global_inside_locations, loc))
        {
            log("already inside {loc,locationformat,decimal}");
        }
        else
        {
            log("entered {loc,locationformat,decimal}");
            addElement(global_inside_locations, loc);
        }
    }
}

// leaving location check
//copy the list since it's not allowed to modify the list when looping over this list
for (loc in copyList(global_inside_locations))
{
    if (distance(loc, location) >= 1000)
    {
        log("exited {loc,locationformat,decimal}");
        removeElementValue(global_inside_locations, loc);
    }
}
The forum software often prevents to attach flow files since it looks like it contains HTML and/or Javascript which could lead to security problems. You can use menu->Forum and button Publish Flows/Widgets to upload a flow to our server. It will create a link on the clipboard that can be copied into a forum post.

Regards,
Martin

skiptannen
Posts: 82
Joined: 13 Jan 2014 21:39

Re: Picking a map location within a flow

Post by skiptannen » 21 Jul 2014 12:11

Thank you VERY much - after starting to play with this code, it looks like I'll finally be able to put together a flow with the functionality I've been looking for. I really appreciate all of your help.


skip

skiptannen
Posts: 82
Joined: 13 Jan 2014 21:39

Re: Picking a map location within a flow

Post by skiptannen » 21 Jul 2014 22:41

Sorry to keep bothering you, but I keep hitting brick walls. I'm trying to figure out how to add a location to a list, but no matter what syntax I use the value gets added as a string. Can you please give me the syntax for adding locations to a list?

Thanks.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Picking a map location within a flow

Post by Martin » 23 Jul 2014 17:59

Following script should work (I assume that the location is available in variable location):
list = newList();
addElement(list, location);

skiptannen
Posts: 82
Joined: 13 Jan 2014 21:39

Re: Picking a map location within a flow

Post by skiptannen » 23 Jul 2014 18:13

That's exactly the syntax I used, but the problem is that it gets added as a string, not a location. I'm trying to figure out how to modify that syntax to tell the variable that I want to add a location.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Picking a map location within a flow

Post by Martin » 23 Jul 2014 18:32

How do you get the location that you want to add to the list? Possibly it was already a string before adding the location to the string.
Do you enter the coordinates manually somewhere or do you create as location object using function newLocation?

skiptannen
Posts: 82
Joined: 13 Jan 2014 21:39

Re: Picking a map location within a flow

Post by skiptannen » 23 Jul 2014 18:43

I think that's the problem - the location is already a string that I've copied to the clipboard, and I can't figure out how to convert the contents of the clipboard to a location.

User avatar
Martin
Posts: 4468
Joined: 09 Nov 2012 14:23

Re: Picking a map location within a flow

Post by Martin » 23 Jul 2014 19:12

Yep, it's a string when the text comes from the clipboard.

You can use the following script to convert a string like 48.858557,2.294540 to a location object:

Code: Select all

s = "48.858557,2.294540";

//find the position of the comma
i=indexOf(s, ",");

//extract the part before the comma
latitude = substring(s, 0, i);

//extract the part after the comma
longitude = substring(s, i+1);

//create the location
location = newLocation(latitude, longitude);

skiptannen
Posts: 82
Joined: 13 Jan 2014 21:39

Re: Picking a map location within a flow

Post by skiptannen » 23 Jul 2014 19:37

Converting the string to a location is EXACTLY what I needed! That was the missing piece - thank you very much!

User avatar
kintrupf
Posts: 257
Joined: 10 Sep 2013 08:59

Re: Picking a map location within a flow

Post by kintrupf » 28 Jul 2014 13:29

Martin wrote:

Code: Select all

// entering location check
for (loc in global_locations)
{
...
Is there any way to use the locations from the list in a trigger?
I'm looking a way to somehow replace the trigger Location (method: Modern) with something that uses variables, so that I don't need to "hard-code" multiple triggers (with a fixed location each) to my flow.
With the script example above I could at least replace the condition Location within my flow....which is fine!

The only thing I can think of is something like a trigger Phone Cell GSM (maybe combined with Periodic Timer Inexact) and the action Init Variable Location to periodically get the current location and then use the example code above to check if the location is around any of the coordinates in the list.
How would that affect the battery life compared with the fixed set of Location triggers?

Post Reply