Get all tiles in shapecast2D

Godot Version

4.1.1.stable

Question

Hello, its my first time posting here! I am having a hard time understanding tilemaps and now, I ran into a problem that I just dont understand how to solve.
I want to detect all the tiles that are in a shapecast2D, store them in an array and then get the distance between the tiles and the mouse cursor for each tile in the array. With that information I want to do something with the tile that is the closest to the mouse cursor.
I already implemented this feature for nodes instead of tiles and it works.
The problems with the tilemap are, that I just get the tilemap node as a value for the array and that I dont know how to get acces to the tiles itself that are in the shapecast2D. I already got some answers regarding this problem, but I still dont know what to do with them, so it would really help me if you could explain in detail how all of the possible solutions work.
Thank you to everyone who is willing to help me, if you need any further information, just ask!

Here is the function that should perform this logic:

func get_nodes_from_shapecast() -> Array:
  shapecast.force_shapecast_update()   #Update the shapecas2D collisions

  number_of_nodes = shapecast.get_collision_count()   #Store the collisions
  nearest_distance = Vector2(100000, 100000)   #Reset the nearest distance
  var nodes_in_shapecast = []

  for i in range(number_of_nodes):   #Loop through all collided objects
    var node = shapecast.get_collider(i)   #Get the objects one at a time
    if node.is_in_group("static_tree") or node.is_class("TileMap"):
      var distance_between

      if node.is_in_group("static_tree"):
        distance_between = mouse_pos - (node.global_position + Vector2(0, 16))
      elif node.is_class("TileMap"):
        pass   #What should I do here?? I cant find a solution

      if distance_between.x < 0:   #Calculate if object is the nearest to the mouse
        distance_between.x *= -1
      if distance_between.y < 0:
        distance_between.y *= -1
      if distance_between < nearest_distance:
        nearest_distance = distance_between
        nearest_node = i
      highlight_box_visibility = true

    nodes_in_shapecast.append(node)

As I said, I already got some answers regarding the problem and one of them was that the closest of all tiles, which get checked in the for loop is:

node.local_to_map(node.get_local_mouse_position()) #"node" is the tilemap node

if I print this out in my game, I just get a different value for each tile my mouse is in, no matter where it is (sometimes it should logically stay the same). If this should return the position of the closest tile to the mouse, why is it always the value the same for all tiles and why does it change everytime I move my mouse to a new tile?
I also looked at the tilemap docs, but I just dont understand why it doesnt work.