简体   繁体   English

如何在react-google-maps中添加标记?

[英]How to add markers in react-google-maps?

Using React JS in Meteor 1.5 Meteor 1.5中使用React JS

Question : Need a way to add Marker using react-google-maps 问题 :需要一种方法来使用react-google-maps添加标记

Using ES6 and in JSX format 使用ES6和JSX格式

Followed the documentation and was able to get the map embedded in, but not able to add the marker. 遵循文档并能够嵌入地图,但无法添加标记。

Here is my code: 这是我的代码:

const InitialMap = withGoogleMap(props => {
  var index = this.marker.index || [];

  return(
    <GoogleMap
      ref={props.onMapLoad}
      defaultZoom={14}
      defaultCenter={{lat: 40.6944, lng:-73.9213}}
      >
        <Marker
          key={index}
          position={marker.position}
          onClick={() => props.onMarkerClick(marker)}
        />
      </GoogleMap>
  )
});

export default class MapContainer extends Component{
  constructor(props){
    this.state = {
      markers:[{
        position:{
          lat: 255.0112183,
          lng:121.52067570000001,
        }
      }]
    }
  }
  render(){
    return(
      <div style={{height:"100%"}}>
        <InitialMap
          containerElement={
            <div style={{height:"100%"}}/>
          }
          mapElement={
            <div style={{height:"100%"}} />
          }
          markers={this.state.markers} />
      </div>
    )
  }
}

Added the first constant 添加了第一个常量

const GettingStartedGoogleMap = withGoogleMap(props => (
  <GoogleMap
    ref={props.onMapLoad}
    zoom={13}
    center={{ lat: 21.178574, lng: 72.814149 }}
    onClick={props.onMapClick}
  >
    {props.markers.map(marker => (
      <Marker
        {...marker}
        onRightClick={() => props.onMarkerRightClick(marker)}
      />
    ))}
  </GoogleMap>

Changed the containerElement size and mapElement size to pixels instead of percentage 将containerElement大小和mapElement大小更改为像素而不是百分比

  containerElement={
    <div style={{ height: `150px` }} />
  }
  mapElement={
    <div style={{ height: `150px` }} />
  }

And just adding marker to the function which was called 只需将标记添加到被调用的函数中

markers={this.state.markers}

I'd check over your lat, lng coordinates again. 我再次检查你的lat,lng坐标。 From google explaining coordinates 谷歌解释坐标

"Check that the first number in your latitude coordinate is between -90 and 90." “检查纬度坐标中的第一个数字是否在-90到90之间。”

Also any other error info would be helpful getting an answer for you. 此外,任何其他错误信息将有助于为您找到答案。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM