【问题标题】:google-map-react marker not showinggoogle-map-react 标记未显示
【发布时间】:2019-11-02 19:24:32
【问题描述】:

大家好,我正在尝试在 react.js 中实现谷歌地图,地图显示正常,但是当我尝试显示我的标记时,没有显示任何内容。如果我删除 lat 和 long 值,则标记将显示在地图顶部。但如果我向它添加 lat 和 long 值,它就会停止显示。有人可以帮忙吗?

import React, { Component } from 'react';
import GoogleMapReact from 'google-map-react';
import marker from './marker.png';
import { geolocated } from 'react-geolocated';
const AnyReactComponent = ({ text }) => (
  <div>
    <img src={marker} style={{ height: '50px', width: '50px' }} />
  </div>
);






export default class Map extends Component {
  static defaultProps = {
    zoom: 11,
  };

  Componentwillmount() {
    console.log(this.props.center.latitude);
  }



  render() {
    return (
      <div className="google-map" style={{ width: '100%', height: '2000px', backgroundColor: 'red' }}>
        <GoogleMapReact
          options={{
            styles:ExampleMapStyles
        }}
         center={this.props.center} defaultZoom={this.props.zoom}>
        <AnyReactComponent
            lat={59.955413}
            lng={30.337844}
            text={'Kreyser Avrora'}
          />
        </GoogleMapReact>
      </div>
    );
  }
}

【问题讨论】:

    标签: reactjs


    【解决方案1】:

    文档显示您可以使用&lt;AnyReactCompenent/&gt;,如果您想拥有自己的标记或文本,可以使用它。但是,如果您希望使用默认标记,则必须将其作为属性传递给 &lt;GoogleMapReact&gt;&lt;/GoogleMapReact&gt; 组件。文档提到“您可以使用onGoogleApiLoaded 访问Google Maps 地图和地图对象,在这种情况下,您需要将yesIWantToUseGoogleMapApiInternals 设置为true”。这只是意味着添加属性的名称而不像我在我的那样向它传递值。从自述文件中的描述可能并不完全清楚,但 maps 参数实际上是 maps API 对象(并且 map 当然是当前的 Google Map 实例)。因此,您应该将两者都传递到带有renderMarkers = (map, maps) =&gt; {} 函数的方法中。

    你可以试试这个:

    import React, { Fragment } from 'react';
    import GoogleMapReact from 'google-map-react';
    
    const GoogleMaps = ({ latitude, longitude }) => {
     const renderMarkers = (map, maps) => {
      let marker = new maps.Marker({
       position: { lat: latitude, lng: longitude },
       map,
       title: 'Hello World!'
       });
       return marker;
     };
    
     return (
      <Fragment>
       <div style={{ height: '50vh', width: '100%' }}>
        <GoogleMapReact
         bootstrapURLKeys={{ key: 'YOUR KEY' }}
         defaultCenter={{ lat: latitude, lng: longitude }}
         defaultZoom={16}
         yesIWantToUseGoogleMapApiInternals
         onGoogleApiLoaded={({ map, maps }) => renderMarkers(map, maps)}
        >
        </GoogleMapReact>
       </div>
      </Fragment>
     );
    };
    
    export default GoogleMaps;
    

    【讨论】:

    • 请不要只发布代码作为答案,而是说明您的代码的作用以及它如何解决问题的问题。带有解释的答案通常质量更高,更有可能吸引投票。
    【解决方案2】:

    我认为您需要回答的主要问题是:您的中心道具是什么? 我在沙盒中使用了您的代码并做了一些小的修改:我使用了相同的纬度/经度,您的标记应该被渲染,以便一目了然地查看它是否实际显示。它是: https://codesandbox.io/s/00p1ry2v0v

    export default class Map extends Component {
      static defaultProps = {
        zoom: 11,
        center: {
          lat: 49.955413,
          lng: 30.337844
        }
      };
    
      render() {
        const mapStyles = {
          width: "100%",
          height: "100%"
        };
        return (
          <div
            className="google-map"
            style={{ width: "100%", height: "2000px", backgroundColor: "red" }}
          >
            <GoogleMapReact
              style={mapStyles}
              center={this.props.center}
              defaultZoom={this.props.zoom}
            >
              <AnyReactComponent
                lat={49.955413}
                lng={30.337844}
                text={"Kreyser Avrora"}
              />
            </GoogleMapReact>
          </div>
        );
      }
    }
    

    因此,请确保从正确的初始中心位置开始地图。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-02
      • 2012-04-10
      • 2020-02-17
      • 2014-02-23
      • 2014-10-20
      • 2012-04-21
      • 2022-07-27
      • 1970-01-01
      相关资源
      最近更新 更多