React Google Maps在使用StandaloneSearchBox元件時遇到了「載入」狀態的問題
P粉138711794
P粉138711794 2023-08-29 09:07:27
0
1
681
<p>我在我的React JS頁面上有一個頁面,我正在嘗試允許公司註冊我的網站並添加他們的位置,以便其他公司前來租賃、購買等。如果可以的話,我還需要添加標記。我已經在我的頁面上放置了Google地圖,但是,我無法將搜尋列(<code>StandaloneSearchBox</code>)放在頁面上,因為它一直處於「載入」狀態。這是我的程式碼:</p> <pre class="brush:php;toolbar:false;">import {useState, useEffect } from 'react' import { GoogleMap, StandaloneSearchBox, LoadScript } from '@react-google-maps/api'; const initialState = { company: '', email: '', password: '', isMember: true, } const Register = () => { const mapContainerStyle = { height: "400px", width: "800px" } const center = { lat: -3.745, lng: -38.523 }; const [values, setValues] = useState(initialState) // global state and useNavigate const handleChange = (e) => { console.log(e.target) } const onSubmit = (e) => { e.preventDefault() console.log(e.target) } const handleLoad = ref => this.searchBox = ref; const handlePlacesChanged = () => console.log(this.searchBox.getPlaces()); return ( <body class="page-template-default page page-id-13"> <header class="site-header"> <div class="container"> <h1 class="school-logo-text float-left"><a href="/landing"><strong>Direct</strong> Connection</a></gt;Direct</strong> Connection</a></ <div class="site-header__util"> </div> </div> </header> <div class="page-banner"> <div class="page-banner__bg-image" style={ { backgroundImage: "url('connection.jpg')" } }></div> <div class="page-banner__content container container--narrow"> <h1 class="page-banner__title">Register</h1> <div class="page-banner__intro"> <p></p> </div> </div> </div>

想要加入並與公司建立聯繫?註冊並推廣您的公司:</h2> <輸入id=“圖像”;類型=“文件”姓名=“個人資料照片” placeholder="照片";必需=“”捕獲></輸入> <標籤類別=「公司名稱」>公司名稱 <div class="company-input"> <標籤類別=「公司電子郵件」>電子郵件 ; <footer class="site-footer"> <div class="site-footer__inner container container--narrow"> <div class="group"> <div class="site-footer__col-one"> <h1 class="school-logo-text school-logo-text--alt-color"> <a href="/landing"><strong>Direct</strong> Connection</a> </h1> <p><a class="site-footer__link" href="index.html">706-263-0175</a></p> </div> </div> </div> </footer> </body> ) } export default Register</pre> <p>我嘗試添加更多的導入,例如從../api/docs資料夾中的<code>ScriptLoaded</code>文件,但是這會導致整個頁面變空白。如果我從<code>import {} from '@react-google-maps/api'</code>和<code>LoadScript GoogleMap</code>程式碼中刪除<code>StandaloneSearchBox</code>,程式碼中刪除<code>StandaloneSearchBox</code>,它就可以在頁面上正常顯示,只是沒有搜尋列和標記來搜尋地址(<code>StandaloneSearchBox</code>)</p>

P粉138711794
P粉138711794

全部回覆(1)
P粉810050669

我認為問題在於沒有函式庫。為了顯示搜尋框,必須有一個庫。 const lib = ['places'];將places設定為一個函式庫,該函式庫必須加入到LoadScript程式碼中,其中包含您的API金鑰。更新後的程式碼應該已經解決了這個問題:

import {useState, useEffect } from 'react'
import { GoogleMap, LoadScript, StandaloneSearchBox } from '@react-google-maps/api'
const lib = ['places'];
const mapContainerStyle = {
  height: "400px",
  width: "800px"
}

const center = {
  lat: -3.745,
  lng: -38.523
};
  
const position = {
  lat: 37.772,
  lng: -122.214
}


const initialState = {
  company: '',
  email: '',
  password: '',
  isMember: true,
}

  const Register = () => {

    
    const [searchBox, setSearchBox] = useState(null);

  const handlePlacesChanged = () => console.log(searchBox.getPlaces());
  const handleLoad = ref => {
    setSearchBox(ref);
  };

    

  const [values, setValues] = useState(initialState)

  // global state and useNavigate
  const handleChange = (e) => {
    console.log(e.target)
  }

  const onSubmit = (e) => {
    e.preventDefault()
    console.log(e.target)
  }

    
      return (
        <body class="page-template-default page page-id-13">
        <header class="site-header">
              <div class="container">
              <h1 class="school-logo-text float-left"><a href="/landing"><strong>Direct</strong> Connection</a></h1>
                         <div class="site-header__util">
                                        </div>
                </div>
                </header>

    <div class="page-banner">
     <div class="page-banner__bg-image" style={ { backgroundImage: "url('connection.jpg')" } }></div>
      <div class="page-banner__content container container--narrow">
        <h1 class="page-banner__title">Register</h1>
        <div class="page-banner__intro">
            <p></p>
        </div>
      </div>
    </div>

<div class="container container--narrow page-section">
  <h2 class="headline headline--tiny">Want to join and connect with companies? Sign up and get your company out there:</h2>
  
    <label class="header">Profile Photo:</label>
      <input id="image" type="file" name="profile_photo" placeholder="Photo" required="" capture></input>
    <label class="company-name">Company Name</label>
      <div class="company-input">
        <input text="headline headline--input" placeholder="Enter Company"></input>
      </div>
    <label class="company-email">Email</label>
          <div class="email-input">
          <input text="headline headline--input" placeholder="Enter Email"></input>
          </div>

     <label class="company-map">Map</label>   

    
  
    <LoadScript
    googleMapsApiKey='API_HERE'
    libraries={lib}
    >
        <GoogleMap
          mapContainerStyle={mapContainerStyle}
          center={center}
          zoom={10}
        >
          <>
           <StandaloneSearchBox
          onLoad={handleLoad}
          onPlacesChanged={handlePlacesChanged}
        >
          
          <input
            type="text"
            placeholder="Search"
            style={{
              boxSizing: `border-box`,
              border: `1px solid transparent`,
              width: `240px`,
              height: `32px`,
              padding: `0 12px`,
              borderRadius: `3px`,
              boxShadow: `0 2px 6px rgba(0, 0, 0, 0.3)`,
              fontSize: `14px`,
              outline: `none`,
              textOverflow: `ellipses`,
              position: "absolute",
              left: "50%",
              marginLeft: "-120px"
            }}
          />
        </StandaloneSearchBox>
        </>
        </GoogleMap>
</LoadScript>
        </div>  
     

    <footer class="site-footer">
                  <div class="site-footer__inner container container--narrow">
                    <div class="group">
                      <div class="site-footer__col-one">
                        <h1 class="school-logo-text school-logo-text--alt-color">
                          <a href="/landing"><strong>Direct</strong> Connection</a>
                        </h1>
                        <p><a class="site-footer__link" href="index.html">706-263-0175</a></p>
                      </div>
                    </div>
                  </div>
            </footer>

</body>
    )
}
  

export default Register
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板