🏑 Home πŸ“– Chapter πŸ‘‰ Next

⚑  GMapsBook.com is crafted by Jozef Sorocin (🟒 Book a consulting hour) and powered by:

Scenario

My users can place markers, circles, polygons and polylines on the map. I’d like to store these in a geospatial database that expects them in the β€œGeoJSON format”. What is GeoJSON and how do I convert the user-generated Google Maps shapes to GeoJSON?

User-generated Google Maps artifacts ready for export

User-generated Google Maps artifacts ready for export

Introduction to GeoJSON

As a JavaScript developer, you’re already be familiar with JavaScript Object Notation (JSON). After all, you already saw it in action when we discussed the .toJSON() method in the chapter on Markers, Lines, & Shapes.

JSON is human readable, predictable, and easily serializable. It powers RESTful APIs and, at the end of the day, lets you interact with this handbook.

Now, GeoJSON is a popular JSON-based open standard used for encoding geographic data structures. It’s standardized, lightweight, and based on the same coordinate system adopted by Google Maps.

Comparison to Google Maps

In Google Maps, coordinates pairs always declare the respective coordinate type, i.e.:

const coordinates = { "lat": 40.6419, "lng": -73.9921 };

GeoJSON is a bit more efficient. It drops the lat / lng keywords and works exclusively with arrays:

const coordinates = [-73.9921, 40.6419];

Notice that the longitude comes before the latitude.

In a way, this makes sense β€” longitude corresponds to the x axis, latitude to y.

Positions, geometries, features, collections