Skip to content

Commit e79a89e

Browse files
committed
fix(examples): update types
1 parent 1f24ff8 commit e79a89e

File tree

8 files changed

+116
-55
lines changed

8 files changed

+116
-55
lines changed

examples/distance-matrix-service/components/distance-matrix-service/distance-matrix-service.tsx

+12
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ const DistanceMatrixService = () => {
6363

6464
// Get distance matrix response
6565
service.getDistanceMatrix(request, response => {
66+
if (!response) {
67+
return;
68+
}
69+
6670
const origins: Array<string> = response.originAddresses;
6771
const destinations: Array<string> = response.destinationAddresses;
6872
const responseElements = response.rows[0].elements;
@@ -72,6 +76,10 @@ const DistanceMatrixService = () => {
7276

7377
// Geocode the response to set a marker at the positions of the origin and the destinations
7478
geocoder.geocode({address: origins[0]}, results => {
79+
if (!results) {
80+
return;
81+
}
82+
7583
const position = results[0]?.geometry.location;
7684

7785
// Add another marker icon for the origin
@@ -82,6 +90,10 @@ const DistanceMatrixService = () => {
8290

8391
destinations.forEach(destination => {
8492
geocoder.geocode({address: destination}, results => {
93+
if (!results) {
94+
return;
95+
}
96+
8597
const position = results[0]?.geometry.location;
8698

8799
createMarker(position);

examples/elevation-service/components/elevation-service/elevation-service.tsx

+27-6
Original file line numberDiff line numberDiff line change
@@ -31,20 +31,41 @@ const ElevationService = () => {
3131
const clickListener = map.addListener(
3232
'click',
3333
(mapsMouseEvent: google.maps.MapMouseEvent) => {
34+
const {latLng} = mapsMouseEvent;
35+
36+
if (!latLng) {
37+
return;
38+
}
39+
3440
// Update infowindow with new position and elevation info
35-
infoWindow.setPosition(mapsMouseEvent.latLng);
41+
infoWindow.setPosition(latLng);
3642

3743
// Retrieve elevation info from elevator
3844
elevator.getElevationForLocations(
39-
{locations: [mapsMouseEvent.latLng]},
40-
(results: google.maps.ElevationResult[]) => {
45+
{locations: [latLng]},
46+
(
47+
results: google.maps.ElevationResult[] | null,
48+
status: google.maps.ElevationStatus
49+
) => {
50+
if (status !== google.maps.ElevationStatus.OK || !results) {
51+
console.error(status);
52+
53+
return;
54+
}
55+
56+
const {location, elevation} = results[0];
57+
58+
if (!location || !elevation) {
59+
return;
60+
}
61+
4162
// eslint-disable-next-line no-console
4263
console.log(results);
4364

44-
map.setCenter(results[0].location);
65+
map.setCenter(location);
4566

46-
infoWindow.setPosition(results[0].location);
47-
infoWindow.setContent(`Elevation: ${results[0].elevation}`);
67+
infoWindow.setPosition(location);
68+
infoWindow.setContent(`Elevation: ${elevation}`);
4869
}
4970
);
5071
}

examples/geocoding-service/components/geocoding-service/geocoding-service.tsx

+18-13
Original file line numberDiff line numberDiff line change
@@ -63,25 +63,30 @@ const GeocodingService = () => {
6363
geocoder?.geocode(
6464
{location: mapsMouseEvent.latLng},
6565
(
66-
results: google.maps.GeocoderResult[],
66+
results: google.maps.GeocoderResult[] | null,
6767
status: google.maps.GeocoderStatus
6868
) => {
69-
if (status === 'OK') {
70-
const position = results[0].geometry.location;
71-
const formattedAddress = results[0].formatted_address;
69+
if (status !== 'OK' || !results) {
70+
console.error(
71+
`Geocoding was not successful for the following reason: ${status}`
72+
);
7273

73-
marker.setPosition(position);
74+
return;
75+
}
7476

75-
infoWindow.setPosition(position);
76-
infoWindow.setContent(formattedAddress);
77+
const position = results[0].geometry.location;
78+
const formattedAddress = results[0].formatted_address;
7779

78-
map.setCenter(results[0].geometry.location);
79-
} else {
80-
// eslint-disable-next-line no-console
81-
console.log(
82-
`Geocode was not successful for the following reason: ${status}`
83-
);
80+
if (!position || !formattedAddress) {
81+
return;
8482
}
83+
84+
marker.setPosition(position);
85+
86+
infoWindow.setPosition(position);
87+
infoWindow.setContent(formattedAddress);
88+
89+
map.setCenter(results[0].geometry.location);
8590
}
8691
);
8792
}

examples/max-zoom-service/components/max-zoom-service/max-zoom-service.tsx

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,14 @@ const MaxZoomService = () => {
3131

3232
// Function to show the maximum zoom level of a location
3333
const showMaxZoomLevel = (event: google.maps.MapMouseEvent) => {
34+
const {latLng} = event;
35+
36+
if (!latLng) {
37+
return;
38+
}
39+
3440
maxZoomService.getMaxZoomAtLatLng(
35-
event.latLng,
41+
latLng,
3642
(result: google.maps.MaxZoomResult) => {
3743
if (result.status !== 'OK') {
3844
// eslint-disable-next-line no-console

examples/places-autocomplete-widget/components/places-autocomplete-widget/places-autocomplete-widget.tsx

+9-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,15 @@ const PlacesAutocompleteWidget = () => {
1616
const onPlaceChanged = (place: google.maps.places.PlaceResult) => {
1717
if (place) {
1818
setSelectedPlace(place);
19-
setInputValue(place.formatted_address || place.name);
19+
20+
const formattedAddress = place.formatted_address;
21+
const {name} = place;
22+
23+
if (!formattedAddress || !name) {
24+
return;
25+
}
26+
27+
setInputValue(formattedAddress || name);
2028

2129
// Keep focus on input element
2230
inputRef.current?.focus();

examples/places-service-with-element/components/places-service-with-element/places-service-with-element.tsx

+8-4
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ const PlacesServiceElement = () => {
3333
};
3434

3535
function callback(
36-
results: google.maps.places.PlaceResult[],
36+
results: google.maps.places.PlaceResult[] | null,
3737
status: google.maps.places.PlacesServiceStatus
3838
) {
39-
if (status === google.maps.places.PlacesServiceStatus.OK) {
40-
setPlaceResults(results);
39+
if (status !== google.maps.places.PlacesServiceStatus.OK || !results) {
40+
console.error(status);
41+
42+
return;
4143
}
44+
45+
setPlaceResults(results);
4246
}
4347

4448
service.nearbySearch(request, callback);
@@ -50,7 +54,7 @@ const PlacesServiceElement = () => {
5054
<h1>Amazing restaurants in Istanbul</h1>
5155
<ul className={styles.restaurantList}>
5256
{placeResults.map((place, index) => {
53-
const name = place.name;
57+
const name = place.name || 'N/A';
5458
const rating = place.rating || 'N/A';
5559

5660
return (

examples/places-service/components/places-service/places-service.tsx

+26-22
Original file line numberDiff line numberDiff line change
@@ -24,37 +24,41 @@ const PlacesService = () => {
2424
};
2525

2626
function callback(
27-
results: google.maps.places.PlaceResult[],
27+
results: google.maps.places.PlaceResult[] | null,
2828
status: google.maps.places.PlacesServiceStatus
2929
) {
30-
if (status === google.maps.places.PlacesServiceStatus.OK) {
31-
for (let index = 0; index < results.length; index++) {
32-
const name = results[index].name;
33-
const position = results[index].geometry?.location;
34-
const openingHours = results[index].opening_hours;
30+
if (status !== google.maps.places.PlacesServiceStatus.OK || !results) {
31+
console.error(status);
3532

36-
const isOpenStatus = openingHours ? 'open' : 'closed';
33+
return;
34+
}
3735

38-
if (!map || !position) {
39-
return;
40-
}
36+
for (let index = 0; index < results.length; index++) {
37+
const name = results[index].name;
38+
const position = results[index].geometry?.location;
39+
const openingHours = results[index].opening_hours;
4140

42-
const marker = new google.maps.Marker({
43-
map,
44-
position
45-
});
41+
const isOpenStatus = openingHours ? 'open' : 'closed';
4642

47-
markers.push(marker);
43+
if (!map || !position) {
44+
return;
45+
}
4846

49-
map.fitBounds(bounds.extend(position));
47+
const marker = new google.maps.Marker({
48+
map,
49+
position
50+
});
5051

51-
const infowindow = new google.maps.InfoWindow({
52-
position,
53-
content: `<b>${name}</b> is ${isOpenStatus}`
54-
});
52+
markers.push(marker);
5553

56-
infowindow.open(map, marker);
57-
}
54+
map.fitBounds(bounds.extend(position));
55+
56+
const infowindow = new google.maps.InfoWindow({
57+
position,
58+
content: `<b>${name}</b> is ${isOpenStatus}`
59+
});
60+
61+
infowindow.open(map, marker);
5862
}
5963
}
6064

library/src/hooks/directions-service.ts

+9-8
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,13 @@ export const useDirectionsService = (
7676
directionsService.route(
7777
request,
7878
(
79-
result: google.maps.DirectionsResult,
79+
result: google.maps.DirectionsResult | null,
8080
status: google.maps.DirectionsStatus
8181
): void => {
82-
if (status === google.maps.DirectionsStatus.OK) {
83-
resolve(result);
84-
} else {
82+
if (status !== google.maps.DirectionsStatus.OK || !result) {
8583
reject(status);
84+
} else {
85+
resolve(result);
8686
}
8787
}
8888
);
@@ -101,16 +101,17 @@ export const useDirectionsService = (
101101
directionsService.route(
102102
request,
103103
(
104-
result: google.maps.DirectionsResult,
104+
result: google.maps.DirectionsResult | null,
105105
status: google.maps.DirectionsStatus
106106
): void => {
107-
if (status === google.maps.DirectionsStatus.OK) {
107+
if (status !== google.maps.DirectionsStatus.OK || !result) {
108+
reject(status);
109+
} else {
108110
if (directionsRenderer) {
109111
directionsRenderer.setDirections(result);
110112
}
113+
111114
resolve(result);
112-
} else {
113-
reject(status);
114115
}
115116
}
116117
);

0 commit comments

Comments
 (0)