|
@@ -1,3 +1,4 @@
|
|
|
+import iso from 'iso-3166-1';
|
|
|
/* global google */
|
|
|
|
|
|
const GEOCODER = new google.maps.Geocoder();
|
|
@@ -7,7 +8,7 @@ export const reverseGeocode = async location => {
|
|
|
const { results } = await GEOCODER.geocode({ location });
|
|
|
for (const { address_components } of results) {
|
|
|
for (const { short_name, types } of address_components) {
|
|
|
- if (types.indexOf("country") >= 0) {
|
|
|
+ if (types.indexOf('country') >= 0) {
|
|
|
return short_name;
|
|
|
}
|
|
|
}
|
|
@@ -17,4 +18,20 @@ export const reverseGeocode = async location => {
|
|
|
// TODO probably alert the user?
|
|
|
}
|
|
|
return null;
|
|
|
-};
|
|
|
+};
|
|
|
+
|
|
|
+export const getCountryBounds = async countryCode => {
|
|
|
+ const { country } = iso.whereAlpha2(countryCode);
|
|
|
+ try {
|
|
|
+ const { results } = await GEOCODER.geocode({ address: country });
|
|
|
+ for (const { geometry, types } of results) {
|
|
|
+ if (geometry.viewport && types.indexOf('country') >= 0) {
|
|
|
+ return geometry.viewport;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ // ignore errors - just use null
|
|
|
+ // TODO is there really no recovery from this?
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+}
|