diff --git a/examples/distance-matrix-service/components/distance-matrix-service/distance-matrix-service.tsx b/examples/distance-matrix-service/components/distance-matrix-service/distance-matrix-service.tsx index 2b9f1cb..9ecfb4f 100644 --- a/examples/distance-matrix-service/components/distance-matrix-service/distance-matrix-service.tsx +++ b/examples/distance-matrix-service/components/distance-matrix-service/distance-matrix-service.tsx @@ -63,6 +63,10 @@ const DistanceMatrixService = () => { // Get distance matrix response service.getDistanceMatrix(request, response => { + if (!response) { + return; + } + const origins: Array = response.originAddresses; const destinations: Array = response.destinationAddresses; const responseElements = response.rows[0].elements; @@ -72,6 +76,10 @@ const DistanceMatrixService = () => { // Geocode the response to set a marker at the positions of the origin and the destinations geocoder.geocode({address: origins[0]}, results => { + if (!results) { + return; + } + const position = results[0]?.geometry.location; // Add another marker icon for the origin @@ -82,6 +90,10 @@ const DistanceMatrixService = () => { destinations.forEach(destination => { geocoder.geocode({address: destination}, results => { + if (!results) { + return; + } + const position = results[0]?.geometry.location; createMarker(position); diff --git a/examples/elevation-service/components/elevation-service/elevation-service.tsx b/examples/elevation-service/components/elevation-service/elevation-service.tsx index 28f887a..ba4e906 100644 --- a/examples/elevation-service/components/elevation-service/elevation-service.tsx +++ b/examples/elevation-service/components/elevation-service/elevation-service.tsx @@ -31,20 +31,41 @@ const ElevationService = () => { const clickListener = map.addListener( 'click', (mapsMouseEvent: google.maps.MapMouseEvent) => { + const {latLng} = mapsMouseEvent; + + if (!latLng) { + return; + } + // Update infowindow with new position and elevation info - infoWindow.setPosition(mapsMouseEvent.latLng); + infoWindow.setPosition(latLng); // Retrieve elevation info from elevator elevator.getElevationForLocations( - {locations: [mapsMouseEvent.latLng]}, - (results: google.maps.ElevationResult[]) => { + {locations: [latLng]}, + ( + results: google.maps.ElevationResult[] | null, + status: google.maps.ElevationStatus + ) => { + if (status !== google.maps.ElevationStatus.OK || !results) { + console.error(status); + + return; + } + + const {location, elevation} = results[0]; + + if (!location || !elevation) { + return; + } + // eslint-disable-next-line no-console console.log(results); - map.setCenter(results[0].location); + map.setCenter(location); - infoWindow.setPosition(results[0].location); - infoWindow.setContent(`Elevation: ${results[0].elevation}`); + infoWindow.setPosition(location); + infoWindow.setContent(`Elevation: ${elevation}`); } ); } diff --git a/examples/geocoding-service/components/geocoding-service/geocoding-service.tsx b/examples/geocoding-service/components/geocoding-service/geocoding-service.tsx index e7dfdd8..205430b 100644 --- a/examples/geocoding-service/components/geocoding-service/geocoding-service.tsx +++ b/examples/geocoding-service/components/geocoding-service/geocoding-service.tsx @@ -63,25 +63,30 @@ const GeocodingService = () => { geocoder?.geocode( {location: mapsMouseEvent.latLng}, ( - results: google.maps.GeocoderResult[], + results: google.maps.GeocoderResult[] | null, status: google.maps.GeocoderStatus ) => { - if (status === 'OK') { - const position = results[0].geometry.location; - const formattedAddress = results[0].formatted_address; + if (status !== 'OK' || !results) { + console.error( + `Geocoding was not successful for the following reason: ${status}` + ); - marker.setPosition(position); + return; + } - infoWindow.setPosition(position); - infoWindow.setContent(formattedAddress); + const position = results[0].geometry.location; + const formattedAddress = results[0].formatted_address; - map.setCenter(results[0].geometry.location); - } else { - // eslint-disable-next-line no-console - console.log( - `Geocode was not successful for the following reason: ${status}` - ); + if (!position || !formattedAddress) { + return; } + + marker.setPosition(position); + + infoWindow.setPosition(position); + infoWindow.setContent(formattedAddress); + + map.setCenter(results[0].geometry.location); } ); } diff --git a/examples/index.d.ts b/examples/index.d.ts index 3e187d2..5e3ca51 100644 --- a/examples/index.d.ts +++ b/examples/index.d.ts @@ -1,4 +1,4 @@ -declare module 'googlemaps'; +declare module 'google.maps'; /* eslint-disable init-declarations */ declare module '*.module.css' { diff --git a/examples/max-zoom-service/components/max-zoom-service/max-zoom-service.tsx b/examples/max-zoom-service/components/max-zoom-service/max-zoom-service.tsx index 5b84af8..7f2d022 100644 --- a/examples/max-zoom-service/components/max-zoom-service/max-zoom-service.tsx +++ b/examples/max-zoom-service/components/max-zoom-service/max-zoom-service.tsx @@ -31,8 +31,14 @@ const MaxZoomService = () => { // Function to show the maximum zoom level of a location const showMaxZoomLevel = (event: google.maps.MapMouseEvent) => { + const {latLng} = event; + + if (!latLng) { + return; + } + maxZoomService.getMaxZoomAtLatLng( - event.latLng, + latLng, (result: google.maps.MaxZoomResult) => { if (result.status !== 'OK') { // eslint-disable-next-line no-console diff --git a/examples/package.json b/examples/package.json index 44364dd..37114bd 100644 --- a/examples/package.json +++ b/examples/package.json @@ -32,7 +32,7 @@ "react-dom": "^18.2.0" }, "devDependencies": { - "@types/googlemaps": "^3.43.3", + "@types/google.maps": "^3.50.5", "@types/react": "^18.0.21", "@types/react-dom": "^18.0.6", "cross-env": "^7.0.3", diff --git a/examples/places-autocomplete-widget/components/places-autocomplete-widget/places-autocomplete-widget.tsx b/examples/places-autocomplete-widget/components/places-autocomplete-widget/places-autocomplete-widget.tsx index a33c15e..aa52092 100644 --- a/examples/places-autocomplete-widget/components/places-autocomplete-widget/places-autocomplete-widget.tsx +++ b/examples/places-autocomplete-widget/components/places-autocomplete-widget/places-autocomplete-widget.tsx @@ -16,7 +16,15 @@ const PlacesAutocompleteWidget = () => { const onPlaceChanged = (place: google.maps.places.PlaceResult) => { if (place) { setSelectedPlace(place); - setInputValue(place.formatted_address || place.name); + + const formattedAddress = place.formatted_address; + const {name} = place; + + if (!formattedAddress || !name) { + return; + } + + setInputValue(formattedAddress || name); // Keep focus on input element inputRef.current?.focus(); diff --git a/examples/places-service-with-element/components/places-service-with-element/places-service-with-element.tsx b/examples/places-service-with-element/components/places-service-with-element/places-service-with-element.tsx index c918234..ab82699 100644 --- a/examples/places-service-with-element/components/places-service-with-element/places-service-with-element.tsx +++ b/examples/places-service-with-element/components/places-service-with-element/places-service-with-element.tsx @@ -33,12 +33,16 @@ const PlacesServiceElement = () => { }; function callback( - results: google.maps.places.PlaceResult[], + results: google.maps.places.PlaceResult[] | null, status: google.maps.places.PlacesServiceStatus ) { - if (status === google.maps.places.PlacesServiceStatus.OK) { - setPlaceResults(results); + if (status !== google.maps.places.PlacesServiceStatus.OK || !results) { + console.error(status); + + return; } + + setPlaceResults(results); } service.nearbySearch(request, callback); @@ -50,7 +54,7 @@ const PlacesServiceElement = () => {

Amazing restaurants in Istanbul