【问题标题】:react-google-maps pass props on marker clickreact-google-maps 在标记点击时传递道具
【发布时间】:2018-05-15 10:23:39
【问题描述】:

我正在尝试在我的应用中集成“react-google-maps”以显示地图。我很难理解他们的标记系统。

https://tomchentw.github.io/react-google-maps/#marker

我能够显示我的地图并正确显示我的所有标记。下一步是我遇到问题的地方。我需要能够单击每个标记并知道单击了哪个标记。我已经评论了以下代码,以显示我现在要在控制台中显示的道具。我认为我只需要在事件中传递另一个参数,但我不知道该怎么做。老实说,现在我相信我正在传递事件侦听器,但我什至不完全确定当我记录那个“e”变量时我在看什么。

你可以在我的 github 中看到我目前拥有的东西。如果您单击标记,您将看到我正在记录的内容。

https://joweber123.github.io/take-me-there/login/James

请帮忙,我在其他任何地方都找不到讨论的信息。非常感谢你

    import React, { Component } from 'react';
    import { withGoogleMap, GoogleMap, Marker, } from "react-google-maps";
    import { compose, withProps } from "recompose";

    class List extends Component {

      //I eventually will use this information to set state and all of that, but for now I just don't understand how to pass information to this function from my marker on onClick

      handleMarkerClick(e){
        console.log(e);
      }
      render(){

        const { compose, lifecycle} = require("recompose");

        const {
          withGoogleMap,
          GoogleMap,
          Marker,
        } = require("react-google-maps");

        const MapWithAMarker = compose(
        withGoogleMap
      )(props =>

        <GoogleMap
          defaultZoom={8}
          defaultCenter={{ lat: Number(`${this.props.locations[Object.keys(this.props.locations)[Object.keys(this.props.locations).length-1]].location.lat}`), lng: Number(`${this.props.locations[Object.keys(this.props.locations)[Object.keys(this.props.locations).length-1]].location.lng}`) }}
          locations={this.props.locations}
        >
        {
          Object
          .keys(this.props.locations)
          .map(key =>
          <Marker
            key={key}
            position={{ lat: Number(`${this.props.locations[key].location.lat}`), lng: Number(`${this.props.locations[key].location.lng}`) }}
            locations={this.props.locations[key]}
            //I want to be able to pass the information that is stored in my locations prop here, but I have no idea how to do that.  
            onClick={props.onMarkerClick}
          />
        )
          }
        </GoogleMap>
      );

      return (
        <div className = "location-list one-third column center border-main full-height">
            <MapWithAMarker
              loadingElement={<div style={{ height: `100%` }} />}
              containerElement={<div style={{ height: `400px` }} />}
              mapElement={<div style={{ height: `100%` }} />}
              locations={this.props.locations}
              //I am not even really sure what I am doing here.  What gets printed out in my console gives me some information but what I really want to be able to do is to pass the locations props of my specific marker and have access to that information
              onMarkerClick={(e)=>this.handleMarkerClick(e)}
            />
        </div>
      );
    }
    }


    export default List;

【问题讨论】:

  • 看起来这个库不是为了这样做......
  • 谢谢-BrandNew。是的,我实际上很难理解这个库的文档。看起来像是 onClick 和事物的提供道具和方法,但我实际上并不了解如何在我自己的地图中使用它。我刚刚添加了一个指向我的 gh-pages 的链接,该链接托管了我正在尝试做的演示。如果您能想出任何方法从该标记中获取一些信息,我将不胜感激。感谢您的帮助。

标签: reactjs google-maps google-maps-markers


【解决方案1】:

我想通了!我之前只是传递了事件侦听器,所以它当然不包含我需要的任何信息。我也不需要将它的父级传递给它,因为只需让它触发自己的事件就足够了。谢谢大家看这个。

onClick={()=> this.props.handleMarkerClick(this.props.locations[key])}

我想我没有问自己正确的问题,但是一旦我找到了这两个答案,我就更好地理解了我需要做什么。

React - Passing props to child onClick

Pass props to parent component in React.js

【讨论】:

    【解决方案2】:

    你能展示 this.props.locations 中包含的内容吗?通常你想映射位置,每个位置都应该有某种 id 字段。而不是通过键映射,您应该通过位置数组进行映射。这样,当调用 onClick 函数时,您可以传递 id(如果它们在位置对象本身中,甚至可以传递坐标,我认为它们会传递)。

    【讨论】:

    • 您好 Rodius,感谢您抽出宝贵时间查看此内容。我只是把我目前所拥有的东西放到了 github 上,这样你就可以更深入地了解它。 this.props.locations 包含一个由时间戳组成的唯一键,加上从我的输入元素的输入字段中获取的名称。抱歉,如果这令人困惑,但我认为如果您查看托管该页面的 gh 页面,您可以了解我在做什么。
    猜你喜欢
    • 2019-01-31
    • 2017-10-30
    • 1970-01-01
    • 1970-01-01
    • 2017-06-04
    • 1970-01-01
    • 2019-09-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多