0

This is basically a map that has building icons and when you tap on a building, it shows information for that building. I am struggling with logic for toggling individual items(buldings) in my array. Like only displaying building 1 for example. So when I tap on a Marker it should show the correct building in the array. I am showing partial code for mapscreen.js but will share all if needed.

Buildings.js

export const buildings = [
  {
    name: "Building1",
    description: "This is some information",
    xp: 25,
  },
  {
    name: "Building2",
    description: "This is some information.",
    xp: 25,
  },
];

Mapscreen.js

const [visible, setVisible] = useState(false);

//MAP THROUGH ARRAY, PASS ARRAY ITEMS AS PROPS TO OVERLAY COMPONENT 

const myBuilding = buildings.map((building, i) => (
    <OverlayBox key={i} title={building.name} info={building.description} xp={building.xp} />
  ));
  const toggleOverlay = () => {
    setVisible(!visible);
  };

return (
    <View>
      <Overlay isVisible={visible} onBackdropPress={toggleOverlay}>
        {myBuilding}**<--------DISPLAYS BOTH BUILDINGS RIGHT NOW**
      </Overlay>

      <Marker
          onPress={toggleOverlay}**<--------WANT TO DISPLAY BUILDING 1**
          coordinate={{ latitude: 34.0198536, longitude: -80.923467 }}
          title={"Building1"}
          description={"25 XP"}
        />
      <Marker
          onPress={toggleOverlay}**<--------WANT TO DISPLAY BUILDING 2**
          coordinate={{ latitude: 34.0195536, longitude: -80.924467 }}
          title={"Building2"}
          description={"25 XP"}
        />
</View>


2 Answers 2

1

Your approach is a bit weird

I suggest you:

  1. Make only one OverlayBox

  2. Pass to toggleOverlay the index of your building like

    <Marker onPress={() => toggleOverlay(0)}

  3. Set the visible building index in state like

    const toggleOverlay = (index) => { setVisibleBuilding(index); ... };

  4. And pass the building to your OverlayBox like

    <OverlayBox building={buildings[visibleBuildingIndex]} ... />

Sign up to request clarification or add additional context in comments.

4 Comments

I am updating my code, are you saying in step one I don't really need to map through? just render one overlay right, also what is visiblebuildingIndex?
I updated it some but not sure if going in right direction? (posted below previous code)
Managed to fix, I will post my solution if needed.
You're in the right way, no need to make more than one overlaybox but you can map buildings to generate your markers dynamically, just store coordinates, color and wathever in buildings array and finally be sure of setting building index before rendering your OverlayBox
1
//ANSWER
  const [visibleBuilding, setVisibleBuilding] = useState(0);
  const buildings = [
    {
      name: "Building1",
      description:
        " This is some information.",
      xp: 25,
    },
    {
      name: "Building2",
      description: "This is some information.",
      xp: 25,
    },
  ];
  const [visible, setVisible] = useState(false);

  const toggleOverlay = () => {
    setVisible(!visible);
  };

  return (
    <View style={{ flex: 1 }}>
      <Overlay isVisible={visible} onBackdropPress={toggleOverlay}>
        <OverlayBox
          building={buildings[visibleBuilding]}
          onPress={() => toggleOverlay()}
          key={visibleBuilding}
          title={buildings[visibleBuilding].name}
          info={buildings[visibleBuilding].description}
          xp={buildings[visibleBuilding].xp}
        />
      </Overlay>
        <Marker
          onPress={() => {
            toggleOverlay();
            setVisibleBuilding(0);
          }}
          coordinate={{ latitude: 34.0198536, longitude: -80.923467 }}
          pinColor="maroon"
          title={"Rutledge"}
          description={"25 XP"}
        />
        

        <Marker
          onPress={() => {
            toggleOverlay();
            setVisibleBuilding(1);
          }}
          coordinate={{ latitude: 34.0195536, longitude: -80.924467 }}
          pinColor="maroon"
          title={"Swearingen Engineering Center"}
          description={"25 XP"}
        />
       
    </View>
  );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.