简体   繁体   English

如何将自定义按钮添加到 react-google-maps?

[英]How to add custom buttons to react-google-maps?

I'm trying to add an in app map to my application.我正在尝试将应用内地图添加到我的应用程序中。 In this I need to add a button which will console log ("Hi I'm here") when clicked.在此,我需要添加一个按钮,单击该按钮将控制台日志(“您好,我在这里”)。

Package used here is "react-google-maps"这里使用的包是“react-google-maps”

const handleOnLoad = GoogleMap => {
  const controlButtonDiv = document.createElement('div');
  ReactDOM.render(<CustomControl onClick={() => console.log('Hi I'm Here')} />, controlButtonDiv);
  GoogleMap.controls[window.google.maps.ControlPosition.TOP_RIGHT].push(controlButtonDiv);
};

<GoogleMap
  defaultZoom={16}
  defaultCenter={{ lat: this.props.latVal, lng: this.props.longVal}}
  onTilesLoaded={GoogleMap => handleOnLoad(GoogleMap)}
  >
    <Marker 
    position={this.state.progress[this.state.progress.length - 1]}
    icon={{
    url: 'https://images.vexels.com/media/users/3/154573/isolated/preview/bd08e000a449288c914d851cb9dae110-hatchback-car-top-view-silhouette-by-vexels.png',
    scaledSize: new window.google.maps.Size(50, 50),
    anchor: { x: 10, y: 10 }
    }}
    />
    <Polyline 
    path={this.state.progress} 
    options={{ strokeColor: "#FF0000" }} 
    />
    <Circle 
    defaultCenter={{lat: this.props.latVal, lng: this.props.longVal}} 
    radius={parseInt(this.props.geofence)}
    options={{strokeColor: "#ff0000"}}
    />          
  </GoogleMap>

If you are using React >= 16.8 (hooks) I suggest you update to @react-google-maps/api .如果您使用的是 React >= 16.8(钩子),我建议您更新到@react-google-maps/api Then you can create a reusable MapControl with a few lines of code:然后你可以用几行代码创建一个可重用的 MapControl:

interface MapControlProps {
  position: keyof typeof google.maps.ControlPosition;
}
const MapControl = (props: React.PropsWithChildren<MapControlProps>) => {
  const map = useGoogleMap();
  const ref = useRef();
  useEffect(() => {
    if (map && ref) {
      map.controls[window.google.maps.ControlPosition[props.position]].push(
        ref.current
      );
    }
  }, [map, ref]);
  return <div ref={ref}>{props.children}</div>;
};

Then you can place a <MapControl> inside your <GoogleMap> using any content you like然后您可以使用您喜欢的任何内容在<GoogleMap>放置一个<MapControl>

<GoogleMap
  defaultZoom={16}
  defaultCenter={{ lat: this.props.latVal, lng: this.props.longVal}}
  onTilesLoaded={GoogleMap => handleOnLoad(GoogleMap)}
  >
  <MapControl position="LEFT_BOTTOM">
    <button
      style={{ backgroundColor: "red" }}
      onClick={() => console.log('Hi I'm Here')}
    >
      CLICK ME
    </button>
    <input placeholder="type some stuff" />
  </MapControl>
</GoogleMap>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在react-google-maps中添加标记? - How to add markers in react-google-maps? 如何使用 react-google-maps 库将自定义 label 添加到单击的标记? - How to add custom label to clicked Marker using react-google-maps libary? 如何在@react-google-maps/api 中添加自定义 ico 或 SVG 作为标记图标 - How to add custom ico or SVG as marker icon in @react-google-maps/api 如何使用react-google-maps通过点击在地图上添加标记? - How to add marker on map by Click using react-google-maps? 如何在react-google-maps中弯曲折线? - How to curve a Polyline in react-google-maps? 如何在react-google-maps中嵌入textview? - How to embed textview in react-google-maps? 如何将“外部”事件侦听器添加到 Google 地图中的标记(react-google-maps/api) - How to add "outside" event listener to Markers in Google Maps (react-google-maps/api) 在react-google-maps中自定义样式的Google地图 - Custom styled Google map in react-google-maps 如何使用 react-google-maps “在 Google 地图上查看” - How to "view on Google maps" with react-google-maps 如何使用 react-google-maps 为使用 create-react-app 创建的 React 应用程序添加 Google Maps API 密钥? - How to add the Google Maps API key for a React app created with create-react-app using react-google-maps?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM