Storing var values in markers

I need to store custom some values in marker to access them with the listener call, but it doesnt work, How can i do it? I am trying this…

local function markerListener(event)
  native.showAlert( “test value”,event.test , { “ok” } )
end

    local options = {
                            title = “whatever”,
                            subtitle = “whatever”,
                            listener = markerListener,
                            imageFile = “marker.png”,
                            test=“my value”
                        }
                        
                        
map:addMarker( lat, lon, options )

 

You need to take the ID returned by addMarker and store your data in a table using the ID as the key.

This works:
local getid=map:addMarker(markerdata[5], markerdata[6], options )

getid is the markerId, the problem is i am updating the map every 5 seconds, i delete the marker with removeAllMarker
but this, it doesnt reseet the id of markers, they continue increasing, so if i store datas in a table, every time they will be in a higher position!

It looks like the docs changed with the daily builds from the last public build.  1202’s docs do not talk about the return value, where as the daily docs do.  So I don’t know if we added the ability to get the marker ID later or if the docs just got update.  Still you want to be running one of the later daily builds to get all of the new map features that were added.  See:

http://docs.coronalabs.com/daily/api/type/Map/addMarker.html

myMarkerID = myMap:addMaker(…)

Where myMarkerID is an integer value.

local getid=map:addMarker(markerdata[5], markerdata[6], options )
                        
                        print(getid)
                        myMarkers[getid]=markerdata[3]

I dont know why this doesn’t work…it is frustrating, all day for nothing…

/game.lua:210: table index is nil

 

Can you provide a little more context about what you’re trying to do?  It kinda sounds like it might be beyond the scope of what our maps library is for.

Ok, i got it. This is how i do it. What i wanted is just to store some values in the marker and then recover them with the listener function, but it is not possible to store values in the marker so i create a table to store the needed values. I save the id of the marker and then i check the table in the listener function to know where are the values of the current marker.

myMarkers={}

I have a loop to create several markers:
 

local options = { title = markerdata[3], subtitle = markerdata[4], listener = markerListener, imageFile = "player\_"..markerdata[1].."\_"..markerdata[2]..".png", } local getid=map:addMarker(markerdata[5], markerdata[6], options ) --Store whatever you need here... myMarkers[#myMarkers + 1]={id=getid,name=markerdata[3],uid=markerdata[4]}

Then in the listener function i look the value of event.markerId to match with the value of stored id.

 

local function markerListener(event) for i=1,#myMarkers do if (myMarkers[i].id== event.markerId) then native.showAlert( myMarkers[i].uid,myMarkers[i].name, { "ok" } ) end end return true end

So now it is easy to access stored values of that marker…

I update my map every certains seconds so dont forget to clean de table and remove all markers:

 

map:removeAllMarkers() --clean table with markers for i=1,#myMarkers do table.remove(myMarkers,i) end myMarkers={} 

                      

Hello, I implemented the same thing in my app to deal markersId changing on map refresh.

However, despite setting a ‘return true’ in the markerListener function I am getting a popup on the map and the native.showAlert box appearing.

I was expecting not to have the popup on the map but only the showAlert box.

I am Building with Version 2013.1244 (2013.10.25)

It happens the same to me, what i do is showing little info in the popup but extended in the alert window, but to me is ok, i also noticed that title and subtitle don’t allow return line if i am not wrong \n

Perhaps a parameter could be added to options

showPopup=true/false

if you dont specify is true. Or i would have to remake all my code :slight_smile:

That sounds like a good idea.  I’ll pass it on to the engineer who’s been working on it.  That way you can use the listener to implement your own popup.

Thank you Rob. Can you keep us informed when it is available by posting to this thread.

Do you get our popups if you leave the title and subtitle blank?

Leaving the title and subtitle empty solved the issue.

Thank you Rob!

You need to take the ID returned by addMarker and store your data in a table using the ID as the key.

This works:
local getid=map:addMarker(markerdata[5], markerdata[6], options )

getid is the markerId, the problem is i am updating the map every 5 seconds, i delete the marker with removeAllMarker
but this, it doesnt reseet the id of markers, they continue increasing, so if i store datas in a table, every time they will be in a higher position!

It looks like the docs changed with the daily builds from the last public build.  1202’s docs do not talk about the return value, where as the daily docs do.  So I don’t know if we added the ability to get the marker ID later or if the docs just got update.  Still you want to be running one of the later daily builds to get all of the new map features that were added.  See:

http://docs.coronalabs.com/daily/api/type/Map/addMarker.html

myMarkerID = myMap:addMaker(…)

Where myMarkerID is an integer value.

local getid=map:addMarker(markerdata[5], markerdata[6], options )
                        
                        print(getid)
                        myMarkers[getid]=markerdata[3]

I dont know why this doesn’t work…it is frustrating, all day for nothing…

/game.lua:210: table index is nil

 

Can you provide a little more context about what you’re trying to do?  It kinda sounds like it might be beyond the scope of what our maps library is for.

Ok, i got it. This is how i do it. What i wanted is just to store some values in the marker and then recover them with the listener function, but it is not possible to store values in the marker so i create a table to store the needed values. I save the id of the marker and then i check the table in the listener function to know where are the values of the current marker.

myMarkers={}

I have a loop to create several markers:
 

local options = { title = markerdata[3], subtitle = markerdata[4], listener = markerListener, imageFile = "player\_"..markerdata[1].."\_"..markerdata[2]..".png", } local getid=map:addMarker(markerdata[5], markerdata[6], options ) --Store whatever you need here... myMarkers[#myMarkers + 1]={id=getid,name=markerdata[3],uid=markerdata[4]}

Then in the listener function i look the value of event.markerId to match with the value of stored id.

 

local function markerListener(event) for i=1,#myMarkers do if (myMarkers[i].id== event.markerId) then native.showAlert( myMarkers[i].uid,myMarkers[i].name, { "ok" } ) end end return true end

So now it is easy to access stored values of that marker…

I update my map every certains seconds so dont forget to clean de table and remove all markers:

 

map:removeAllMarkers() --clean table with markers for i=1,#myMarkers do table.remove(myMarkers,i) end myMarkers={} 

                      

Hello, I implemented the same thing in my app to deal markersId changing on map refresh.

However, despite setting a ‘return true’ in the markerListener function I am getting a popup on the map and the native.showAlert box appearing.

I was expecting not to have the popup on the map but only the showAlert box.

I am Building with Version 2013.1244 (2013.10.25)