|
| 1 | +import {useEffect, useState} from 'react'; |
| 2 | +import {useGeocoder, useGoogleMap} from '@ubilabs/google-maps-react-hooks'; |
| 3 | + |
| 4 | +const GeocoderExample = () => { |
| 5 | + const {map} = useGoogleMap(); |
| 6 | + |
| 7 | + // Get the geocoder from the useGeocoder hook |
| 8 | + const geocoder = useGeocoder(); |
| 9 | + |
| 10 | + const initialPosition = {lat: 51.08998021141488, lng: 10.627828045134935}; |
| 11 | + |
| 12 | + const [marker, setMarker] = useState<google.maps.Marker | null>(null); |
| 13 | + const [infoWindow, setInfoWindow] = useState<google.maps.InfoWindow | null>( |
| 14 | + null |
| 15 | + ); |
| 16 | + |
| 17 | + useEffect(() => { |
| 18 | + if (!map) { |
| 19 | + return () => {}; |
| 20 | + } |
| 21 | + |
| 22 | + // Add an initial marker |
| 23 | + if (!marker) { |
| 24 | + const initialMarker = new google.maps.Marker({ |
| 25 | + map, |
| 26 | + position: initialPosition |
| 27 | + }); |
| 28 | + |
| 29 | + setMarker(initialMarker); |
| 30 | + |
| 31 | + return () => {}; |
| 32 | + } |
| 33 | + |
| 34 | + // Add an initial infowindow |
| 35 | + if (!infoWindow) { |
| 36 | + const initialInfoWindow = new google.maps.InfoWindow({ |
| 37 | + content: |
| 38 | + 'Click somewhere on the map to reverse geocode the position to an address.', |
| 39 | + position: initialPosition |
| 40 | + }); |
| 41 | + |
| 42 | + setInfoWindow(initialInfoWindow); |
| 43 | + initialInfoWindow.open(map, marker); |
| 44 | + |
| 45 | + return () => {}; |
| 46 | + } |
| 47 | + |
| 48 | + // Click on the map and open an infowindow with the reversed geocoded address. |
| 49 | + map.addListener('click', (mapsMouseEvent: google.maps.MapMouseEvent) => { |
| 50 | + // Use the geocoder to reverse geocode the position from the map |
| 51 | + // and add the address as content of the infowindow |
| 52 | + geocoder?.geocode( |
| 53 | + {location: mapsMouseEvent.latLng}, |
| 54 | + ( |
| 55 | + results: google.maps.GeocoderResult[], |
| 56 | + status: google.maps.GeocoderStatus |
| 57 | + ) => { |
| 58 | + if (status === 'OK') { |
| 59 | + const position = results[0].geometry.location; |
| 60 | + const formattedAddress = results[0].formatted_address; |
| 61 | + |
| 62 | + marker.setPosition(position); |
| 63 | + |
| 64 | + infoWindow.setPosition(position); |
| 65 | + infoWindow.setContent(formattedAddress); |
| 66 | + |
| 67 | + map.setCenter(results[0].geometry.location); |
| 68 | + } else { |
| 69 | + // eslint-disable-next-line no-console |
| 70 | + console.log( |
| 71 | + `Geocode was not successful for the following reason: ${status}` |
| 72 | + ); |
| 73 | + } |
| 74 | + } |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + // Clean up listeners and remove marker and infowindow instances |
| 79 | + return () => { |
| 80 | + google.maps.event.clearListeners(map, 'click'); |
| 81 | + }; |
| 82 | + }, [map, infoWindow, marker]); |
| 83 | + |
| 84 | + return null; |
| 85 | +}; |
| 86 | + |
| 87 | +export default GeocoderExample; |
0 commit comments