I have modify with:
---------------------------------
Public array_marker(100000) As MapMarker ' codice <= 2000
....
Do While MyRs.NextRow
Pin.Initialize2 ("Id_row", ico_id, 75,0,5, lat, lon)
Pin.SetCursor (fx.Cursors.HAND)
If FLAG_VIEW Then
array_marker(codice)=Pin
OSM.AddMarker (Pin)
else
OSM.RemoveMarker (array_marker(codice))
End If
Loop
---------------------------------
Array of MapMarker,
But also with FLAG_VIEW = false the marker remain visible
1- MapMarker array definition
2- marker assignment to the 'code' position in the array, during AddMarker instruction
3- read marker from array on line 'code' during RemoveMarker instruction
It still doesn't work
Is it a mistake or is there an error?
I think the problem is that the variable "codice" would need to be incremented when each Pin is created and added to your array. I can't tell what "codice" is or how it changes, from your example. I suspect that the same MapMarker (Pin) is being removed every time, and not all the other ones in the array.
Try this example.
B4X:
Public ListOfPins as list
....
ListOfPins.Initialize
....
Do While MyRs.NextRow
'UID must be unique for this PIN only. Every Pin must have its own UID value.
Dim UID as string = ico_id 'I think "ico_id" is your unique identifier for this PIN
'If you don't already, you may need to create one with every PIN and storing it on your database under its own column.
'You possibly could use the RowNumber if you can read it, and the row numbers don't change for an existing PIN.
If FLAG_VIEW Then
Pin.Initialize2 ("Id_row", ico_id, 75,0,5, lat, lon)
AddPinToMap(Pin,UID)
Else
DeletePinFromMap(UID)
End If
Loop
....
End Sub
Private sub AddPinToMap(ThisPin as MapMarker,Unique_ID as String)
ThisPin.Tag = Unique_ID 'so you can find it later
ThisPin.SetCursor(fx.Cursors.HAND)
ListOfPins.Add(ThisPin)
OSM.AddMarker(ThisPin)
End Sub
Private sub DeletePinFromMap(Unique_ID as String)
Dim I as int
if ListOfPins.Size > 0 Then
For I = 0 to ListOfPins.Size-1
Dim ThisPin as MapMarker = ListOfPins.Get(I)
if ThisPin.Tag = Unique_ID then
OSM.RemoveMarker(ThisPin)
ListOfPins.RemoveAt(I)
Return 'Pin was found and removed
End If
Next
End If
End Sub
Private Sub DeleteAllPinsFromMap
For Each P as MapMarker in ListOfPins
OSM.RemoveMarker(P)
Next
ListOfPins.Clear
End Sub
I hope you can see the principle of creating a unique identifier for each Pin so it can be later removed from the map.
Good Luck
PS.
You could have a read about Objects (classes, creation/initialization/instantiation, pointers, etc).
In short, when a class in instantiated (lets say Initialized) a new unique instance of that class is Created in memory. This is an "Object". It can contain a collection of many things such as Code, Data, Images and even other objects, to form this new Object in memory.
The declaration of the variable (Dim Pin as MapMarker), once Initialized, is only a memory pointer to that newly created object (of MapMarker).
If you have another variable, say (Dim Abc as MapMarker) and we code (Abc = Pin) then both (Pin) and (Abc) reference the SAME object. They are just aliases for each other.
ie. the (Pin) memory pointer value is copied to (Abc)'s memory pointer.
In the example above (Sub AddPinToMap), adding the (Pin) to our "ListOfPins" is only storing the memory pointer to that (Pin) object into the list. The object itself is NOT copied. The "ListOfPins" contains a list of pointers to many different (Pin) Objects in memory.
When you pass an Object as a parameter to a function like; AddPinToMap(Pin,UID) the memory pointer of (Pin) is passed into the function so, that exact Pin can be accessed in memory, while the (UID) parameter (being a String) is copied, its current value is passed to the function, not (UID) itself.
I here you say, "I am Splitting Hairs" but the affect is that altering (ThisPin) inside the function makes changes to (Pin) in memory, while if you alter (Unique_ID) inside the function, then (UID) outside the function remains unaltered.
Other than standard variable types (Byte, Int, Long, Double, Float, String), and as a rule, if you need to (Initialize) it, then it is an Object and the variable is actually a pointer to it in memory.
If you declare your own TYPES and declare variables of these types, then they are also considered and treated as Objects, even if you choose not to call their Initialize() function.
If you get a compiler or run-time error of "Null Reference Pointer" this means that the variable does not point to an object in memory. It probably has not been Initialized yet. Its pointer holds the value of "Null".
Removing a (Pin) Object from the list and map does not delete it from memory yet, even though you are no longer showing it, or using it. It's only when ALL variables that were pointing to (Referenced) the object are them selves overwritten or no longer required (eg inside a sub-function) that the Object in memory becomes "De-referenced". The memory manager (garbage Collector) in the language environment will come along later and remove it from memory "Destroy It", as and when required. You don't need to worry about it.
Happy coding....