Skip to content

openplacedatabase/geojson-google-maps

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

67 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

GeoJSON for Google Maps

version 2.0

Overview

GeoJSON for Google Maps enables you to create Google Maps API v3 overlays from GeoJSON objects and create GeoJSON objects from Google Maps overlays.

google.maps.geojson.from(geojson, [options])

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);

Conversions

GeoJSONGoogle Maps
Pointgoogle.maps.Marker
LineStringgoogle.maps.Polyline
Polygongoogle.maps.Polygon
MultiPointArray of google.maps.Marker
MultiLineStringArray of google.maps.Polyline
MultiPolygonArray of google.maps.Polygon
Featuregoogle.maps.[Marker,Polyline,Polygon] (depends on Feature geometry type)
FeatureCollectionArray of google.maps.[Marker,Polyline,Polygon] (depends on Feature geometry type)
GeometryCollectionArray of google.maps.[Marker,Polyline,Polygon] (depends on geometry type)

google.maps.geojson.to(overlays)

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);

Conversions

Google MapsGeoJSON
google.maps.MarkerPoint
google.maps.PolylineLineString
google.maps.PolygonPolygon
Array of google.maps.MarkerMultiPoint
Array of google.maps.PolylineMultiLineString
Array of google.maps.PolygonMultiPolygon
Mixed array of supported typesGeometryCollection

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.

Error Handling

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);
}

Contributors

About

Utility for using GeoJSON with Google Maps

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Languages

  • JavaScript 100.0%