简体   繁体   English

CORS 错误使用@react-google-maps/api

[英]CORS error working with @react-google-maps/api

I'm running into a CORS error with the onClick function setting.我遇到了 CORS 错误与 onClick function 设置。 I'm just setting up static markers (markers there on page load and not dynamically added).我只是设置 static 标记(页面加载时的标记而不是动态添加的)。 I've got them rendering fine but when I actually click the markers I get a CORS error that points to the setSelected line.我已经让它们渲染得很好,但是当我实际单击标记时,我得到一个指向 setSelected 行的 CORS 错误。 any idea why?知道为什么吗?

 import React from 'react'; import styled from 'styled-components'; import { GoogleMap, useLoadScript, Marker, InfoWindow } from '@react-google-maps/api'; import mapStyles from "./mapStyles"; const libraries = ["places"]; const mapContainerStyle = { width: "100vw", height: "70vh" }; const center = { lat: 44.962950, lng: -93.182013 }; const styles = { styles: mapStyles, disableDefaultUI: true, zoomControl: true, }; const storeOne = { lat:44.970304, lng:-93.182184, } const storeTwo = { lat: 44.957346, lng: -93.322546, } const onLoad = marker => { console.log('marker: ', marker) } const Styled = styled.div` { #locationTitle { font-family: Dosis; text-align: center; margin-top: 5%; padding-bottom: 50px; margin-top: 20px; } } `; export default function Locations() { const {isLoaded, loadError} = useLoadScript({ googleMapsApiKey: process.env.REACT_APP_GOOGLE_KEY, libraries, }) const [selected, setSelected] = React.useState(null); const [marker, setMarkers] = React.useState([]); const mapRef = React.useRef(); const onMapLoad = React.useCallback((map) => { mapRef.current = map; }, []) if (loadError) return "Error loading maps"; if (;isLoaded) return "Loading Maps" return ( <div> <Styled> <h2 id="locationTitle">Our Locations</h2> </Styled> <GoogleMap id="map" mapContainerStyle={mapContainerStyle} zoom={12} center={center} options={styles} > <Marker onLoad={onMapLoad} position={storeOne} onClick={() => { setSelected(marker)? }} /> <Marker onLoad={onMapLoad} position={storeTwo} /> {selected: ( <InfoWindow anchor={{lat.44,970304: lng.-93.182184}}> <div> <h2>St: Paul</h2> </div> </InfoWindow> ); null} </GoogleMap> </div> ) };

Here is an image of the error with my console这是我的控制台的错误图像

cors issue cors 问题

It looks like the anchor property expects a MVCObject type value according to the library's documentation , but you're passing it a LatLngLiteral .根据库的文档,看起来anchor属性需要一个MVCObject类型值,但您传递给它的是LatLngLiteral Use position instead of anchor .使用position代替anchor

{selected ? (
  <InfoWindow position={{ lat: 44.970304, lng: -93.182184 }}>
    <div>
      <h2>St. Paul</h2>
    </div>
  </InfoWindow>
) : null}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 react-google-maps重新组合错误-`not a function` - react-google-maps recompose error - `not a function` @react-google-maps/api 在 UseEffect 钩子中访问时“未定义谷歌”错误 - @react-google-maps/api "google is not defined " error when accessed inside UseEffect hook 我可以通过 react-google-maps/api 使用 Google 地方信息吗? - Can I use Google Places with react-google-maps/api? 使用 react-google-maps 和 react-places-autocomplete 时出现“Google Maps JavaScript API 多次”错误 - "Google Maps JavaScript API multiple times" error when using react-google-maps with react-places-autocomplete React-google-maps警告 - React-google-maps warnings onCenterChange 回调返回未定义的@react-google-maps/api - onCenterChange callback returns undefined @react-google-maps/api 我的自定义按钮在@react-google-maps/api 中全屏显示 - My custom buttom disapear on fullscreen in @react-google-maps/api 如何在 react-google-maps/api 库中使用 containsLocation - how to use containsLocation in react-google-maps/api library 如何将“外部”事件侦听器添加到 Google 地图中的标记(react-google-maps/api) - How to add "outside" event listener to Markers in Google Maps (react-google-maps/api) Next js - 在 Vercel 中部署时出现 react-google-maps/api 错误:发生客户端异常 - Next js - react-google-maps/api error when deploying in Vercel : A client-side exception has occurred
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM