Advertisement
br1wr2el3

Code xx - http maps save

May 1st, 2013
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1.  
  2. --# Main
  3. -- http and maps v2
  4.  
  5. -- Bruce Elliott
  6. -- May 2013
  7.  
  8. -- Ideas from Image I/O Example
  9. -- This program demonstrates several things
  10. -- Use of Http.request to get a map using Google map API
  11. -- v2 - zoom is now controllable in the map
  12. -- string.gsub() replaced a lot of code - Thanks to Aciolino
  13. -- Use of paramters to input text and contol functions
  14. -- Use of string functions to process text
  15. -- Use of saveImage to save maps to Documents and Dropbox
  16.  
  17.  
  18. function setup()
  19. -- Use a function to draw address parameter
  20. parameterSet01()
  21.  
  22.  
  23. img = nil
  24. logo = nil
  25. end
  26.  
  27.  
  28. function getMap()
  29. -- Uses address from user to get map
  30. -- using Google maps API (Application Programming Interface)
  31. logo = nil -- it's empty for now
  32. -- print("get map")
  33.  
  34. --Insert address and zoom into API call
  35. http1 = "http://maps.googleapis.com/maps/api/staticmap?center="
  36. http2 = "&zoom="
  37. zoom = Zoom
  38. http3 = "&size=400x400&sensor=false"
  39. request = http1.. addressNew .. http2 .. zoom .. http3
  40.  
  41. -- Use http.request to submit Internet request
  42. http.request(request, didGetLogo)
  43.  
  44. -- http.request( "http://maps.googleapis.com/maps/api/staticmap?center=Berkeley,CA&zoom=14&size=400x400&sensor=false", didGetLogo )
  45.  
  46. end
  47.  
  48. function makeImage()
  49. -- This function makes map sprite
  50. -- Renders it into an image, and returns the image
  51. -- We exapanded the size of img to screen size
  52. local img = image(WIDTH, HEIGHT)
  53. ------------------------------------
  54. -- Use the image as a render target
  55. setContext(img)
  56. background(0,0,0,0) -- transparent background
  57. fill(255,0,0)
  58. -- We draw the sprite to img instead of the screen
  59. sprite( logo, WIDTH/2, HEIGHT/2, WIDTH )
  60. -- ellipse(200,200,200)
  61. -- Set render target back to screen
  62. setContext()
  63. ------------------------------------
  64. return img
  65. end
  66.  
  67. function didGetLogo( theLogo, status, headers )
  68. -- http.request calls this for results
  69. print( "Response Status: " .. status )
  70.  
  71. -- Store map in our global variable
  72. logo = theLogo
  73.  
  74. -- Check if the status is OK (200)
  75. if status == 200 then
  76. print( "Downloaded logo OK" )
  77. print( " Image dimensions: " ..
  78. logo.width .. "x" .. logo.height )
  79. -- Call makeImage once image was downloaded
  80. newMap = makeImage()
  81.  
  82. else
  83. print( "Error downloading logo" )
  84. end
  85. end
  86.  
  87. -- This function gets called once every frame
  88. function draw()
  89. -- This sets a dark background color
  90. background(40, 40, 50)
  91.  
  92. -- Draw the map we downloaded (when it's ready!)
  93. if logo ~= nil then
  94. sprite(newMap, WIDTH/2, HEIGHT/2, WIDTH )
  95. else
  96. ellipse(200,200,200)
  97. end
  98.  
  99. end
  100.  
  101. function parameterSet01()
  102. parameter.clear()
  103. parameter.text("Address", "Fort Worth, TX")
  104. parameter.action("Address Complete", addressDone)
  105. end
  106.  
  107. function parameterSet02()
  108. -- Show adding new parameters to list
  109. parameter.integer("Zoom", 0, 21, 14)
  110. parameter.action("Get Map", getMap)
  111. parameter.text("FileName", fileName)
  112. parameter.action("Filename Complete", filenameDone)
  113.  
  114. -- output.clear()
  115.  
  116. print("Set new zoom level then")
  117. print("press Get Map to move in")
  118. print("or out with the same address")
  119. print("")
  120. print("Press Filename Complete")
  121. print("to save image")
  122. end
  123.  
  124. function addressDone(value)
  125. -- User says address is ready for processing
  126. -- Google API requires "+" instead of spaces
  127. -- store in addressVal
  128. addressVal = Address
  129.  
  130. -- Convert address to all lowercase
  131. addressVal = string.lower(addressVal)
  132. -- Convert spaces to plus "+"
  133. addressNew = string.gsub(addressVal, " ", "+")
  134. print(addressNew)
  135. parameterSet02()
  136. end
  137.  
  138. function filenameDone()
  139. -- Use input filename to create strings
  140. -- to save to Documents abd Dropbox
  141. -- There is no error checking of filename
  142. docName = "Documents:"..FileName
  143. boxName = "Dropbox:"..FileName
  144.  
  145. -- Save to Dropbox or Documents or both
  146. -- Add comments if you do not want to save
  147. -- to a destination
  148. saveImage(boxName, newMap)
  149. saveImage(docName, newMap)
  150.  
  151. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement