How to get the user's location

This feels like a very simple entry-level question, but I have looked at this everywhere in the forum and the documentation and I haven’t found a satisfactory answer. When the user launches the app, how can I obtain is location? E.g. I would like to know which country the user is in. How do I accomplish this?

This might help:
You can use the GPS Poller to get the Geolocation (latitude, longitude), and use that to make a GET request to Google Maps api.

For example, if your latitude=55.704093 and longitude=13.193582, then you could do a HTTP GET request to:
http://maps.googleapis.com/maps/api/geocode/json?latlng=” + STRING(pageVar_Latitude) + “,” + STRING(pageVar_Longitude) + “&sensor=false”

It will return a pretty large JSON document that looks something similar to this:

{
“results”:[
{
“address_components”:[
{
“long_name”:“6”,
“short_name”:“6”,
“types”:[
“street_number”
]
},
{
“long_name”:“Kyrkogatan”,
“short_name”:“Kyrkogatan”,
“types”:[
“route”
]
},
{
“long_name”:“Lund”,
“short_name”:“Lund”,
“types”:[
“locality”,
“political”
]
},
{
“long_name”:“Lund”,
“short_name”:“Lund”,
“types”:[
“administrative_area_level_2”,
“political”
]
},
{
“long_name”:“Skåne County”,
“short_name”:“Skåne County”,
“types”:[
“administrative_area_level_1”,
“political”
]
},
{
“long_name”:“Sweden”,
“short_name”:“SE”,
“types”:[
“country”,
“political”
]
},
{
“long_name”:“22222”,
“short_name”:“22222”,
“types”:[
“postal_code”
]
},
{
“long_name”:“Lund”,
“short_name”:“Lund”,
“types”:[
“postal_town”
]
}
],
“formatted_address”:“Kyrkogatan 6, 222 22 Lund, Sweden”,
“geometry”:{
“location”:{
“lat”:55.7040564,
“lng”:13.1936853
},
“location_type”:“ROOFTOP”,
“viewport”:{
“northeast”:{
“lat”:55.7054053802915,
“lng”:13.1950342802915
},
“southwest”:{
“lat”:55.7027074197085,
“lng”:13.1923363197085
}
}
},
“types”:[
“street_address”
]
},
{
“address_components”:[
{
“long_name”:“Lund Domkyrkan”,
“short_name”:“Lund Domkyrkan”,
“types”:[
“bus_station”,
“transit_station”,
“establishment”
]
},
}

}

The logic to derive the country from this would be a little complex, but at some point you need to do an IF condition on the Object to check: IS_IN_ARRAY(object.types, “country”) then get the Country Name from the “long_name” field. Hope this can help you.

1 Like

Hey @JOHN_WORSHAM, thanks a lot for this! The only caveat to your answer is that it requires me to get a google API key and enable billing. Do you know of any free alternative?