Built-in location API example

I searched for some examples using the built in location API, but couldn’t find any, so I made an extremely basic program that shows how to use the basic functions/values.

-- Location
function setup()
    location.enable()
    parameter.action("Open in Google Maps", function() openURL("http://www.google.com/maps/place/"..location.latitude..","..location.longitude) end)
end
function draw()
    background(0, 0, 0, 255)
    fill(255, 255, 255, 255)
    strokeWidth(5)
    font("HelveticaNeue-UltraLight")
    fontSize(64)
    textWrapWidth(WIDTH)
    text("Your latitude: "..location.latitude.."°", WIDTH/2, HEIGHT/4*3)
    text("Your longitude: "..location.longitude.."°", WIDTH/2, HEIGHT/2)
    text("Your altitude: "..location.altitude.." m", WIDTH/2, HEIGHT/4)
end

And no, I did not forget to use string.format(), I just wanted a value which is as accurate as possible. :wink:

@Saturn031000 Nice example. I like the jump to Google Maps. Using string.format doesn’t limit your accuracy. Here’s your code using string.format too. How many of those digits are valid, I don’t know. Probably not that many, but string.format doesn’t limit what’s displayed.


supportedOrientations(LANDSCAPE_ANY)

-- Location
function setup()
    location.enable()
    parameter.action("Open in Google Maps", function() openURL("http://www.google.com/maps/place/"..location.latitude..","..location.longitude) end)
end
function draw()
    background(0, 0, 0, 255)
    fill(255)
    text("Your latitude: "..location.latitude.."°", WIDTH/2, HEIGHT/4*3)
    text("Your longitude: "..location.longitude.."°", WIDTH/2, HEIGHT/2)
    text("Your altitude: "..location.altitude.." m", WIDTH/2, HEIGHT/4)
    text(string.format("Your latitude: %.48f",location.latitude), WIDTH/2, HEIGHT/4*3-50)
    text(string.format("Your longitude: %.48f",location.longitude), WIDTH/2, HEIGHT/2-50)
    text(string.format("Your altitude: %.48f",location.altitude), WIDTH/2, HEIGHT/4-50)
end

@dave1707 Huh. I thought that formatting it to a float with more decimal places than the not formatted one would just give the same decimal with 0s at the end.

@qSaturn031000 Just in case you’re interested, here’s something to show the accuracy of displaying the results of a division. You get a few more digits with string.format, but after that it’s just garbage. Of course, the results will vary based on the calculation.


displayMode(FULLSCREEN)

function setup()  
    textMode(CORNER) 
end

function draw()
    background(0, 0, 0, 255)
    fill(255)
    text("Without string.format\
23/47 = "..23/47,100,400)
    text(string.format("Using string.format\
23/47 = %.48f",23/47),100,350)
    text("Actual value\
23/47 = 0.489361702127659574468085106382978723",100,300)
end

@dave1707 Say the value is not really a calculation and is a value, like math.pi or, like above, location.latitude, etc., would string.format() still be garbage, or would it be accurate? Also, is there any way to know how many digits it will be accurate to?

@Saturn031000 Codea uses 64 bit math calculations, so the accuracy of any calculation will be based on that. There was discussions in the forum about that already, but you can always do a google search on 64 bit math precision. It’s going to be about 15 to 17 digits accuracy. That also includes digits before the decimal point.