Task Walkthough: Retrieving current location as a place name and postcode (UK)

2,028 views
Skip to first unread message

CodeMonkey

unread,
Mar 24, 2011, 3:08:30 PM3/24/11
to Tasker
Having read (and agreed with others) that we're losing our
creativity....

Thought I'd step up and post this here (having just written it all out
on the xda developers forum, probably unnecessarily lol)..

This is a task I wrote having been inspired by users on this forum
using various weather APIs to get forecast information.
I display the retrieved information using Minimalistic Text widget
(waiting for Zoom to come out so I can switch) - the display update is
triggered on a 'Variable set' context for %LOCNAME

The task itself is pretty reliable where a data connection is
available and uses the following user variables:
- %LOCNAME : this holds the retrieved town / place name to be
displayed
- %PCODE : this holds the retrieved postcode (no spaces). I use this
elsewhere in Tasker to get a forecast using Google's weather API
- %LASTLOCN : I use this to ensure I only get location where there has
been a change (in the triggering profile)

Enough talk, on with the show...

Task GetPlaceName
1. Variable Clear %LOCNAME
2. Variable Clear %PCODE
3. HTTP GET:
Port: maps.google.com/maps/api/geocode/json?latlng=%LOCN&sensor=false
Timeout: 10
Mime Type: text/xml
4. Variable Split %HTTPD
Splitter: ""types": [ "postal_code" ]
5. Variable Split %HTTPD1
Splitter: "locality",
Delete Base
6. Variable Split %HTTPD11
Splitter: }, {
Delete Base
7. Variable Clear %HTTPD12
8. Variable Clear %HTTPD13
9. Variable Clear %HTTPD14
10. Variable Clear %HTTPD15
11. Variable Clear %HTTPD16
12. Variable Clear %HTTPD111
13. Variable Clear %HTTPD114
14. Variable Clear %HTTPD115
15. Variable Clear %HTTPD116
16. IF %HTTPD113 Is SET
17. Variable Split %HTTPD113
Splitter: "short_name": "
Delete Base
18. Variable Clear %HTTPD1131
19. Variable Split %HTTPD1132
Splitter: ",
Delete Base
20. Variable Set %LOCNAME to %HTTPD11321
21. Variable Clear %HTTPD11321
22. Variable Clear %HTTPD11322
23. Variable Clear %HTTPD11323
24. Variable Clear %HTTPD112
25. End If
26. IF %LOCNAME ! Set
27. Variable Split %HTTPD112
Splitter: "short_name": "
Delete Base
28. Variable Clear %HTTPD1121
29. Variable Split %HTTPD1122
Splitter: ",
Delete Base
30. Variable set %LOCNAME to %HTTPD11221
31. Variable Clear %HTTPD11221
32. Variable Clear %HTTPD11222
33. End If
34. Variable Split %HTTPD2
Splitter: "short_name": "
Delete Base
35. Variable Clear %HTTPD21
36. Variable Split %HTTPD22
Splitter: ",
Delete Base
37. Variable Split %HTTPD221
Splitter:
Delete Base
38. Variable Set %PCODE to %HTTPD2211%HTTPD2212
39. Variable Clear %HTTPD2211
40. Variable Clear %HTTPD2212
41. Variable Clear %HTTPD3
42. Variable Clear %HTTPD4
43. Variable Clear %HTTPD5
44. Variable Clear %HTTPD6
45. Variable Clear %HTTPD7
46. Variable Set %LASTLOCN to %LOCN
=================================

Some Debugging Notes
If you have problems getting this working:

1. Check your splitters - I have written them here as Splitter:
everything after the : and space is the splitter

2. Check what you're getting back from the HTTPGet by using a popup to
get your %LOCN and feeding that into a browser to get the XML back on
your laptop / PC.

3. There are a lot of variable clears here because I like to clean up
after myself, they can muddy the waters when debugging though so you
might want to add them in afterwards.

4. I do get the very occasional failure (which displays "Unknown" in
my widget). This is probably due to a couple of towers with dodgy data
near my office (yes O2, I'm looking at you...)

5. This comes with no guarantees - I built it using some Google
documentation and a lot of patience. YMMV

david morin

unread,
Jan 8, 2012, 11:19:42 AM1/8/12
to tas...@googlegroups.com
think you could send me the xml?

CodeMonkey

unread,
Jan 8, 2012, 1:21:30 PM1/8/12
to Tasker
Done

sayling

unread,
Aug 1, 2012, 8:12:32 AM8/1/12
to tas...@googlegroups.com
Variable Clear %HTTPD*

New since the first post was made last year, yes :)

Richard Wallace

unread,
Feb 11, 2014, 6:06:31 PM2/11/14
to tas...@googlegroups.com

I know this is an ancient post but just wondered if the xml files were available to send?

Bob Hansen

unread,
Feb 11, 2014, 6:40:58 PM2/11/14
to tas...@googlegroups.com
I don't know what all that profile pulled, but here is a much much shorter version that works for me.

get loc name (134)
        A1: HTTP Get [ Server:Port:http://maps.google.com/maps/api/geocode/json?latlng=%LOCN&sensor=false Path: Attributes: Cookies: Timeout:10 Mime Type: Output File: Ignore SSL Anchor Errors:Off ]
        A2: Variable Set [ Name:%httpd To:%HTTPD Do Maths:Off Append:Off ]
        A3: Variable Search Replace [ Variable:%httpd Search:(?<="formatted_address" : ").+(?=",) Ignore Case:Off Multi-Line:Off One Match Only:Off Store Matches In:%match Replace Matches:Off Replace With: ]
        A4: Flash [ Text:%match1 Long:Off ]

Note: It is critical that the Search parameter in A3 be exactly as shown or it will fail.

Richard Wallace

unread,
Feb 12, 2014, 5:03:23 AM2/12/14
to tas...@googlegroups.com
Excellent thanks. works a treat.
Is there a way to shorten this to get say, just the town? Or even break it down, to street, town, postcode?

I just then need to figure out how to get this to work on a map in a scene I'm working on as a car dock app!

Bob Hansen

unread,
Feb 12, 2014, 6:07:28 AM2/12/14
to tas...@googlegroups.com
There are other parts in the %match array 2 - 8. I believe that 4 will provide what you want.

Here is a modified version of the task that will show some additional parts.

get loc name (134)
A1: HTTP Get [ Server:Port:http://maps.google.com/maps/api/geocode/json?latlng=%LOCN&sensor=false Path: Attributes: Cookies: Timeout:10 Mime Type: Output File: Ignore SSL Anchor Errors:Off ]
A2: Variable Set [ Name:%httpd To:%HTTPD Do Maths:Off Append:Off ]
A3: Variable Search Replace [ Variable:%httpd Search:(?<="long_name" : ").+(?=",) Ignore Case:Off Multi-Line:Off One Match Only:Off Store Matches In:%match Replace Matches:Off Replace With: ]
< "types" : [ "neighborhood", "political" ]>
A4: Variable Search Replace [ Variable:%httpd Search:(?<=types" : \[ ).+(?=\]) Ignore Case:Off Multi-Line:Off One Match Only:Off Store Matches In:%types Replace Matches:Off Replace With: ]
A5: For [ Variable:%item Items:1:8 ]
A6: Flash [ Text:%item
%match(%item)
%types(%item) Long:Off ]
A7: End For


CazMaz

unread,
Feb 12, 2014, 8:12:06 PM2/12/14
to tas...@googlegroups.com
Hi Bob and all others,

For those of us still playing with the older tasker 1.6u2m, you just have to add a readfile step, after modifying the HTTP GET.
In the older Tasker, %HTTPD has a 4k limit, and the resulting data returned is a little over 8K.

This could also be used as a HTTP POST along with https, which might be a more secure way of getting the data.
Thank you Bob, the search filter works great!

GoogleLocation (100)
Abort Existing Task, Stay Awake
A1: Secure Settings [ Configuration:Screen & Keyboard Lights On 5 Minutes Package:com.intangibleobject.securesettings.plugin Name:Secure Settings ]
A2: Secure Settings [ Configuration:GPS Enabled Package:com.intangibleobject.securesettings.plugin Name:Secure Settings ]
A3: Get Location [ Source:GPS Timeout (Seconds):65 Continue Task Immediately:Off Keep Tracking:Off ]
A4: HTTP Get [ Server:Port:http://maps.google.com/maps/api/geocode/json?latlng=%LOC&sensor=false Path: Attributes: Cookies: Timeout:10 Mime Type: Output File:./googleloc.txt ]
A5: Read File [ File:googleloc.txt To Var:%httpd ]
A6: Variable Set X [ Name:%httpd To:%HTTPD Do Maths:Off Append:Off ]
A7: Variable Search Replace [ Variable:%httpd Search:(?<="formatted_address" : ").+(?=",) Ignore Case:Off Multi-Line:Off One Match Only:Off Store Matches In:%match Replace Matches:Off Replace With:] ]
A8: Flash [ Text:%match1 Long:Off ]
 Here's the response from Google on my location using a GPS %LOC location instead of a network, %LOCN.

Some numbers are changed to keep the hate mail to a minimum, but the lat and lon are there, so I suppose a nuclear strike is still possible!
I posted this so folks can see how to change the search criteria and match what they are looking for, to unvail the results in a human readable format, so to speak.

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "0000",
               "short_name" : "0000",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "C Road",
               "short_name" : "C Rd",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Edgerton",
               "short_name" : "Edgerton",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Center",
               "short_name" : "Center",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Williams",
               "short_name" : "Williams",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Ohio",
               "short_name" : "OH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "43517",
               "short_name" : "43517",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "0000 C Road, Edgerton, OH 43517, USA",
         "geometry" : {
            "location" : {
               "lat" : 41.455537,
               "lng" : -84.672209
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.4568859802915,
                  "lng" : -84.67086001970848
               },
               "southwest" : {
                  "lat" : 41.4541880197085,
                  "lng" : -84.67355798029151
               }
            }
         },
         "types" : [ "street_address" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Center",
               "short_name" : "Center",
               "types" : [ "administrative_area_level_3", "political" ]
            },
            {
               "long_name" : "Williams",
               "short_name" : "Williams",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Ohio",
               "short_name" : "OH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Center, OH, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 41.5140029,
                  "lng" : -84.573621
               },
               "southwest" : {
                  "lat" : 41.4264499,
                  "lng" : -84.691599
               }
            },
            "location" : {
               "lat" : 41.4559947,
               "lng" : -84.6269824
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.5140029,
                  "lng" : -84.573621
               },
               "southwest" : {
                  "lat" : 41.4264499,
                  "lng" : -84.691599
               }
            }
         },
         "types" : [ "administrative_area_level_3", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "43517",
               "short_name" : "43517",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Edgerton",
               "short_name" : "Edgerton",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Ohio",
               "short_name" : "OH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Edgerton, OH 43517, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 41.519236,
                  "lng" : -84.62447490000001
               },
               "southwest" : {
                  "lat" : 41.375299,
                  "lng" : -84.804484
               }
            },
            "location" : {
               "lat" : 41.4617811,
               "lng" : -84.75244819999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.519236,
                  "lng" : -84.62447490000001
               },
               "southwest" : {
                  "lat" : 41.375299,
                  "lng" : -84.804484
               }
            }
         },
         "types" : [ "postal_code" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Williams",
               "short_name" : "Williams",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Ohio",
               "short_name" : "OH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Williams, OH, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 41.705816,
                  "lng" : -84.34166399999999
               },
               "southwest" : {
                  "lat" : 41.426021,
                  "lng" : -84.806209
               }
            },
            "location" : {
               "lat" : 41.55076020000001,
               "lng" : -84.5222189
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.705816,
                  "lng" : -84.34166399999999
               },
               "southwest" : {
                  "lat" : 41.426021,
                  "lng" : -84.806209
               }
            }
         },
         "types" : [ "administrative_area_level_2", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "Ohio",
               "short_name" : "OH",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Ohio, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 41.9773019,
                  "lng" : -80.51819999999999
               },
               "southwest" : {
                  "lat" : 38.4034229,
                  "lng" : -84.8203049
               }
            },
            "location" : {
               "lat" : 40.4172871,
               "lng" : -82.90712300000001
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 41.9773019,
                  "lng" : -80.51819999999999
               },
               "southwest" : {
                  "lat" : 38.4034229,
                  "lng" : -84.8203049
               }
            }
         },
         "types" : [ "administrative_area_level_1", "political" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "United States",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 90,
                  "lng" : 180
               },
               "southwest" : {
                  "lat" : -90,
                  "lng" : -180
               }
            },
            "location" : {
               "lat" : 37.09024,
               "lng" : -95.712891
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 49.38,
                  "lng" : -66.94
               },
               "southwest" : {
                  "lat" : 25.82,
                  "lng" : -124.39
               }
            }
         },
         "types" : [ "country", "political" ]
      }
   ],
   "status" : "OK"
}

