React-google-maps: Custom button to map

Created on 26 Jul 2015  路  11Comments  路  Source: tomchentw/react-google-maps

Hi,

how to create custom button like control button ?
Thanks for any advice.

Most helpful comment

Is there an update on this issue? I'm trying to add it into my ReactJS project using ES6 and RxJS. I have tried to convert it over and get no errors, but it also isn't displaying at all by following this issue and the Custom Controls docs in Google Maps.

All 11 comments

According to the docs, I believe you need something like this

map.controls[google.maps.ControlPosition.BOTTOM_LEFT].push(centerControlDiv);

However, this might not be supported at the moment. Will look into this in our roadmap.

+1 for this feature

I found a way to make this work. So apparently it is hidden a few layers deep. Here is how I was able to push a button onto the map. Let's say you rendered your map with the following.

<GoogleMap ref={(map) => (this._googleMapComponent = map)}...

Then you can push a button on the map with the following

var map = this._googleMapComponent.props.map; map.controls[google.maps.ControlPosition.BOTTOM_LEFT].push(centerControlDiv);

@frankwong this is work. But if your centerControlDiv is react component, you will get a trouble later, because react cannot find that component you move. My suggestion to create menu on top of google map is just create react component and put it on top of map by set z-index.

@frankwong and where I have to write this code?

@n0ne, you can add it anywhere you have access to googleMapComponent. Below is an example of how I structured the code

const MapView = React.createClass({
  ...
  _insertButton: (parentThis, map)=> {
    let controlDiv = document.createElement('div');
    map.controls[google.maps.ControlPosition.RIGHT_BOTTOM].push(controlDiv);
  },
  render: ()=> {
    return (
      <GoogleMapLoader
        googleMapElement={
          <GoogleMap
            ref={(map) => (this._googleMapComponent = map)}
            ...
            controls={this._insertButton}
          >
          ...
          </GoogleMap>
        }
      />
    );
  }
});

Feel free to submit a PR. Close for now

We're also looking for maintainers. Involve in #266 to help strengthen our community!

@frankwong hi
im a lil bit perplexed as to how i should port your solution to mine. can you post the entire code of it for our reference?

Is there an update on this issue? I'm trying to add it into my ReactJS project using ES6 and RxJS. I have tried to convert it over and get no errors, but it also isn't displaying at all by following this issue and the Custom Controls docs in Google Maps.

@Michael-Brooks you can try add this JSX code

<SearchBox controlPosition={window.google.maps.ControlPosition.BOTTOM_LEFT}> <div> <input type="hidden" className="input-search-map" placeholder="Search..." /> <Your button component here... /> </div> </SearchBox>

@Michael-Brooks and anyone who is looking into this issue, so sorry I forgot to mention my solution. Basically the code posted works, however you will need to make sure the reference is ready. So here is what I do

if (this.mapComponentRef.current) {
      return (
        <CustomControl
          mapRef={this.mapComponentRef.current}
          controlPosition={window.google.maps.ControlPosition.RIGHT_CENTER}
        >
          <CusomZoomControl />
        </CustomControl>
      );
    }

I hope this helps.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

julienvincent picture julienvincent  路  3Comments

tahir-masood1 picture tahir-masood1  路  4Comments

LukasZvikas picture LukasZvikas  路  3Comments

0x1bitcrack3r picture 0x1bitcrack3r  路  3Comments

ShintaroNippon picture ShintaroNippon  路  3Comments