version 2.0
GeoJSON for Google Maps enables you to create Google Maps API v3 overlays from GeoJSON objects and create GeoJSON objects from Google Maps overlays.
Convert GeoJSON objects into Google Maps overlays.
geojson - A supported and valid GeoJSON object. See the official GeoJSON spec for more details, including the list of examples.
options - (optional) Options for the specific type of Google Maps vector (Marker, Polyline, Polygon). If none specified, boring black vectors and red markers will be created.
// Convert the GeoJSON into a Google Maps Marker
var marker = google.maps.geojson.from(geojson);
// Add the marker to the map
marker.setMap(map);
GeoJSON | Google Maps |
---|---|
Point | google.maps.Marker |
LineString | google.maps.Polyline |
Polygon | google.maps.Polygon |
MultiPoint | Array of google.maps.Marker |
MultiLineString | Array of google.maps.Polyline |
MultiPolygon | Array of google.maps.Polygon |
Feature | google.maps.[Marker,Polyline,Polygon] (depends on Feature geometry type) |
FeatureCollection | Array of google.maps.[Marker,Polyline,Polygon] (depends on Feature geometry type) |
GeometryCollection | Array of google.maps.[Marker,Polyline,Polygon] (depends on geometry type) |
Convert Google Maps overlays into the most simple GeoJSON object possible.
overlays - An array of Google Maps overlays. Only Marker, Polyline, and Polygon are supported.
// Convert Google Maps polygon to a GeoJSON Polygon
var geojson = google.maps.geojson.to(polygon);
Google Maps | GeoJSON |
---|---|
google.maps.Marker | Point |
google.maps.Polyline | LineString |
google.maps.Polygon | Polygon |
Array of google.maps.Marker | MultiPoint |
Array of google.maps.Polyline | MultiLineString |
Array of google.maps.Polygon | MultiPolygon |
Mixed array of supported types | GeometryCollection |
If an array containing only one Google Maps overlay is given then it will be distilled down to the most simple GeoJSON object possible. For example, if given an array with just one google.maps.Marker, a GeoJSON Point will be returned instead of a MultiPoint.
from
checks for properly formatted GeoJSON.
to
returns an error object for unsupported Google Maps Overlay types.
{
"type": "Error",
"message": "Invalid GeoJSON object: Feature object missing \"properties\" or \"geometry\" member."
}
It would be in your best interest to first check the returned object to see if it's error free.
var overlay = google.maps.geojson.from(myGeoJSON, options);
if (overlay.type === "Error"){
// Handle the error.
} else {
overlay.setMap(myMap);
}