【问题标题】:react-google-maps calls empty InfoWindowreact-google-maps 调用空的 InfoWindow
【发布时间】:2020-09-19 07:10:19
【问题描述】:

我在第一次调用 InfoWindow 时遇到问题,它调用了 2 次,一个没有任何文本/信息,第二个 infoWindow 有更正我的信息(屏幕上的数字 2)。如果我单击另一个我的 Markeк,这个空的 InfoWindow 将保留在第一位。但是,如果我尝试关闭空窗口并打开任何我的标记,则此空窗口将再次显示在新位置。

我的代码:

    function MapCreator() {
  const [selectedPoint, setselectedPoint] = useState(null);
  const [data,setData] = useState([]);

  useEffect(() => {
    axios.get('api/coordinates')
    .then(values=>{
        console.log(values.data);
        setData(values.data);
    })
    const listener = e => {
      if (e.key === "Escape") {
        setselectedPoint(null);
      }
    };
    window.addEventListener("keydown", listener);

    return () => {
      window.removeEventListener("keydown", listener);
    };
  }, []);
  return (
    <GoogleMap
      defaultZoom={14}
      defaultCenter={{ lat: 50.444571, lng: 30.559099 }}
    >
      {data.map(park => (
        <Marker
          key={park._id}
          position={{
            lat: park.coordinates.latitude,
            lng: park.coordinates.longitude
          }}
          onClick={() => {
            setselectedPoint(park);
          }}
          icon={{
            url: nfcIcon,
            scaledSize: new window.google.maps.Size(60, 60)
          }}
        />
      ))}

      {selectedPoint && (
        <InfoWindow
          onCloseClick={() => {
            setselectedPoint(null);
          }}
          position={{
            lat: selectedPoint.coordinates.latitude,
            lng: selectedPoint.coordinates.longitude
          }}
        >
          <div>
                <h2>Device ID: {selectedPoint.identificatorNFC}</h2>
                <p>Date: {new Date(selectedPoint.date) .toUTCString()}</p>
          </div>
        </InfoWindow>
      )}
    </GoogleMap>
  );
}

const MapWrapped = withScriptjs(withGoogleMap(MapCreator));

export default function App() {
  return (
    <div style={{ width: `100%`, height: "100vh" }}>
      <MapWrapped
        googleMapURL={`https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places&key=${
        keys.googleApi
        }`}
        loadingElement={<div style={{ height: `100%` }} />}
        containerElement={<div style={{ height: `100%` }} />}
        mapElement={<div style={{ height: `100%` }} />}
      />
    </div>
  );
}

【问题讨论】:

    标签: reactjs google-maps google-maps-markers


    【解决方案1】:

    Tbh,我在您的代码中没有发现任何问题,但我建议您尝试一下:

      function MapCreator() {
      const [selectedPoint, setselectedPoint] = useState(null);
      const [data,setData] = useState([]);
    
      useEffect(() => {
        axios.get('api/coordinates')
        .then(values=>{
            console.log(values.data);
            setData(values.data);
        })
        const listener = e => {
          if (e.key === "Escape") {
            setselectedPoint(null);
          }
        };
        window.addEventListener("keydown", listener);
    
        return () => {
          window.removeEventListener("keydown", listener);
        };
      }, []);
      return (
        <GoogleMap
          defaultZoom={14}
          defaultCenter={{ lat: 50.444571, lng: 30.559099 }}
        >
          {data.map(park => (
            <Marker
              key={park._id}
              position={{
                lat: park.coordinates.latitude,
                lng: park.coordinates.longitude
              }}
              onClick={() => {
                setselectedPoint(park);
              }}
              icon={{
                url: nfcIcon,
                scaledSize: new window.google.maps.Size(60, 60)
              }}
            >
             {selectedPoint && selectedPoint._id === park._id && (
               <InfoWindow onCloseClick={() => setselectedPoint(null)}
               >
                 <div>
                    <h2>Device ID: {selectedPoint.identificatorNFC}</h2>
                    <p>Date: {new Date(selectedPoint.date) .toUTCString()}</p>
                 </div>
               </InfoWindow>
             )}
            </Marker>
          ))}
    
        </GoogleMap>
      );
    }
    
    const MapWrapped = withScriptjs(withGoogleMap(MapCreator));
    
    export default function App() {
      return (
        <div style={{ width: `100%`, height: "100vh" }}>
          <MapWrapped
            googleMapURL={`https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=geometry,drawing,places&key=${
            keys.googleApi
            }`}
            loadingElement={<div style={{ height: `100%` }} />}
            containerElement={<div style={{ height: `100%` }} />}
            mapElement={<div style={{ height: `100%` }} />}
          />
        </div>
      );
    }
    

    【讨论】:

    • {selectedPoint && selectedPoint._id === park._id && ( 这部分代码我忘了,我的代码没有检查选定点是否等于.map循环中的元素
    • 在您的代码中,InfoWindow 也不在标记内。
    • 谢谢!对我来说关键是 --> 并在 中移动 代码
    猜你喜欢
    • 2019-05-14
    • 1970-01-01
    • 2020-02-17
    • 1970-01-01
    • 2020-09-05
    • 1970-01-01
    • 2018-12-15
    • 2011-02-11
    • 2023-03-16
    相关资源
    最近更新 更多