Custom Zoom Control UI
Hello, this is the first time I am opening issue in this project. I have a question regarding adding custom control. Here is my environment:
- "react-google-maps": "^9.4.5"
- "react": "^16.3.2"
- "react-dom": "^16.3.2"
Upon some googling and stackoverflow searching. I have found some good references: For example:
- https://jsfiddle.net/maunovaha/jptLfhc8/
- https://developers.google.com/maps/documentation/javascript/examples/control-custom
However, I don't think the .controls are in the google.maps object.
Following is my code:
class MapComponent extends Component {
constructor(props){
super(props);
this.mapComponentRef = React.createRef();
}
render(){
return (
<GoogleMap
ref={this.mapComponentRef}
/>);
}
}
I can confirm that I can access the ref of the map. For example, I use
this.mapComponentRef.current.fitBounds(this.bounds);
to update the bounds of the map and it works.
However, when I try to do this.mapComponentRef.current.controls[google.maps.ControlPosition.TOP_CENTER].push(centerControlDiv), it gives me errors that the controls is undefined.
Does anyone has pointer on how to get access the controls so I can push my custom component to control the zoom level of the map?
I was adding a customized button to the map a couple months ago. Basically I used a component based on your first link. Here's the CustomControl component:
import React, { Component } from 'react';
class CustomControl extends Component {
constructor(props) {
super();
this.state = {
flag: true
};
}
addToMap(root) {
if (this.state.flag) {
const { mapHolderRef, controlPosition } = this.props;
mapHolderRef.context.__SECRET_MAP_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.controls[
controlPosition
].push(root);
this.setState({ flag: false });
}
}
render() {
return <div ref={this.addToMap.bind(this)}>{this.props.children}</div>;
}
}
export default CustomControl;
Then you should add this CustomControl in your MapComponent, pass the map ref, and wrap your zoom control component as a child of CustomControl:
class MapComponent extends Component {
constructor(props) {
super(props);
this.mapComponentRef = React.createRef();
}
render() {
return (
<GoogleMap ref={this.mapComponentRef}>
<CustomControl
mapHolderRef={this.mapComponentRef}
controlPosition={window.google.maps.ControlPosition.TOP_LEFT}
>
<YourZoomControl onClick={() => this.mapComponentRef.map.panTo(center)} />
</CustomControl>
</GoogleMap>
);
}
}
Thanks @AonanLi for your reply. I tried it but still couldn't get it working. I see same error that I can't read property 'context' of null.
Here is how I call this custom control. It should be aligned with what you provided.
<CustomControl
mapHolderRef={this.mapComponentRef}
controlPosition={window.google.maps.ControlPosition.BOTTOM_RIGHT}
>
<div onClick={() => this.mapComponentRef.current.map.setZoom(8)}>testing component</div>
</CustomControl>
I think it's possibly because your "mapComponentRef" wasn't created yet when you call "addToMap".
I didn't use "React.createRef()" in my code to get the map ref, so for my case the ref is already there.
You could try this to see if it could work:
addToMap(root) {
const { mapHolderRef, controlPosition } = this.props;
if (mapHolderRef) {
if (this.state.flag) {
mapHolderRef.context.__SECRET_MAP_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.controls[
controlPosition
].push(root);
this.setState({ flag: false });
}
}
}
If it doesn't work, try to get your map ref before rendering your map component. For my case I got the map ref in "onMapMounted" and stored it in redux store.
you just need to add current to ref
<CustomControl
mapHolderRef={this.mapComponentRef.current}
controlPosition={window.google.maps.ControlPosition.TOP_LEFT}
>
<div onClick={() => this.mapComponentRef.current.map.setZoom(8)}>testing component</div>
</CustomControl>
For anyone who is looking into this issue. I have posted my solution in https://github.com/tomchentw/react-google-maps/issues/82#issuecomment-412579391 .
I will close this PR now. Thanks so much for the help.