Removing map marker

When I click on a maker I then click the default button to remove it. This seems to work but only evenryother one will delete I am not sure where I am going wrong. The button is at the end of the code. Any help would be appreciated.

-- Create a native map view local myMap = native.newMapView( 20, 20, 280, 360 ) myMap.x = display.contentCenterX local function markerListener(event) print( "type: ", event.type ) -- event type print( "markerId: ", event.markerId ) -- ID of the marker that was touched print( "lat: ", event.latitude ) -- latitude of the marker print( "long: ", event.longitude ) -- longitude of the marker ID = event.markerId lat = event.latitude lon = event.longitude local myText = display.newText( ID, 200, 300, native.systemFont, 16 ) myText:setFillColor( 1, 0, 0 ) end local options = { title = "Displayed Title", subtitle = "Subtitle text", listener = markerListener, } local attempts = 0 local locationText = display.newText( "Location: ", 0, 400, native.systemFont, 16 ) locationText.anchorY = 0 locationText.x = display.contentCenterX local function locationHandler( event ) local currentLocation = myMap:getUserLocation() if ( currentLocation.errorCode or ( currentLocation.latitude == 0 and currentLocation.longitude == 0 ) ) then locationText.text = currentLocation.errorMessage attempts = attempts + 1 if ( attempts \> 10 ) then native.showAlert( "No GPS Signal", "Can't sync with GPS.", { "Okay" } ) else timer.performWithDelay( 1000, locationHandler ) end else locationText.text = "Current location: " .. currentLocation.latitude .. "," .. currentLocation.longitude myMap:setCenter( currentLocation.latitude, currentLocation.longitude ) myMap:addMarker( currentLocation.latitude, currentLocation.longitude ) end end locationHandler() local function mapMarkerListener( event ) markerID = myMap:addMarker(event.latitude, event.longitude,options) return true end myMap:addEventListener( "mapLocation", mapMarkerListener ) local widget = require( "widget" ) -- Function to handle button events local function handleButtonEvent( event ) if ( "ended" == event.phase ) then myMap:removeMarker(ID) end end -- Create the widget local button1 = widget.newButton( { left = 50, top = 200, id = "button1", label = "Default", onEvent = handleButtonEvent } )

Where does the value for ID come from in line 70?

It looks to me like ID is a global that doesn’t get set until you’ve touched a marker. How does the button know which marker to remove?

Rob

It works fine if you click in the order from the most recent to the first marker you put on but if you go out of order it will only delete the first one you selected out of order and then wont delete the most recent.

You select the marker than press the button to remove it to answer you question.

Thanks for the help by the way

After doing more research I select the marker and when I press the button I print out the ID number of the marker selected and then remove it for some reason I have to go in order from most recent or oldest to newest. If i deviate from that the middle marker gets stuck on the map even though it still is being printed on the screen showing that ID has the correct value to be removed. Is this a bug?

You probably should have a table of marker ID’s. You’re using global variables and as you do things you end up overwriting things you previously did.

Rob

Where does the value for ID come from in line 70?

It looks to me like ID is a global that doesn’t get set until you’ve touched a marker. How does the button know which marker to remove?

Rob

It works fine if you click in the order from the most recent to the first marker you put on but if you go out of order it will only delete the first one you selected out of order and then wont delete the most recent.

You select the marker than press the button to remove it to answer you question.

Thanks for the help by the way

After doing more research I select the marker and when I press the button I print out the ID number of the marker selected and then remove it for some reason I have to go in order from most recent or oldest to newest. If i deviate from that the middle marker gets stuck on the map even though it still is being printed on the screen showing that ID has the correct value to be removed. Is this a bug?

You probably should have a table of marker ID’s. You’re using global variables and as you do things you end up overwriting things you previously did.

Rob