在使用 react-google-maps-api 中在街景视图上显示信息框/信息窗口。

huangapple go评论61阅读模式
英文:

Show InfoBox/InfoWindow on the streetView with react-google-maps-api

问题

在这个代码沙箱中,我有一些带有在点击时显示的 InfoBox 嵌套在标记中的标记。

如何在切换到 StreetView 模式时也显示该 InfoBox?

// 导入部分
import { GoogleMap, InfoBox, Marker, OverlayView, Polyline, useJsApiLoader } from '@react-google-maps/api';

{data.map(({ source, destination, distance }, index) => (

<InfoBox
options={{
enableEventPropagation: true,
boxStyle: {...}
}}
>

这里有一些文本



))}

注意:在代码沙箱中没有提供 API 密钥。提供 API 密钥将显示可在地图附近拖放到标记附近的 StreetView 图标。

英文:

In this code sandbox, I have some markers with InfoBox nested in the marker that is shown on click.
How can I also show that InfoBox when switching into the StreetView mode?

  // The imports 
import { GoogleMap, InfoBox, Marker, OverlayView, Polyline, useJsApiLoader } from &#39;@react-google-maps/api&#39;;
  {data.map(({ source, destination, distance }, index) =&gt; (
    &lt;Marker key={index} position={source}&gt;
      &lt;InfoBox
        options={{
          enableEventPropagation: true,
          boxStyle: {...}
        }}
      &gt;
        &lt;div
          style={{...}}
        &gt;
          &lt;p&gt;Some text here&lt;/p&gt;
        &lt;/div&gt;
      &lt;/InfoBox&gt;
    &lt;/Marker&gt;
  ))}

Note: No API key is provided in the code sandbox. Providing the API key will show the StreetView icon that can be dragged-dropped on the map close to a Marker.

答案1

得分: 1

在调用 InfoWindow#open 方法时,将 Map 替换为 StreetViewPanorama

Google地图JavaScript API关于街景中叠加物的文档中提到,可以通过在调用 open() 方法时传递 StreetViewPanorama 而不是 Map,在 StreetViewPanorama 内打开一个 InfoWindow

@react-google-maps/api 代码库中,InfoWindow.tsx 文件通过React的Context API获取了 Map 实例,并使用它来打开 InfoWindow 实例:

const map = useContext<google.maps.Map | null>(MapContext)
// ...
instance.open(map)

因此,我们可以导入 MapContext 并在 &lt;InfoWindow&gt; 包装它,以替换 mapStreetViewPanorama 实例,可以通过 &lt;StreetViewPanorama&gt;onLoad 事件获取。

import { createRoot } from "react-dom/client";
import {
  useLoadScript,
  GoogleMap,
  StreetViewPanorama,
  Marker,
  InfoWindow,
  MapContext,
} from "@react-google-maps/api";
import React, { useContext, useState } from "react";

function App() {
  const astorPlace = { lat: 40.729884, lng: -73.990988 };
  const [panorama, setPanorama] =
    useState<google.maps.StreetViewPanorama | null>(null);
  const markers = [
    {
      position: { lat: 40.730031, lng: -73.991428 },
      icon: "https://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=cafe|FFFF00",
      title: "Cafe",
    },
    {
      position: { lat: 40.729681, lng: -73.991138 },
      icon: "https://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=dollar|FFFF00",
      title: "Bank",
    },
    {
      position: { lat: 40.729559, lng: -73.990741 },
      icon: "https://chart.apis.google.com/chart?chst=d_map_pin_icon&chld=bus|FFFF00",
      title: "Bus Stop",
    },
  ];

  function onPanoramaLoad(panorama: google.maps.StreetViewPanorama) {
    panorama.setPosition(astorPlace);
    panorama.setPov({
      heading: 265,
      pitch: 0,
    });
    panorama.setVisible(true);
    setPanorama(panorama);
  }

  const { isLoaded, loadError } = useLoadScript({
    googleMapsApiKey: import.meta.env.VITE_GOOGLE_MAPS_API_KEY,
  });

  return (
    <>
      {isLoaded ? (
        <GoogleMap center={astorPlace} zoom={18} id="map">
          <StreetViewPanorama onLoad={onPanoramaLoad} />
          {panorama ? (
            // @ts-ignore
            <MapContext.Provider value={panorama}>
              {markers.map((marker) => (
                <Marker key={marker.title} {...marker}>
                  <InfoWindow position={marker.position}>
                    <div
                      style={{
                        backgroundColor: "white",
                        opacity: 0.8,
                      }}
                    >
                      <h2>{marker.title}</h2>
                    </div>
                  </InfoWindow>
                </Marker>
              ))}
            </MapContext.Provider>
          ) : null}
        </GoogleMap>
      ) : null}
      {loadError ? <p>{loadError.message}</p> : null}
    </>
  );
}

