Passing lat,long variables to mapview setcenter and addmarker

Hey guys, I am having trouble getting mapview - setcenter and addmarker to recognize a lat,long variable. Would be grateful to anyone out there that could offer up any suggestions. It’s difficult to diagnose since I have to load the apk on the device to test, I’m not able to see the errors. 

 

What Works

This code works just fine on the device:

 

    myMap:setCenter( 30.192729,-81.364483 )

 

    local function mapmarker( event )

    myMap:addMarker( 30.192729,-81.364483 )

    end

 

    timer.performWithDelay( 10000, mapmarker)

 

FYI: I had to add the delay to give the map time to load the marker, otherwise it wouldn’t show the marker on the device.

 

What Doesn’t Work

Now if I replace the actual lat, long numbers with variables, it won’t work. The mapview still works but it defaults to my current locaction.

 

local currentXlatitude = tonumber(decodedData[2]) 

local currentXlongitude = tonumber(decodedData[3])

local location = (currentXlatitude…"," …currentXlongitude)

 

  1. When I print these variables to the console they are holding valid numbers.

print (currentXlatitude)

print (currentXlongitude)

30.192729    

-81.364483    

 

  1. Additionally, I tested the variables  by passing to a map url and they work fine.

mapURL = “http://maps.google.com/maps?q=Hello,!@”…location

 

  1. Just to be sure, I set the variables to actual numbers but that did not work either.

local currentXlatitude =  30.192729    

local currentXlongitude = -81.364483    

 

 

  1. Here is the code with the variable “location”. On the device, the map just defaults to my location, no marker.

 

    myMap:setCenter( location )

 

    local function mapmarker( event )

    myMap:addMarker( location )

    end

 

    timer.performWithDelay( 10000, mapmarker)

 

  1. I also tried the following. Same result on the device, defaults to my location.

 

    myMap:setCenter( currentXlatitude…","…currentXlongitude)

 

    local function mapmarker( event )

    myMap:addMarker( currentXlatitude…","…currentXlongitude)

    end

 

    timer.performWithDelay( 10000, mapmarker)

 

  1. One last thing, I tried adding a delay with setcenter but that did not work either.

 

Thanks in advance for anyone out there who can offer up any suggestions.

Solved: Here is what finally worked. Hope this helps someone else

 

 

            local currentXlatitude = tonumber(data[2])

            local currentXlongitude = tonumber(data[3])

            myMap:setCenter(currentXlatitude, currentXlongitude )

            myMap:addMarker(currentXlatitude, currentXlongitude, options )

Solved: Here is what finally worked. Hope this helps someone else

 

 

            local currentXlatitude = tonumber(data[2])

            local currentXlongitude = tonumber(data[3])

            myMap:setCenter(currentXlatitude, currentXlongitude )

            myMap:addMarker(currentXlatitude, currentXlongitude, options )

Useful - makes clear how to use data-driven parameters with the addMarker function Thanks for sharing.

Useful - makes clear how to use data-driven parameters with the addMarker function Thanks for sharing.