CazMaz

unread,
Feb 12, 2014, 9:07:18 PM2/12/14
to tas...@googlegroups.com
I all,

After I posted to this forum, it occurred to me that Google may not accept the HTTP POST method, which is strange, or I am not aware of something about using the POST METHOD.
As it happens, they do not as I read other folks post in other forums in reference to static maps anyway.
So, I tried the several flavors of the HTTP POST method in Tasker 1.6u2m and could only receive an error messages that stated "sensor had to be true or false" (which I had set to false), and continued with results[]...blah blah blah
So, the posting method reached maps.google...json?, but wouldn't return anything useful, so I have to conclude that google maps won't accept POST METHOD :(,
or there is a need for an api key, or something illusive to me anyway.

Use the HTTP GET,
I hope the post of the return data was helpful,
CazMaz

Richard Wallace

unread,
Feb 13, 2014, 11:46:16 AM2/13/14
to tas...@googlegroups.com
Thanks for your help.
But i get lost at A3 action.
where does this go...< "types" : [ "neighborhood", "political" ]>
As it says Replace matches off, Replace with: ]
 
Not sure if its the way the text has become fortmatted oddly
 
Any chance yuo couldpost / send an xml?

Bob Hansen

unread,
Feb 13, 2014, 11:55:53 AM2/13/14
to tas...@googlegroups.com
>But i get lost at A3 action.
>where does this go...< "types" : [ "neighborhood", "political" ]>

