You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// create a table with 2 columns: `rowid` and `_shape`
148
150
awaitdb.sql`CREATE VIRTUAL TABLE polygons USING geopoly()`;
149
151
152
+
// create a table with 5 columns: `id`, `name`, `lng`, `lat`, and `coordinates`
150
153
awaitdb.sql`CREATE TABLE attractions (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, lng REAL NOT NULL, lat REAL NOT NULL, coordinates TEXT NOT NULL)`;
151
154
155
+
// populate the `attractions` table using the GeoJSON FeatureCollection in `geodata.json`
152
156
for (constfeatureof geodata['features']) {
153
157
const { name } =feature.properties;
154
158
const { coordinates } =feature.geometry;
@@ -166,12 +170,6 @@ async function createDatabase() {
166
170
167
171
createDatabase();
168
172
```
169
-
170
-
- The `createDatabase` function:
171
-
- connects to your `geopoly-demo` database;
172
-
- creates a virtual `polygons` table with 2 columns: `rowid` and `_shape`;
173
-
- creates an `attractions` table with 5 columns: `id`, `name`, `lng`, `lat`, and `coordinates`; and
174
-
- populates the `attractions` table using the GeoJSON FeatureCollection in `geodata.json`.
- To understand how the app works, let's break down this monster component's core functionality:
677
-
678
-
- The first `useEffect`:
679
-
- creates, styles, and centers the map;
680
-
- applies 3 controls to the top right of the map: a geocoder (i.e. address search input), an icon to toggle the map to fullscreen mode, and an icon to locate the app user on the map;
681
-
- sets an event listener to track the map center coordinates and zoom level, all displayed on the top left of the app; and
682
-
- sets an event listener on the geocoder to call the `queryGeopoly` function when the user clicks a geocoder result.
683
-
684
-
- The `queryGeopoly` function:
685
-
- connects to your `geopoly-demo` database;
686
-
- uses the `geopoly_regular` function to generate a polygon and adds it to the `polygons` table (set a positive `radius` and up to 1000 `sides`);
687
-
- returns all named attractions in the polygon area;
688
-
- uses Turf.js to calculate the distance between the searched location and each attraction (set `units` to be miles or kilometers);
689
-
- applies clickable markers for all attractions on the map;
690
-
- upsorts attractions nearest the user's searched location;
691
-
- uses the `getBbox` helper function (defined in the next step) to zoom the map to the attraction nearest the searched location; and
692
-
- updates the `geometry` state to hold the returned Polygon and attraction Point features.
693
-
694
-
- The second `useEffect`:
695
-
- is triggered by the `geometry` state update; and
696
-
- calls the `drawFeatureCollection` function to draw the returned Polygon, its outline, and attraction Points on the map.
697
-
698
692
**8. Create a helper function**
699
693
700
694
- Create `src/helpers/getBbox.js`. Copy and paste in the following code:
@@ -727,17 +721,15 @@ export function getBbox(sortedEvents, locationLng, locationLat) {
727
721
}
728
722
return0;
729
723
});
724
+
725
+
// return a bounding box, defined by a southwest coordinate pair and northeast coordinate pair
730
726
return [
731
727
[sortedLons[0], sortedLats[0]],
732
728
[sortedLons[1], sortedLats[1]],
733
729
];
734
730
}
735
731
```
736
732
737
-
- The `getBbox` function:
738
-
- generates a bounding box, defined by a southwest coordinate pair and northeast coordinate pair; and
739
-
- is used in `queryGeopoly` to fit/ zoom the map view to a user's searched location and its nearest attraction.
740
-
741
733
**9. Run your app!**
742
734
743
735
- Replace your `package.json` code with the following, which includes all dependencies needed to run the app:
@@ -814,11 +806,12 @@ SELECT rowid, geopoly_json(_shape) FROM polygons;
814
806
815
807
- You can click on any attraction listing or marker to fly/ zoom to and center on that attraction on the map.
816
808
817
-
- Turf.js calculates straight-line distances between your searched location and each attraction. Expect discrepancies between this app's calculated distances vs, say, Google or Apple Maps.
809
+
-[Turf.js uses the Haversine formula](https://turfjs.org/docs/api/distance) to account for global curvature when calculating the distance between your searched location and each attraction. However, you should still expect discrepancies between this app's calculated distances vs, say, Google or Apple Maps.
818
810
819
811
And that’s it! You’ve successfully built a local attractions finder app that utilizes Geopoly to write geodata to and read from a SQLite Cloud database.
820
812
821
813
### Additional Guidance on Overpass:
814
+
822
815
- To fetch other attractions or any other kind of location data in NY or another area of interest to you, refer to [OpenStreetMap's Map features documentation](https://wiki.openstreetmap.org/wiki/Map_features). As a starting point, modify the area or key-value pairs in the NY query.
823
816
824
817
- NOTE: The app works only with Point features (represented in the [Map features](https://wiki.openstreetmap.org/wiki/Map_features) tables' `Element` columns by an icon with a single dot). Be sure to query only nodes and the key-value pairs that can return Point data. For example, don't use most of the values available for the Boundary key.
0 commit comments