javascript - 在 google-map-react 上动态添加标记

标签 javascript reactjs google-maps

我不想做的是在 map 上显示从某些移动设备选择的位置。 关于位置的数据在那里..

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

假设我已将位置数据({Lat,Lang})设置为州标记 然后如何添加它以在 map 中显示。

我的 map 代码如下!

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

最佳答案

你可以试试:

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>
    );
  }

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

关于javascript - 在 google-map-react 上动态添加标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43937887/

相关文章:

javascript - 为什么在运行成功回调之前解决 promise ?

reactjs - AG 网格 : cellRenderer disables value formatter

javascript - 为什么这条新线路刚刚出现在我的谷歌地图上

android - 在谷歌地图上显示天气和云

javascript - .innerHTML 不更新 Javascript 中的 HTML

javascript - 不理解局部变量和全局变量的概念

javascript - 在 React hook 中,handleChange 中没有 e.target 和 setValue()

reactjs - 模块解析失败:filename. jsx,您可能需要适当的加载器来处理此文件类型

java - 填充 map Overlay

javascript - 显示颜色选择器对话框而不显示输入选项?