【问题标题】:How to load google maps markers in a React Component如何在 React 组件中加载谷歌地图标记
【发布时间】:2018-07-03 12:13:09
【问题描述】:

我在这里发现这篇帖子有类似的问题,但没有帮助。 Google Map Marker as Reactjs Component

在我的数据库中,我存储了一些纬度和经度值。我在模型中检索这些值以向用户显示一些数据。我将这些值发送到我按照本教程创建的简单 Google 地图组件 https://tomchentw.github.io/react-google-maps/#introduction

组件从“props”接收“model”,我得到这些值并设置在两个变量中,并尝试将它们发送到 google 组件。

地图根本没有加载我发送的这些值。如果我不使用“parseFloat”,我会收到一条错误消息,指出这些值的格式不正确。

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

const MyMapComponent = withScriptjs(withGoogleMap((props) =>
 <GoogleMap
  defaultZoom={8}
  defaultCenter={{lat:parseFloat(props.lat),lng:parseFloat(props.long)}}
 >
  {props.isMarkerShown && <Marker position={{lat:-18.245630,lng:-45.222387}} 
 />}
 </GoogleMap>
))

export default class extends React.Component {
  constructor(props) {
  super(props)
  this.state = {
   isMarkerShown: false
 }
}
componentDidMount() {
 this.delayedShowMarker()
}
delayedShowMarker = () => {
 setTimeout(() => {
  this.setState({ isMarkerShown: true })
 }, 3000)
}
handleMarkerClick = () => {
  this.setState({ isMarkerShown: false })
  this.delayedShowMarker()
}
render() {
  let { model } = this.props;
  let lat = model.value.latitudeGeoreferencia
  let long = model.value.longitudeGeoreferencia
  return (
    <MyMapComponent
      isMarkerShown
      googleMapURL="https://maps.googleapis.com/maps/api/js?
      key=myKey.exp&libraries=geometry,drawing,places"
      loadingElement={<div style={{ height: `100%` }} />}
      containerElement={<div style={{ height: `400px` }} />}
      mapElement={<div style={{ height: `100%` }} />}
      lat
      long
    />
  )
 }
}

【问题讨论】:

    标签: javascript reactjs google-maps-api-3 google-maps-markers react-google-maps


    【解决方案1】:

    您没有将道具传递给MyMapComponent。如果您只是输入属性名称,它将收到 true 作为 prop 值。试试:

    <MyMapComponent
        isMarkerShown={this.state.isMarkerShown}
        googleMapURL="https://maps.googleapis.com/maps/api/js?
        key=myKey.exp&libraries=geometry,drawing,places"
        loadingElement={<div style={{ height: `100%` }} />}
        containerElement={<div style={{ height: `400px` }} />}
        mapElement={<div style={{ height: `100%` }} />}
        lat={lat}
        long={long}
    />
    

    【讨论】:

    • 非常感谢,成功了。您知道是否可以将标记值发送回前一个组件?换句话说,如果用户想要保存另一个位置,我需要将这些值发送回模型,以便我可以将其发送到我的控制器。
    • 当然,您需要将回调作为来自父组件的道具传递,查看this codepen 以及“仅显示库存产品”复选框如何与父组件交互。或者,您可以考虑使用 Redux,然后“连接”您的两个组件。网站上提供的示例大致涵盖了您正在寻找的内容 - redux.js.org/docs/basics/UsageWithReact.html
    • @ArthurMedeiros - 马特的建议对你有用吗?我想知道它是否可以应用于我的问题:stackoverflow.com/questions/49890542/…
    • @BaronGrivet 是的,他的建议对我有用。我读了你的问题,看来你在加载标记时遇到了问题,对吧?我在这里没有提到它,但就我而言,我正在使用 axios 调用从控制器中检索标记。这个 axios 调用被放置在“componentDidMount”中。你的 fetch 函数做同样的事情吗?我知道您的 JSON 在某处得到了更新,然后您想对其做出反应,我是对的吗?如果是这样,您是否尝试过使用 ComponentDidUpdate?但这还不够,您似乎需要一个循环来持续监听变化
    猜你喜欢
    • 2021-10-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-26
    • 2016-02-15
    • 2014-05-08
    • 1970-01-01
    相关资源
    最近更新 更多