You don't need the types part at all. I included it is a label and goes in the label field.

Richard Wallace

unread,
Feb 13, 2014, 2:23:34 PM2/13/14
to tas...@googlegroups.com
Sorry to be annoying but I still don't follow.
Any chance of an xml file

Richard Wallace

unread,
Feb 13, 2014, 2:29:39 PM2/13/14
to tas...@googlegroups.com
I took that out and added it to the label field and it still doesn't work.
It just flashes
%item
%match(0)
%types(0)exactly like that.

Bob Hansen

unread,
Feb 13, 2014, 3:45:41 PM2/13/14
to tas...@googlegroups.com

Richard Wallace

unread,
Feb 13, 2014, 4:33:30 PM2/13/14
to tas...@googlegroups.com
Thanks very much Bob.
Really appreciate that.
Hopefully this will help me learn!

Mike L

unread,
Feb 14, 2014, 1:00:43 PM2/14/14
to tas...@googlegroups.com
I made a little javascript/task to geocode things a while ago. I'll post it here for future reference.


Geocode (316)
<par1 = "%LOC" or "123 Whatever St, Meriden, CT"

par2 = any combo of the following with any seperator: address, st_num, street, city, state, country, zip, lat, long>
A1: Anchor
A2: JavaScriptlet [ Code:var patt = /-?\d+\.\d+,-?\d+\.\d+/g;

var param = (patt.test(par[0])) ? "latlng=" : "address=";
var query = par[0].replace(" ", "+");

var url = "http://maps.googleapis.com/maps/api/geocode/json?" + param + query + "&sensor=false";

xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", url, false);
xmlHttp.send(null);

var data = JSON.parse(xmlHttp.responseText);

var address = data.results[0].formatted_address;
var lat = String(data.results[0].geometry.location.lat);
var lng = String(data.results[0].geometry.location.lng);

var a = data.results[0].address_components.filter(function (val) {
    return val.types[0] === "street_number";
});
var st_num = a[0].long_name;

var b = data.results[0].address_components.filter(function (val) {
    return val.types[0] === "route";
});
var street = b[0].long_name;

var c = data.results[0].address_components.filter(function (val) {
    return val.types[0] === "locality";
});
var city = c[0].long_name;

var d = data.results[0].address_components.filter(function (val) {
    return val.types[0] === "administrative_area_level_2";
});
var county = d[0].long_name;

var e = data.results[0].address_components.filter(function (val) {
    return val.types[0] === "administrative_area_level_1";
});
var state = e[0].long_name;

var f = data.results[0].address_components.filter(function (val) {
    return val.types[0] === "country";
});
var country = f[0].long_name;

var g = data.results[0].address_components.filter(function (val) {
    return val.types[0] === "postal_code";
});
var zip = g[0].long_name;

var x = (par[1] == "all") ? "address,st_num,street,county,city,state,country,zip,lat,lng" : par[1];

var response = x.replace(/(address|st_num|street|county|city|state|country|zip|lat|lng)/gi,

function ($1) {
    switch ($1) {
        case 'address':
            return address;
        case 'st_num':
            return st_num;
        case 'street':
            return street;
        case 'hood':
            return hood;
        case 'city':
            return city;
        case 'state':
            return state;
        case 'country':
            return country;
        case 'zip':
            return zip;
        case 'lat':
            return lat;
        case 'lng':
            return lng;
    }
}); Libraries: Auto Exit:On Timeout (Seconds):45 ]
A3: Return [ Value:%response Stop:On ]

kinthiri

unread,
Jun 9, 2014, 1:33:46 AM6/9/14
to tas...@googlegroups.com
I love the idea of this javascriptlet, the concept works for what I need. If I get a Lat/Long pair, get the address. If I get an address, fetch the lat/long pair.

I created a Task that contains this scriptlet. I then call that task using Perform Task, passing the address or lat/long in %par1 and %par2 as "address" or "lat"/"long" as desired. I think figured that your A3 would return the contents of the %response variable to the calling Task and I could use it that way. However, my calling task is currently getting "%response" back as text. Not the resolved values from the scriptlet.

I even tried replacing the "var response=x.replace(...);" with "tk.setLocal("%response", x.replace...);" to see if that would make a difference.

Does anyone have any insight on how I can get this scriptlet to work?

Brandon Horwath

unread,
Jun 9, 2014, 1:50:34 AM6/9/14
to tas...@googlegroups.com
Richard,

If you're trying to get this to work with a map element in a scene, you'll need lat/long values to have your position indicated on the map within the scene. An 'address' on the map in tasker won't set your position marker in the scene.

Reply all
Reply to author
Forward
0 new messages