cant remove newMapView

Hi I am struggeling since days nows removing an newMapview, i read older forum comments but non of them apply to me and i dont think that i do things totally wrong but lets see, hope somebody can give me a hint

my  usecase looks like  that, i got one scene called WorkOrder and another one called mapview. Mapview got just the map and a back button to reach the WorkOrder again.

To do so i setup my myMap variable outside of any function and open a newMapView

-> so far so good see the map and marker.

[lua]

local composer = require( “composer” )

local widget = require( “widget” )

local sqlite3 = require( “sqlite3” )

local json = require(“json”)

–local utility = require( “utility” )

local scene = composer.newScene() 

local path = system.pathForFile( “data.db”, system.DocumentsDirectory )

local db = sqlite3.open( path )

local myMap

–…init other variables and methods,
function scene:show( event )

   print(“change to show”)

   local sceneGroup = self.view

   local phase = event.phase

   if ( phase == “will” ) then

      myMap = native.newMapView( display.screenOriginX+155, display.screenOriginY+121+205, 300, 400 )

      myMap:setCenter( 46.625, 14.305, 0.01, 0.01 )

      – Called when the scene is still off screen (but is about to come on screen).

   elseif ( phase == “did” ) then

      myMap:addMarker( 46.625, 14.305 )

   end

end

[/lua]

and then when i call the back button i want to todo a removeself()

[lua]

function removeMap ()

   myMap: removeSelf (); 

   myMap = nil

end

local function goBack()

   pcall(function () removeMap() end)

   composer.gotoScene( “workorder”, { effect = “crossFade”, time = 200 } )

end

[/lua]

thanks to the pcall i dont see the exception but the map is still there

the exception which is actually caught is:

attempt to index upvalue ‘myMap’ a nil value

the exception occurs in the line when i do the removeSelf

and that sounds a bit like a scope problem or not proper initialized thing

But that doesnt make sense for me because the scope is good for the show method, so marker is set but whats the difference in the removeMap function if the variable is declared above both methods

Hence i tried to call the removeSelf from the hide() and destroy() method but seems thats not doing much 

to my knowledge i cant debug that somehow because ist a native object.

However, question remains, how can i remove the native.newMapView when leaving the scene

thx

a lot

Hi @kurtklaus.horvath,

Typically, you should remove things in Composer’s “hide” function, which is of course the opposite of “show”. For a map view, I would suggest doing it in the “hide > did” state:

[lua]

function scene:hide( event )

   local sceneGroup = self.view

   local phase = event.phase

   if ( phase == “did” ) then

      myMap:removeSelf()

   end

end

[/lua]

Hope this helps,

Brent

Hi @kurtklaus.horvath,

Typically, you should remove things in Composer’s “hide” function, which is of course the opposite of “show”. For a map view, I would suggest doing it in the “hide > did” state:

[lua]

function scene:hide( event )

   local sceneGroup = self.view

   local phase = event.phase

   if ( phase == “did” ) then

      myMap:removeSelf()

   end

end

[/lua]

Hope this helps,

Brent