[英]Combining google-maps-react with react-router
So I'm rendering a bunch of <Marker />
that are giving by google-maps-react
The problem is that it don't seems like google-maps-react
likes it when I add a <Link />
that comes from react-router-dom
所以我渲染了一堆由
google-maps-react
提供的<Marker />
问题是当我添加一个来自react-router-dom
的<Link />
时,它似乎不像google-maps-react
喜欢它react-router-dom
This is how I put it together :这就是我把它放在一起的方式:
render() {
return (
<div>
<Map
google={this.props.google}
zoom={14}
styles={this.props.mapStyles}
disableDefaultUI={true}
onClick={this.saveCoords}
>
{this.state.data.map(m => {
return (
<Link to={`/c/contribution/${m.id}`}>
<Marker position={{ lat: m.x, lng: m.y }} title={m.title} />
</Link>
)
})}
</Map>
</div>
)
}
I tried using window.location
instead but this reloads the page and I don't want that.我尝试使用
window.location
但这会重新加载页面,我不想要那样。
I get this error with the code above, it don't really makes sense for me :我在上面的代码中遇到了这个错误,这对我来说没有意义:
Warning: React does not recognize the `mapCenter` prop on a DOM element. If you intentionally want it to appear in the DOM as a custom attribute, spell it as lowercase `mapcenter` instead. If you accidentally passed it from a parent component, remove it from the DOM element.
With this I try to achieve a clickable <Marker/>
that will render another element.有了这个,我尝试实现一个可点击的
<Marker/>
来呈现另一个元素。 This specific elemend can be accessed by going to the Route
present in the code example.可以通过转到代码示例中的
Route
来访问此特定元素。
The route used :使用的路线:
<Route path='/c/contribution/:id' component={Contribution} />
Unfortunately, you cannot wrap markers with google-maps-react
like that because of how the map's children are rendered.不幸的是,由于地图子项的呈现方式,您不能像那样用
google-maps-react
包装标记。 I didn't look into that too deep, so why exactly it works the way it does is unclear to me.我没有深入研究,所以我不清楚为什么它会以这种方式工作。
With google-maps-react
I think the way to go is to attach click handlers, but that makes working with react-router
a bit more complicated.使用
google-maps-react
我认为要走的路是附加点击处理程序,但这使得使用react-router
变得更加复杂。
For maybe an easier path, you could try the google-map-react
package.为了更简单的路径,您可以尝试使用
google-map-react
包。 It works a bit differently, but makes it easy to render almost anything on the map.它的工作方式略有不同,但可以轻松渲染地图上的几乎所有内容。
You can use the onClick
event on the markers for the redirect.您可以在标记上使用
onClick
事件进行重定向。 Set up a state property for the path you want to redirect to, and if that property is not an empty string , instead of renering your normal component, return a <Redirect/>
element.为您要重定向到的路径设置一个 state 属性,如果该属性不是空 string ,则返回一个
<Redirect/>
元素,而不是重新生成您的普通组件。
constructor(props){
super(props)
this.state = {
...
redirectTo:"
};
}
this.setRedirect = (path)=>{
this.setState({
redirectTo:path
}
}
render(){
if(this.state.redirectTo !== "")
return <Redirect to={this.state.redirectTo}/>
else
return (
<div>
<Map
google={this.props.google}
zoom={14}
styles={this.props.mapStyles}
disableDefaultUI={true}
onClick={this.saveCoords}
>
{this.state.data.map(m => {
return (
<Marker onClick={()=>this.setRedirect(`/c/contribution/${m.id}`)} position={{ lat: m.x, lng: m.y }} title={m.title} />
)
})}
</Map>
</div>
)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.