Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --# Main
- -- http and maps v2
- -- Bruce Elliott
- -- May 2013
- -- Ideas from Image I/O Example
- -- This program demonstrates several things
- -- Use of Http.request to get a map using Google map API
- -- v2 - zoom is now controllable in the map
- -- string.gsub() replaced a lot of code - Thanks to Aciolino
- -- Use of paramters to input text and contol functions
- -- Use of string functions to process text
- -- Use of saveImage to save maps to Documents and Dropbox
- function setup()
- -- Use a function to draw address parameter
- parameterSet01()
- img = nil
- logo = nil
- end
- function getMap()
- -- Uses address from user to get map
- -- using Google maps API (Application Programming Interface)
- logo = nil -- it's empty for now
- -- print("get map")
- --Insert address and zoom into API call
- http1 = "http://maps.googleapis.com/maps/api/staticmap?center="
- http2 = "&zoom="
- zoom = Zoom
- http3 = "&size=400x400&sensor=false"
- request = http1.. addressNew .. http2 .. zoom .. http3
- -- Use http.request to submit Internet request
- http.request(request, didGetLogo)
- -- http.request( "http://maps.googleapis.com/maps/api/staticmap?center=Berkeley,CA&zoom=14&size=400x400&sensor=false", didGetLogo )
- end
- function makeImage()
- -- This function makes map sprite
- -- Renders it into an image, and returns the image
- -- We exapanded the size of img to screen size
- local img = image(WIDTH, HEIGHT)
- ------------------------------------
- -- Use the image as a render target
- setContext(img)
- background(0,0,0,0) -- transparent background
- fill(255,0,0)
- -- We draw the sprite to img instead of the screen
- sprite( logo, WIDTH/2, HEIGHT/2, WIDTH )
- -- ellipse(200,200,200)
- -- Set render target back to screen
- setContext()
- ------------------------------------
- return img
- end
- function didGetLogo( theLogo, status, headers )
- -- http.request calls this for results
- print( "Response Status: " .. status )
- -- Store map in our global variable
- logo = theLogo
- -- Check if the status is OK (200)
- if status == 200 then
- print( "Downloaded logo OK" )
- print( " Image dimensions: " ..
- logo.width .. "x" .. logo.height )
- -- Call makeImage once image was downloaded
- newMap = makeImage()
- else
- print( "Error downloading logo" )
- end
- end
- -- This function gets called once every frame
- function draw()
- -- This sets a dark background color
- background(40, 40, 50)
- -- Draw the map we downloaded (when it's ready!)
- if logo ~= nil then
- sprite(newMap, WIDTH/2, HEIGHT/2, WIDTH )
- else
- ellipse(200,200,200)
- end
- end
- function parameterSet01()
- parameter.clear()
- parameter.text("Address", "Fort Worth, TX")
- parameter.action("Address Complete", addressDone)
- end
- function parameterSet02()
- -- Show adding new parameters to list
- parameter.integer("Zoom", 0, 21, 14)
- parameter.action("Get Map", getMap)
- parameter.text("FileName", fileName)
- parameter.action("Filename Complete", filenameDone)
- -- output.clear()
- print("Set new zoom level then")
- print("press Get Map to move in")
- print("or out with the same address")
- print("")
- print("Press Filename Complete")
- print("to save image")
- end
- function addressDone(value)
- -- User says address is ready for processing
- -- Google API requires "+" instead of spaces
- -- store in addressVal
- addressVal = Address
- -- Convert address to all lowercase
- addressVal = string.lower(addressVal)
- -- Convert spaces to plus "+"
- addressNew = string.gsub(addressVal, " ", "+")
- print(addressNew)
- parameterSet02()
- end
- function filenameDone()
- -- Use input filename to create strings
- -- to save to Documents abd Dropbox
- -- There is no error checking of filename
- docName = "Documents:"..FileName
- boxName = "Dropbox:"..FileName
- -- Save to Dropbox or Documents or both
- -- Add comments if you do not want to save
- -- to a destination
- saveImage(boxName, newMap)
- saveImage(docName, newMap)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement