π‘ Home π Chapter π Next
β‘Β GMapsBook.com is crafted by Jozef Sorocin (π’Β Book a consulting hour) and powered by:
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
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.
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
.