简体   繁体   English

为什么我看不到谷歌地图上的任何标记有反应?

[英]Why can't I see any markers on google maps react?

I have been building the front end of an application that has the google maps API enabled.我一直在构建启用了谷歌地图 API 的应用程序的前端。 I want to have the user click on the map to add markers, and store it into an array;我想让用户点击地图添加标记,并将其存储到数组中; However after I implemented an add marker function to the onClick of the google map tag... The markers won't render.但是,在我为 google 地图标签的 onClick 实现了添加标记功能之后......标记不会呈现。

I have had a lot of difficulty implementing the add marker function in react, and watched several tutorials but cannot seem to find a solution.我在 React 中实现添加标记功能时遇到了很多困难,看了几个教程但似乎找不到解决方案。 Any help would be greatly appreciated!任何帮助将不胜感激!

    const options = {
        styles: mapStyles,
        disableDefaultUI: true,
        zoomControl: true,
    }

    var markersArray = [];
    function addMarker(event){
            let marker = new window.google.maps.Marker({
            position: event.latLng,
            time: new Date(),
            draggable: true
        });
        markersArray.push(marker);
    }


    const Map = withScriptjs(withGoogleMap(props =>
        <GoogleMap
          defaultZoom={8}
          defaultCenter={{ lat: this.state.mapPosition.lat, lng: this.state.mapPosition.lng }}
          options={options}
          onClick={(event) => {
            console.log(event);
            console.log(markersArray);
            addMarker(event);
        }}
        >

        {markersArray.map((marker) =>(
            <Marker
                key={marker.time.toISOString()}
                position={{lat: marker.lat, lng: marker.lng }}
            />
        ))}

        <AutoComplete
            style={{width: "100%", height:'40px'}}
            types={['(regions)']}
            onPlaceSelected={this.onPlaceSelected}
        />

        </GoogleMap>
      ));
      

I believe the issue is with this but i'm not sure how to make it work.我相信问题出在这一点上,但我不确定如何使其发挥作用。

        {markersArray.map((marker) =>(
            <Marker
                key={marker.time.toISOString()}
                position={{lat: marker.lat, lng: marker.lng }}
            />
        ))}

markersArray is a plain array. markersArray数组是一个普通数组。 When you do addMarker(event);当你做addMarker(event); you're not updating the state of your component, it's just a plain array.您没有更新组件的状态,它只是一个普通数组。 So probably React is not aware of that change.所以可能 React 没有意识到这种变化。 If you're using hooks, you could create a state with your markers like this如果你使用钩子,你可以用这样的标记创建一个状态

const [markers, setMarkers] = useState([])

use that markers array to render the <Marker /> and then on your event to add the marker使用该markers数组来呈现<Marker />然后在您的事件上添加标记

onClick={(event) => {
  setMarker(previousMarkers => previousMarkers.concat([event]))
}

this will cause a re-render with the new value of markers and you should be able to see them.这将导致使用新的markers值重新渲染,您应该能够看到它们。

If it's not a functional component and it's classes, it's the same, but you would define the markers in your class' state如果它不是功能组件而是类,则相同,但是您将在类的状态中定义标记

constructor(props) { 
  super(props)
  this.state = { markers: [] }
  this.addMarker = this.addMarker.bind(this)

}

// function class below
addMarker(marker) {
  this.setState(previousState => previousState.concat([marker])
}

and in your event, you'd call this.addMarker(marker)在你的活动中,你会调用this.addMarker(marker)

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

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