react-google-maps icon indicating copy to clipboard operation
react-google-maps copied to clipboard

Custom button to map

Open knapeto opened this issue 11 years ago • 11 comments

Hi,

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

knapeto avatar Jul 26 '15 19:07 knapeto

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.

tomchentw avatar Aug 04 '15 23:08 tomchentw

+1 for this feature

sohibul avatar Feb 27 '16 03:02 sohibul

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 avatar Mar 12 '16 02:03 frankwong

@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.

sohibul avatar Mar 12 '16 03:03 sohibul

@frankwong and where I have to write this code?

n0ne avatar Apr 04 '16 12:04 n0ne

@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>
        }
      />
    );
  }
});

frankwong avatar Apr 11 '16 20:04 frankwong

Feel free to submit a PR. Close for now

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

tomchentw avatar May 29 '16 09:05 tomchentw

@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?

akosialz avatar Nov 10 '16 05:11 akosialz

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 avatar Feb 21 '17 09:02 Michael-Brooks

@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>

asonni avatar Apr 18 '18 11:04 asonni

@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.

rockia avatar Aug 13 '18 16:08 rockia