createRoot(document.getElementById("app")!).render(<App />);

在CodeSandbox上查看此示例的实时演示。

在使用 react-google-maps-api 中在街景视图上显示信息框/信息窗口。

英文:

Replace the Map in calls to the InfoWindow#open method with a StreetViewPanorama

In Google's documentation for the Maps JavaScript API on overlays within street view, it is noted that an InfoWindow may be opened within a StreetViewPanorama by passing the StreetViewPanorama instead of a Map when calling the open() method.

In the @react-google-maps/api codebase, the InfoWindow.tsx file gets the Map instance through React's Context API and uses it to open the InfoWindow instance:

const map = useContext&lt;google.maps.Map | null&gt;(MapContext)
// ...
instance.open(map)

With that, we can import MapContext and wrap our &lt;InfoWindow&gt; with it to replace the map with a StreetViewPanorama instance, which we can get by using &lt;StreetViewPanorama&gt;'s onLoad event.

import { createRoot } from &quot;react-dom/client&quot;;
import {
  useLoadScript,
  GoogleMap,
  StreetViewPanorama,
  Marker,
  InfoWindow,
  MapContext,
} from &quot;@react-google-maps/api&quot;;
import React, { useContext, useState } from &quot;react&quot;;

function App() {
  const astorPlace = { lat: 40.729884, lng: -73.990988 };
  const [panorama, setPanorama] =
    useState&lt;google.maps.StreetViewPanorama | null&gt;(null);
  const markers = [
    {
      position: { lat: 40.730031, lng: -73.991428 },
      icon: &quot;https://chart.apis.google.com/chart?chst=d_map_pin_icon&amp;chld=cafe|FFFF00&quot;,
      title: &quot;Cafe&quot;,
    },
    {
      position: { lat: 40.729681, lng: -73.991138 },
      icon: &quot;https://chart.apis.google.com/chart?chst=d_map_pin_icon&amp;chld=dollar|FFFF00&quot;,
      title: &quot;Bank&quot;,
    },
    {
      position: { lat: 40.729559, lng: -73.990741 },
      icon: &quot;https://chart.apis.google.com/chart?chst=d_map_pin_icon&amp;chld=bus|FFFF00&quot;,
      title: &quot;Bus Stop&quot;,
    },
  ];

  function onPanoramaLoad(panorama: google.maps.StreetViewPanorama) {
    panorama.setPosition(astorPlace);
    panorama.setPov({
      heading: 265,
      pitch: 0,
    });
    panorama.setVisible(true);
    setPanorama(panorama);
  }

  const { isLoaded, loadError } = useLoadScript({
    googleMapsApiKey: import.meta.env.VITE_GOOGLE_MAPS_API_KEY,
  });

  return (
    &lt;&gt;
      {isLoaded ? (
        &lt;GoogleMap center={astorPlace} zoom={18} id=&quot;map&quot;&gt;
          &lt;StreetViewPanorama onLoad={onPanoramaLoad} /&gt;
          {panorama ? (
            // @ts-ignore
            &lt;MapContext.Provider value={panorama}&gt;
              {markers.map((marker) =&gt; (
                &lt;Marker key={marker.title} {...marker}&gt;
                  &lt;InfoWindow position={marker.position}&gt;
                    &lt;div
                      style={{
                        backgroundColor: &quot;white&quot;,
                        opacity: 0.8,
                      }}
                    &gt;
                      &lt;h2&gt;{marker.title}&lt;/h2&gt;
                    &lt;/div&gt;
                  &lt;/InfoWindow&gt;
                &lt;/Marker&gt;
              ))}
            &lt;/MapContext.Provider&gt;
          ) : null}
        &lt;/GoogleMap&gt;
      ) : null}
      {loadError ? &lt;p&gt;{loadError.message}&lt;/p&gt; : null}
    &lt;/&gt;
  );
}

createRoot(document.getElementById(&quot;app&quot;)!).render(&lt;App /&gt;);

View this example live at CodeSandbox.

在使用 react-google-maps-api 中在街景视图上显示信息框/信息窗口。

huangapple
  • 本文由 发表于 2023年5月29日 19:07:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76356806.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定