因此,我正在try 整合谷歌 map ,我正在关注一篇关于next13类型脚本的说明的博客文章.我一直遵循到了应该显示 map 但什么都不显示的地步.控制台上也没有错误.请帮我解决这个问题

这是nextjs根页面

'use client';

import { useLoadScript } from '@react-google-maps/api';
import React from 'react';
import Ground from '@/app/components/Maps/Ground';

export default function Page() {
  const libraries = React.useMemo(() => ['places', 'routes', 'geocoding'], []);

  const { isLoaded } = useLoadScript({
    googleMapsApiKey: process.env.NEXT_GOOGLE_MAPS_INTEGRATION_API_KEY as string,
    libraries: libraries as any,
  });
  if (!isLoaded) {
    return <div>Loading...</div>;
  }

  return <Ground />;
}

这是渲染 map 组件的主页

import React from 'react';
import {
  GoogleMap,
  Marker,
  DirectionsRenderer,
  Circle,
  MarkerClusterer,
} from '@react-google-maps/api';
import { useMantineColorScheme } from '@mantine/core';
import { useLocation } from '@/app/dashboard-home/useLocation';
import { ICoordinates } from '@/interfaces/map';

type LatLngLiteral = google.maps.LatLngLiteral;
type DirectionsResult = google.maps.DirectionsResult;
type MapOptions = google.maps.MapOptions;

export default function Ground() {
  const [baseLocation, setBaseLocation] = React.useState<ICoordinates>();

  const { getUserLocation, errorMsg, loading } = useLocation();
  const { colorScheme } = useMantineColorScheme();

  React.useEffect(() => {
    async function fetchLocationInformation() {
      try {
        const userLocation = await getUserLocation();
        if (userLocation) {
          setBaseLocation(userLocation);
        }
      } catch (error) {
        console.log(errorMsg);
      }
    }
    fetchLocationInformation();
  }, []);

  const mapRef = React.useRef<GoogleMap>();
  const center = React.useMemo<LatLngLiteral | undefined>(
    () => (baseLocation ? { lat: baseLocation.latitude, lng: baseLocation.longitude } : undefined),
    [baseLocation]
  );

  const options = React.useMemo<MapOptions>(
    () => ({
      clickableIcons: true,
      scrollwheel: true,
      mapId: colorScheme === 'dark' ? 'faaa827d36c66866' : '8660e06b8e3da749',
    }),
    [colorScheme]
  );
  const onLoad = React.useCallback((map: any) => {
    mapRef.current = map;
  }, []);

  if (errorMsg) {
    return <div>{errorMsg}</div>;
  }
  if (loading) {
    return <div>Loading...</div>;
  }

  return (
    <>
      <GoogleMap
        zoom={8}
        center={center}
        options={options}
        onLoad={onLoad}
        mapContainerStyle={{
          width: '100%',
          height: '100%',
          position: 'relative',
          overflow: 'hidden',
        }}
      >
      </GoogleMap>
    </>
  );
}

I have made some changes to my package.json and added resolutions field and the error that says 'GoogleMap' cannot be used as a JSX component is gone enter image description here I wish I can ping-point the area I need this help. If you require more code tell me. I don't deem useLocation() hook to be important, so what happens is it get the device location coordinates. Thanks in advance

推荐答案

样式未正确定义

<GoogleMap
      zoom={14}
      center={center}
      options={options}
      onLoad={onLoad}
      mapContainerStyle={{
        width: '100%',
        height: '100vh',
        position: 'relative',
        overflow: 'hidden',
      }}
    />

Typescript相关问答推荐

等待的R没有用响应对象展开到R

将props传递给子组件并有条件地呈现它''

为什么从数组中删除一个值会删除打字错误?

Angular 行为:属性类型从SignalFunction更改为布尔

为什么我的Set-Cookie在我执行下一次请求后被擦除

使某些(嵌套)属性成为可选属性

泛型类型联合将参数转换为Never

如何在Angular 12中创建DisplayBlock组件?

如何判断对象是否有重叠的叶子并产生有用的错误消息

等待用户在Angular 函数中输入

使用Redux Saga操作通道对操作进行排序不起作用

防止重复使用 Select 器重新渲染

通过泛型函数本身推断受约束的泛型参数的类型脚本问题

在Cypress中,您能找到表格中具有特定文本的第一行吗?

为什么在泛型方法中解析此promise 时会出现类型错误?

作为函数参数的联合类型的键

设置不允许相同类型的多个参数的线性规则

作为参数的KeyOf变量的类型

有没有一种更好的方法来存储内存中的数据,而不是在类型脚本中使用数组和基于索引的函数?

如何从抽象类的静态方法创建子类的实例?