【发布时间】: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