0

So I kinda got lost and I tried several codes. I managed to implement the markers however I think i'm doing something incorrect to make them actually clickable and display an InfoWindow.

2 Answers2

0

The Marker by default isn't clickable, besides passing the onClick handler, you need to pass the clickable prop.

You can check all the Marker props in the documentation: https://tomchentw.github.io/react-google-maps/#marker

jonathanrz
  • 4,206
  • 6
  • 35
  • 58
  • Hey, thanks for helping out! I still can't figure out how to do that. Everything else on the map is clickable but not my markers.. – Kostya Inverse Aug 25 '18 at 16:56
0

You have mixed react-google-maps and google-maps-react. Here is the working example for your case: Please add you APIkey in url.

App.js

import React from "react";
import ReactDOM from "react-dom";

const { compose, withProps, withStateHandlers } = require("recompose");
const {
  withScriptjs,
  withGoogleMap,
  GoogleMap,
  Marker
} = require("react-google-maps");
const { InfoBox } = require("react-google-maps/lib/components/addons/InfoBox");


const App = compose(
  withProps({
    googleMapURL: "https://maps.googleapis.com/maps/api/js?key=**YOUR_API_KEY**&v=3.exp&libraries=geometry,drawing,places",
    loadingElement: <div style={{ height: `100%` }} />,
    containerElement: <div style={{ height: `400px` }} />,
    mapElement: <div style={{ height: `100%` }} />,
    center: { lat: 25.03, lng: 121.6 },
  }),
  withStateHandlers(() => ({
    isOpen: false,
  }), {
      onToggleOpen: ({ isOpen }) => () => ({
        isOpen: !isOpen,
      })
    }),
  withScriptjs,
  withGoogleMap
)(props =>
  <GoogleMap
    defaultZoom={5}
    defaultCenter={props.center}
  >

    <Marker
      position={{ lat: 22.6273, lng: 120.3014 }}
      onClick={props.onToggleOpen}
    >
      {props.isOpen && <InfoBox
        onCloseClick={props.onToggleOpen}
        options={{ closeBoxURL: ``, enableEventPropagation: true }}
      >
        <div style={{ backgroundColor: `yellow`, opacity: 0.75, padding: `12px` }}>
          <div style={{ fontSize: `16px`, fontColor: `#08233B` }}>
            no search place
          </div>
        </div>
      </InfoBox>}
    </Marker>
  </GoogleMap>
);
export default App;

react-google-maps is smart component based using recomose but you can make it class base component too.

This is working codesandbox (temporarily live) : https://codesandbox.io/s/2wwjmx673r Don't forget to add your API key. feel free to ask any question.

Sakhi Mansoor
  • 7,832
  • 5
  • 22
  • 37
  • Oh wow you're amazing! No wonder I had trouble after mixing those two lol. Now my Maps.js is completely useless if I implement your code.EDIT:I still have to make the marker change color when i click it, and implement an info window there with a link from FourSquare or something... Should I do it in your code or make another .js file for Foursquare? – Kostya Inverse Aug 25 '18 at 17:35
  • Glad to help you. – Sakhi Mansoor Aug 25 '18 at 17:35
  • Wait I just noticed the code won't let me add more markers? – Kostya Inverse Aug 25 '18 at 17:40
  • How do i add more? I get an error right away if I try adding any tags – Kostya Inverse Aug 25 '18 at 17:41
  • Okay!! Thank you very much – Kostya Inverse Aug 25 '18 at 17:46
  • 1
    Hey there. So everything worked out fine. I had to re-do my entire thing from scratch because It was difficult for me to understand the logic behind everything, but thank you so much! Either way, I need your help again. This is my sandbox: https://codesandbox.io/s/0338vv69zv And for some reason my markers dont change color when clicked. I'm also trying to figure out how to implement an InfoBox in there again lol. Do you know how to implement a FourSquare window inside of the info window when clicked? – Kostya Inverse Aug 26 '18 at 16:35
  • Sure. Happy to help – Sakhi Mansoor Aug 26 '18 at 16:36
  • Sorry my comment was sent before I could finish writing, i edited it – Kostya Inverse Aug 26 '18 at 16:38
  • okay. But you must post another question on stackoverflow So coomunity will be aware of it If someone would face the issue in future. Then your posts will be helpful for particular issues. and You should accept this one as it served your purpose previously. I hope this would make sense. – Sakhi Mansoor Aug 26 '18 at 16:48
  • oh yeah it makes sense. It just won't let me post since I'm new on stack overflow so I have to wait until tomorrow... Which is why I asked you because you know this area so well – Kostya Inverse Aug 26 '18 at 17:03