【问题标题】:Dynamically Adding Markers on google-map-react在 google-map-react 上动态添加标记
【发布时间】:2017-10-11 18:47:12
【问题描述】:

我不想在地图上显示从某些移动设备中挑选的位置。 有关位置的数据在那里..

我这里需要的是根据从服务器接收到的数据在地图上添加标记。

假设我已将位置数据 ({Lat,Lang}) 设置为 状态标记 那么如何添加它以显示在地图中。

我的地图代码如下!

import React, { Component } from 'react';
import GoogleMapReact from 'google-map-react';

const AnyReactComponent = ({ text }) => <div>{text}</div>;

class MyClass extends Component {
  constructor(props){
    super(props);

  }

  render() {
    return (
      <GoogleMapReact
        defaultCenter={this.props.center}
        defaultZoom={this.props.zoom}
        style={{height: '300px'}}
      >
        <AnyReactComponent
          lat={59.955413}
          lng={30.337844}
          text={'Google Map'}
        />
      </GoogleMapReact>
    );
  }
}
MyClass.defaultProps = {
  center: {lat: 59.95, lng: 30.33},
  zoom: 11
};

export default MyClass;

此代码来自答案Implementing google maps with react

使用的 npm 包:- google-map-react

【问题讨论】:

    标签: javascript reactjs google-maps


    【解决方案1】:

    小心。你说react-google-map,但你使用的是google-map-react。那是2个不同的包。不要混淆他们的文档。

    【讨论】:

    • 链接对我不起作用。给出 404。也许它是一个私有存储库。 :)
    【解决方案2】:

    你可以试试:

    import React, { Component } from 'react';
    import GoogleMapReact from 'google-map-react';
    
    const AnyReactComponent = ({  img_src }) => <div><img src={img_src} className="YOUR-CLASS-NAME" style={{}} /></div>;
    
    class MyClass extends Component {
      constructor(props){
        super(props);
        this.state = {
            markers: [],
        }
      }
    
      componentDidMount(){
        // or you can set markers list somewhere else
        // please also set your correct lat & lng
        // you may only use 1 image for all markers, if then, remove the img_src attribute ^^
        this.setState({
          markers: [{lat: xxxx, lng: xxxx, img_src: 'YOUR-IMG-SRC'},{lat: xxxx, lng: xxxx, img_src: 'YOUR-IMG-SRC' },{lat: xxxx, lng: xxxx,  img_src: 'YOUR-IMG-SRC'}],
        });
      }
    
      render() {
        return (
          <GoogleMapReact
            defaultCenter={this.props.center}
            defaultZoom={this.props.zoom}
            style={{height: '300px'}}
          >
                {this.state.markers.map((marker, i) =>{
                  return(
                    <AnyReactComponent
                      lat={marker.lat}
                      lng={marker.lng}
                      img_src={marker.img_src}
                    />
    
                  )
                })}      
          </GoogleMapReact>
        );
      }
    }
    MyClass.defaultProps = {
      center: {lat: 59.95, lng: 30.33},
      zoom: 11
    };
    

    如果这里有错误,也请在这里显示,我们稍后会修复它

    ============

    为标记上的点击事件添加示例

     markerClicked(marker) {
       console.log("The marker that was clicked is", marker);
       // you may do many things with the "marker" object, please see more on tutorial of the library's author:
      // https://github.com/istarkov/google-map-react/blob/master/API.md#onchildclick-func 
      // Look at their examples and you may have some ideas, you can also have the hover effect on markers, but it's a bit more complicated I think 
     }
    
     render() {
        return (
          <GoogleMapReact
            defaultCenter={this.props.center}
            defaultZoom={this.props.zoom}
            style={{height: '300px'}}
          >
                {this.state.markers.map((marker, i) =>{
                  return(
                    <AnyReactComponent
                      lat={marker.lat}
                      lng={marker.lng}
                      img_src={marker.img_src}
                      onChildClick={this.markerClicked.bind(this, marker)}
                    />
    
                  )
                })}      
    
          </GoogleMapReact>
        );
      }
    

    再一次,如果有错误,请在这里发布一些错误^^!

    【讨论】:

    • 没有错误!但是,图像不出现!只是一个正方形。在 Image 属性类中,我应该输入为 png/jpg/ico 我对吗?
    • 您是否检查了那些“方形”图像以检查它们的 url,也许它们的 src 无效^^?此外,请使用 img 的有效 url,不要添加 png/ico 或任何东西 ^^ 这里在 jsx 中的 className 只是表示他们的 CSS 类(你可以像 className="" 一样将其留空)谢谢
    • 是的!来源有问题!..你能给我最后一个帮助吗?
    • 如果我不想得到标记的 onTap 事件!我应该如何进行?非常感谢好友!你为我节省了几天的工作时间!
    • 不客气^^!实际上,上面的答案只是GoogleMap for react的一个非常简单的例子。我们使用的库用于在地图上渲染 react-component,这对于服务器端渲染很有用,因此,标记的点击事件会稍微复杂一些。我现在将向您展示一些关于如何实现点击事件的演示,但如果您真的想拥有更“自然”的 googlemap 的完整功能(标记+事件、折线、方向等),您可以考虑使用另一个强大的库: github.com/tomchentw/react-google-maps
    猜你喜欢
    • 2017-05-15
    • 2011-12-12
    • 2016-06-22
    • 2019-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-17
    • 2020-12-03
    相关资源
    最近更新 更多