Eun*_*orn 9 javascript google-maps google-maps-markers reactjs redux
我试图在React Component中使用Google Map,但它似乎不起作用.我目前指的是https://developers.google.com/maps/documentation/javascript/adding-a-google-map
这是我的组件代码:
class ContactBody extends React.Component {
componentWillMount() {
const script = document.createElement("script");
const API = 'AIzaSyDbAz1XXxDoKSU2nZXec89rcHPxgkvVoiw';
script.src = `https://maps.googleapis.com/maps/api/js?key=${API}&callback=initMap`;
script.async = true;
document.body.appendChild(script);
};
initMap() {
const uluru = {lat: -25.363, lng: 131.044};
const map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
const marker = new google.maps.Marker({
position: uluru,
map: map
});
}
render() {
this.initMap();
return (
<div>
<h1>Contact</h1>
<div id="map" style={{width: 400, height: 300}}></div>
</div>
)
}
}
ReactDOM.render(
<ContactBody />,
document.getElementById('react')
);Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="react"></div>Run Code Online (Sandbox Code Playgroud)
但是,当我运行它时,我得到"未捕获的ReferenceError:谷歌未定义"
谁能告诉我我的代码有什么问题?
谢谢.
tre*_*mby 20
您正在<script>为文档添加标记以加载Google Maps API,但在运行initMap方法之前,您并没有等待它实际加载.由于尚未加载,因此该google变量尚不存在.
您已callback使用值向脚本的URL添加了参数initMap.Google Maps API会看到这一点,并在initMap准备就绪后运行一个函数.但是您的 initMap方法不能从全局范围中获得,因此不会运行.
修复代码的一种方法是让自己成为Google Maps API的承诺,并在Google Maps API可以运行的(全局)回调函数中解决该承诺.在组件代码中,您将等待承诺得到解决,然后再继续.
这可能看起来像这样:
class ContactBody extends React.Component {
getGoogleMaps() {
// If we haven't already defined the promise, define it
if (!this.googleMapsPromise) {
this.googleMapsPromise = new Promise((resolve) => {
// Add a global handler for when the API finishes loading
window.resolveGoogleMapsPromise = () => {
// Resolve the promise
resolve(google);
// Tidy up
delete window.resolveGoogleMapsPromise;
};
// Load the Google Maps API
const script = document.createElement("script");
const API = 'AIzaSyDbAz1XXxDoKSU2nZXec89rcHPxgkvVoiw';
script.src = `https://maps.googleapis.com/maps/api/js?key=${API}&callback=resolveGoogleMapsPromise`;
script.async = true;
document.body.appendChild(script);
});
}
// Return a promise for the Google Maps API
return this.googleMapsPromise;
}
componentWillMount() {
// Start Google Maps API loading since we know we'll soon need it
this.getGoogleMaps();
}
componentDidMount() {
// Once the Google Maps API has finished loading, initialize the map
this.getGoogleMaps().then((google) => {
const uluru = {lat: -25.363, lng: 131.044};
const map = new google.maps.Map(document.getElementById('map'), {
zoom: 4,
center: uluru
});
const marker = new google.maps.Marker({
position: uluru,
map: map
});
});
}
render() {
return (
<div>
<h1>Contact</h1>
<div id="map" style={{width: 400, height: 300}}></div>
</div>
)
}
}
ReactDOM.render(
<ContactBody />,
document.getElementById('react')
);Run Code Online (Sandbox Code Playgroud)
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<div id="react"></div>Run Code Online (Sandbox Code Playgroud)
这是我的方法:
import React from 'react';
export default class GoogleMap extends React.Component {
constructor(props) {
super(props);
this.state = {
mapIsReady: false,
};
}
componentDidMount() {
const ApiKey = 'XXXXXXXXXXXXXXXXXXXX';
const script = document.createElement('script');
script.src = `https://maps.googleapis.com/maps/api/js?key=${ApiKey}`;
script.async = true;
script.defer = true;
script.addEventListener('load', () => {
this.setState({ mapIsReady: true });
});
document.body.appendChild(script);
}
componentDidUpdate() {
if (this.state.mapIsReady) {
// Display the map
this.map = new window.google.maps.Map(document.getElementById('map'), {
center: {lat: -34.397, lng: 150.644},
zoom: 12,
mapTypeId: 'roadmap',
});
// You also can add markers on the map below
}
}
render() {
return (
<div id="map" />
);
}
}
Run Code Online (Sandbox Code Playgroud)
对于那些正在出错的人google并没有在eslint提到的错误之上定义继续定义
const google = window.google;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7169 次 |
| 最近记录: |