【发布时间】:2021-01-14 14:26:01
【问题描述】:
我有一个使用 google maps api 的 gatsby 应用程序。我正在加载 useRef 和 useEffect 来加载地图。 useRef 返回一个可变 ref 对象,其 .current 属性初始化为传递的参数 (initialValue)。返回的对象将在组件的整个生命周期内持续存在。这是我的代码:
import React, { useRef, useEffect } from "react"
import MapMarker from "../assets/images/logos/map-marker.png"
const Map = () => {
const map = useRef(null)
useEffect(()=>{
if(typeof window !== "undefined"){
window.initMap = function() {
const map = new window.google.maps.Map(map.current, {
center: {
lat: 142.1463554,
lng: 121.5245996
},
zoom: 10,
styles: [{"featureType":"water","elementType":"geometry","stylers":[{"color":"#d3d3d3"},{"lightness":50}]},{"featureType":"landscape","elementType":"geometry","stylers":[{"color":"#ffffff"},{"lightness":20}]},{"featureType":"road.highway","elementType":"geometry.fill","stylers":[{"color":"#ffffff"},{"lightness":17}]},{"featureType":"road.highway","elementType":"geometry.stroke","stylers":[{"color":"#ffffff"},{"lightness":29},{"weight":0.2}]},{"featureType":"road.arterial","elementType":"geometry","stylers":[{"color":"#ffffff"},{"lightness":18}]},{"featureType":"road.local","elementType":"geometry","stylers":[{"color":"#ffffff"},{"lightness":16}]},{"featureType":"poi","elementType":"geometry","stylers":[{"color":"#f5f5f5"},{"lightness":21}]},{"featureType":"poi.park","elementType":"geometry","stylers":[{"color":"#dedede"},{"lightness":21}]},{"elementType":"labels.text.stroke","stylers":[{"visibility":"on"},{"color":"#ffffff"},{"lightness":16}]},{"elementType":"labels.text.fill","stylers":[{"saturation":50},{"color":"#000000"},{"lightness":40}]},{"elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"transit","elementType":"geometry","stylers":[{"color":"#f2f2f2"},{"lightness":19}]},{"featureType":"administrative","elementType":"geometry.fill","stylers":[{"color":"#4c4c4c"},{"lightness":20}]},{"featureType":"administrative","elementType":"geometry.stroke","stylers":[{"color":"#4c4c4c"},{"lightness":17},{"weight":1.2}]}]
})
new window.google.maps.Marker({
position: { lat: 112.1463554, lng: 130.5245993 },
icon: MapMarker,
animation: window.google.maps.Animation.BOUNCE,
map: map
});
}
}
return () => map.current = null
}, [])
return (
<div id="map-container" data-aos="fade-up">
<div className="container-fluid">
<div className="row">
<div className="col-lg-12 col-12 pl-0 pr-0">
<div className="google-map">
<div id="map" ref={map}></div>
</div>
</div>
</div>
</div>
</div>
)
}
export default Map
当我加载我的 gatsby 应用程序时,它返回了:
Unhandled Rejection (TypeError): Cannot read property 'current' of undefined
window.initMap
C:/Users/Read/Desktop/gatsby/src/components/Map.js:12
9 |
10 | if(typeof window !== "undefined"){
11 | window.initMap = function() {
> 12 | const map = new window.google.maps.Map(map.current, {
13 | center: {
14 | lat: 11.1463554,
15 | lng: 110.5245996
知道如何解决这个问题吗?请帮忙!
【问题讨论】:
-
const map = new window.google.maps.Map(map.current- 惊讶于错误与时间死区无关......因为您在定义map之前尝试使用map.current -
什么意思?你可以解释吗?我该如何解决?
-
map 在构造函数完成之前没有定义 - 所以传递
map.current将不起作用,因为......好吧...... map 没有定义......不知道你如何修复它.. . 你想传递给那个构造函数什么?我想你可以在代码的前面尝试const mapX = useRef(null),然后使用mapX.current- 然后尝试理解变量范围 -
错误信息很奇怪……你用的是哪个浏览器?
-
我正在使用 Chrome
标签: javascript reactjs google-maps gatsby