[英]@react-google-maps/api - Loading map is blocking the main thread
I am currently working on a website, where I use google maps to display some markers with places location.我目前正在一个网站上工作,在那里我使用谷歌地图来显示一些带有地点位置的标记。 I am using this npm package: https://www.npmjs.com/package/@react-google-maps/api .我正在使用这个 npm package: https://www.npmjs.com/package/@react-google-maps/api 。
The issue is that when I do site audit with Google Insights ( https://developers.google.com/speed/pagespeed/insights/ ) I am getting very low mobile performnace.问题是,当我使用 Google Insights ( https://developers.google.com/speed/pagespeed/insights/ ) 进行站点审核时,我的移动性能非常低。 It is about 50.大约是50。
This is considered a issue, because I am using Next.js (version 10.0.6).这被认为是一个问题,因为我使用的是 Next.js(版本 10.0.6)。 Audit score on desktop is great.桌面上的审计分数很棒。
I think, that the main issue is with loading a map.我认为,主要问题是加载 map。 This proccess is blocking my website for about 500 ms.这个过程阻止了我的网站大约 500 毫秒。 It is a lot.这是很多。
Screen shot of an issue问题的屏幕截图
Here is how I am getting the map at the moment:以下是我目前获得 map 的方式:
I have already tried usingEffect (with an empty array), async/await syntax, nothing helped.我已经尝试过 usingEffect (使用空数组),异步/等待语法,没有任何帮助。
I will be very grateful for any help.我将非常感谢任何帮助。
Kind regards,亲切的问候,
Bartek巴特克
Although I am not that familiar on how Google Insight works, loading the script directly instead of relying on other 3rd party libraries (@react-google-maps/api) could prove beneficial and should reduce the latency.虽然我对 Google Insight 的工作原理不是很熟悉,但直接加载脚本而不是依赖其他 3rd 方库 (@react-google-maps/api) 可能是有益的,并且应该会减少延迟。
App.js应用程序.js
import React, { Component } from 'react';
import { render } from 'react-dom';
import Map from './components/map';
import "./style.css";
class App extends Component {
render() {
return (
<Map
id="myMap"
options={{
center: { lat: -33.8569, lng: 151.2152 },
zoom: 8
}}
/>
);
}
}
export default App;
map.js map.js
import React, { Component } from "react";
import { render } from "react-dom";
class Map extends Component {
constructor(props) {
super(props);
this.state = {
map: ""
};
}
onScriptLoad() {
this.state.map = new window.google.maps.Map(
document.getElementById(this.props.id),
this.props.options
);
var marker = new window.google.maps.Marker({
position: { lat: -33.8569, lng: 151.2152 },
map: this.state.map,
title: "Hello Sydney!"
});
//adding markers by click
this.state.map.addListener("click", e => {
//Determine the location where the user has clicked.
this.addMarker(e.latLng);
});
}
componentDidMount() {
if (!window.google) {
var s = document.createElement("script");
s.type = "text/javascript";
s.src = `https://maps.google.com/maps/api/js?key=YOUR_API_KEY`;
var x = document.getElementsByTagName("script")[0];
x.parentNode.insertBefore(s, x);
s.addEventListener("load", e => {
this.onScriptLoad();
});
} else {
this.onScriptLoad();
}
}
addMarker(latLng) {
var marker = new google.maps.Marker({
position: latLng,
map: this.state.map
});
}
render() {
return <div className="map" id={this.props.id} />;
}
}
export default Map;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.