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

Geocoder not implemented ?!

Open Bartaf83 opened this issue 3 years ago • 4 comments

I need to make Reverse Geocoding (as in this example).

It seems not implemented yet.

Bartaf83 avatar Jul 01 '21 14:07 Bartaf83

I also need

s6091214 avatar Jul 14 '21 06:07 s6091214

PR is welcome

JustFly1984 avatar Jul 19 '21 13:07 JustFly1984

I was try something and find solution as below:

import { useState } from 'react';
import { GoogleMap, LoadScript, Marker } from '@react-google-maps/api';

const LIBRARIES = ["places"];

const containerStyle = {
  width: "100%",
  height: '600px'
};

const CENTER = {
  lat: -3.745,
  lng: -38.523
};

const Map = ({ mapApiKey, center,...props }) => {
  const [position, setPosition] = useState(center);

  const onClick = async (event) => {
    const { latLng: { lat, lng } } = event;
    const latlng = { lat: lat(), lng: lng() };

    setPosition(latlng);

    const geocoder = new window.google.maps.Geocoder();
    const response = await geocoder.geocode({ location: latlng });
  }

  return (
    <LoadScript
      googleMapsApiKey={mapApiKey}
      libraries={LIBRARIES}
    >
      <GoogleMap
        {...props}
        center={center}
        onClick={onClick}
      >
        <Marker
          position={position}
        />
      </GoogleMap>
    </LoadScript>
  );
};

const App = () => {
  return (
    <Map
      mapApiKey={""} //your API key, remember enable Geocoding API
      center={CENTER}
      mapContainerStyle={containerStyle}
      zoom={15}
      options={{ streetViewControl: false }}
    />
  );
};

hkngoc avatar Aug 11 '21 10:08 hkngoc

in your case onClick will always re-render whole app. you need to chache it with useCallback hook, and also wrap Map in memo() before using it in App

JustFly1984 avatar Aug 11 '21 11:08 JustFly1984