Browse Source

Add reverse geocoding domain function to UI

Kirk Trombley 4 years ago
parent
commit
ed8257bdc2
2 changed files with 16 additions and 1 deletions
  1. 1 1
      client/public/index.html
  2. 15 0
      client/src/domain/geocoding.js

+ 1 - 1
client/public/index.html

@@ -12,7 +12,7 @@
     <link rel="manifest" href="%PUBLIC_URL%/manifest.json" />
     <title>TerrAssumptions</title>
     <!-- Statically and synchronously load the Google Maps Javascript API -->
-    <script src="https://maps.googleapis.com/maps/api/js?key=%REACT_APP_GOOGLE_API_KEY%"></script>
+    <script src="https://maps.googleapis.com/maps/api/js?key=%REACT_APP_GOOGLE_API_KEY%&v=beta"></script>
   </head>
   <body>
     <noscript>You need to enable JavaScript to run this app.</noscript>

+ 15 - 0
client/src/domain/geocoding.js

@@ -0,0 +1,15 @@
+/* global google */
+
+const GEOCODER = new google.maps.Geocoder();
+
+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) {
+        return short_name;
+      }
+    }
+  }
+  return null;
+};