反应谷歌地图不显示HeatMap,纬度和长期不适合我的位置

时间:2019-03-11 17:18:14

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

This what i want, each marker to be sharing the same position as the lat and lng as the heatmap, the markers are working perfectly 在向我的热图发送数据时遇到问题,好像Google的热图接收的纬度和经度与标记的位置不同吗?任何想法 ?当我创建一个这样的对象列表数组时,但是我希望我的数据读取就像我的标记正在读取一样。纬度为M,经度为N。如果我将其放入{this.props.policeCall.map(({ A, M, N }) => {这样的包装器中 当我将热图位置放入position={{lat: M , lng:N}}

时出现错误

export class MapContainer extends Component {
  state = {
    showingInfoWindow: false, //Hides or the shows the infoWindow
    activeMarker: {}, //Shows the active marker upon click
    selectedPlace: {} //Shows the infoWindow to the selected place upon a marker
  };

  onMarkerClick = (props, marker, e) =>
    this.setState({
      selectedPlace: props,
      activeMarker: marker,
      showingInfoWindow: true
    });

  onClose = props => {
    if (this.state.showingInfoWindow) {
      this.setState({
        showingInfoWindow: false,
        activeMarker: null
      });
    }
  };

  render() {
   
    return (
      <Map
        google={this.props.google}
        zoom={14}
        style={mapStyles}
        initialCenter={{
          lat: 32.71573699,
          lng: -117.16108799
        }}
      >
        {this.props.policeCall.map(({ A, M, N }) => {
          return (
            <Marker
              onClick={this.onMarkerClick}
              name={A}
              position={{ lat: M, lng: N }}
            />
          );
        })}

        {this.props.policeCall.map(({ A, M, N }) => {
          return (
            <HeatMap
              gradient={gradient}
              opacity={3}
              position={{lat: M , lng:N}}
              radius={30}
              
            />
          );
        })}

        <InfoWindow
          marker={this.state.activeMarker}
          visible={this.state.showingInfoWindow}
          onClose={this.onClose}
        >
          <div>
            <h4>{this.state.selectedPlace.name}</h4>
          </div>
        </InfoWindow>
      </Map>
    );
  }
}

这是我从json读取数据时的样子,M和N是lat和lng如何将其注入我的热图数据

module.exports = [
    {"A": "P17060024503", "B": "6/14/2017 21:54", "C": "4", "D": "10", "E": "", "F": "14TH", "G": "ST", "H": "10 14TH ST, San Diego, CA", "I": "1151", "J": "O", "K": "521", "L": "2", "M": "32.7054489", "N": "-117.1518696"},
    { "A": "P17030051227", "B": "3/29/2017 22:24", "C": "4", "D": "10", "E": "", "F": "14TH", "G": "ST", "H": "10 14TH ST, San Diego, CA", "I": "1016", "J": "A", "K": "521", "L": "2", "M": "32.7054544", "N": "-117.1467137"},
    { "A": "P17060004814", "B": "6/3/2017 18:04", "C": "7", "D": "10", "E": "", "F": "14TH", "G": "ST", "H": "10 14TH ST, San Diego, CA", "I": "1016", "J": "A", "K": "521", "L": "2", "M": "32.7053961", "N": "-117.1444185"},
    { "A": "P17030029336", "B": "3/17/2017 10:57", "C": "6", "D": "10", "E": "", "F": "14TH", "G": "ST", "H": "10 14TH ST, San Diego, CA", "I": "1151", "J": "OT", "K": "521", "L": "2", "M": "32.7054244", "N": "-117.1425917"},
    { "A": "P17030005412", "B": "3/3/2017 23:45", "C": "6", "D": "10", "E": "", "F": "15TH", "G": "ST", "H": "10 15TH ST, San Diego, CA", "I": "911P", "J": "CAN", "K": "521", "L": "2", "M": "32.7055067", "N": "-117.1405936"},

1 个答案:

答案 0 :(得分:0)

根据source code of google-maps-react libraryHeatMap组件期望使用positions强制属性而不是position

       <HeatMap
          gradient={gradient}
          opacity={3}
          position={{lat: M , lng:N}}
          ^^^^^^^^^^^^^^^^^^^^^^^^^^
          radius={30}
        />

鉴于提供的数据格式,HeatMap可以这样初始化:

class MapContainer extends React.Component {

  render() {

    const positions = data.map(item => { return { "lat": item.M, "lng": item.N}});

    return (
      <div className="map-container">
        <Map
          google={this.props.google}
          className={"map"}
          zoom={this.props.zoom}
          initialCenter={this.props.center}
        >
          <HeatMap
            gradient={gradient}
            positions={positions}
            opacity={1}
            radius={20}
          />
        </Map>
      </div>
    );
  }
}

data存储您的JSON数据

最后但并非最不重要的一点,请确保已加载visualization程序包(取决于Google Heatmaps):

export default GoogleApiWrapper({
  apiKey: "--YOUR-KEY-GOES-HERE--",
  libraries: ["visualization"]
})(MapContainer);

Here is a demo(该示例已从Google Maps文档改编而成)