NAVIADDRESS REST API
Lua
How to use Naviaddress in video games, developed with Corona SDK game engine
Where to start?

Many modern game development frameworks are written on Lua programming language. Let's take a look at how a simple geo game can be developed using the Naviaddress platform and the Corona SDK game engine.

Before start the development you need to download and register the Corona SDK on the Russian version of Corona Labs website.

After Corona has been installed, run the Corona Simulator program.
After Corona has been installed, run the Corona Simulator program.

After starting the simulator click the New Project button and set the name of the new program "navigame". The system will offer to open the source file main.lua in a text editor.

Open the file and add a few lines in the open window:

• load the json package to handle JSON objects

• create a constant indicating the center of the screen (this constant will be useful when we center the text and image on the center of the screen)
    -----------------------------------------------------------------------------------------
    --
    -- main.lua
    --
    -----------------------------------------------------------------------------------------
    
    -- Your code here
    local json = require( "json" )
    local cx = display.contentCenterX 
    Now make a REST API request using the network.request() procedure. As parameters this system procedure accepts the URL for calls, the method ("GET" in our example), and the special handleResponse() procedure for handling, which will be called when the request is made.

    Insert a check into the handleResponse() procedure – actions in the case an error has been detected during the running of the program. If we did everything right, the system will save naviaddress returned from the platform to the event.response variable.
    local function handleResponse( event )
     
        if not event.isError then
            print( "Naviaddress is loaded" )
            -- write the handling instructions for the received naviaddress here
        else
            print( "Error!" )
        end
     
        return
    end
      
    network.request( "https://staging-api.naviaddress.com/api/v1.5/Addresses/7/0022", 
                "GET", handleResponse )
    In order to start using the received response normally, we need to decode it from the JSON format using the json.decode() instruction:
            local response = json.decode( event.response )
            local result = response.result
    Now the decoded response contains a JSON object with the following fields:

    • description - the name of the naviaddress
    • cover - an array of photos from the cover.
    Let's display the data on the phone screen.
            display.newText(result.container,cx,150)
    	display.newText(result.description,cx,200)
    
    Let's display the cover image as well. The Images are stored in the Amazon cloud and can be uploaded using the Corona SDK built-in function network.download(). Since uploading images can take a lot of time, let's make it asynchronous:
    local function handleImage( event )
        if ( event.isError ) then
            print( "Network error - download failed: ", event.response )
    	elseif ( event.phase == "ended" ) then
            myImage = display.newImage( event.response.filename, event.response.baseDirectory, cx, 350 )
            myImage:scale(0.2,0.2)
        end
    end
    
    network.download(result.cover[1].image,"GET",handleImage {}, "image.jpg", system.TemporaryDirectory)
    
    If you did everything right, then text and images of Naviaddress office will be displayed on the screen of your phone or simulator.

    The full program code from the article is available here.

    written by: A.Gershun (09/11/2018)
    Made on
    Tilda