Partager via


GeoJSON Module

Note

Bing Maps Web Control SDK retirement

Bing Maps Web Control SDK is deprecated and will be retired. Free (Basic) account customers can continue to use Bing Maps Web Control SDK until June 30th, 2025. Enterprise account customers can continue to use Bing Maps Web Control SDK until June 30th, 2028. To avoid service disruptions, all implementations using Bing Maps Web Control SDK will need to be updated to use Azure Maps Web SDK by the retirement date that applies to your Bing Maps for Enterprise account type. For detailed migration guidance, see Migrate from Bing Maps Web Control SDK and Migrate Bing Maps Enterprise applications to Azure Maps with GitHub Copilot.

Azure Maps is Microsoft's next-generation maps and geospatial services for developers. Azure Maps has many of the same features as Bing Maps for Enterprise, and more. To get started with Azure Maps, create a free Azure subscription and an Azure Maps account. For more information about azure Maps, see Azure Maps Documentation. For migration guidance, see Bing Maps Migration Overview.

Module Name: Microsoft.Maps.GeoJson

Namespace: Microsoft.Maps.GeoJSON

GeoJSON is a common file format used for storing spatial data as a JSON object. This file format tends to be more compact then its XML equivalents. This results in a much smaller file size, making it ideal for transferring spatial data to web and mobile applications. When storing in a file the .js or .json file extensions are usually used, however occasionally you may come across some files that use .geojson. The following is an example of a GeoJSON file containing the location of New York.

{
    "type": "FeatureCollection",
    "features": [
      {
          "type": "Feature",
          "geometry": {
              "type": "Point",
              "coordinates": [-74.006393, 40.714172]
          },
          "properties": {
              "name": "New York",
              "description": "New York"
          }
      }
    ]
}

Additional information on this file format can be found here.

This module allows you to easily read and write GeoJSON data. When reading GeoJSON data, it is parsed into Bing Maps shapes. Specifically, MultiPoint, MultiLineString, MultiPolygon, GeometryCollection and FeatureCollection GeoJSON types are parsed into an array of Bing Maps shapes. To write GeoJSON, simply pass in a Bing Maps shape or array of shapes and the GeoJSON equivalent will be returned.

Supported GeoJSON tags:

  • Point
  • Linestring
  • Polygon
  • MultiPoint
  • MultiLinestring
  • MultiPolygon
  • Geometrycollection
  • Feature
  • FeatureCollection

The GeoJSON module only has one static class called GeoJson that allows you to read and write GeoJSON data.

In addition to being able to set the default style for shapes when reading a GeoJSON object, any properties on a GeoJSON shape that aligns with one of the names of a Pushpin, Polyline, or Polygon option will automatically be used to in styling that shape. This allows you to define custom styles for individual shapes, directly in your GeoJSON. Here is a list of the supported options:

  • draggable: boolean
  • icon: string
  • visible: boolean
  • title: string
  • subTitle: string
  • enableHoverStyle: boolean
  • enableClickedStyle: boolean
  • text: string
  • color: string
  • cursor: string
  • fillColor: string
  • strokeColor: string
  • strokeThickness: number
  • strokeDashArray: string or number[]
  • generalizable: boolean

Static Methods

The GeoJson class has the following static methods.

Name Return Type Description
read(geoJson: IGeoJsonObject or string, styles?: StylesOptions) IPrimitive or IPrimitive[] Takes in a GeoJSON object or string of GeoJSON data and parses it into Bing Maps shapes (Pushpin, Polyline, Polygon). Default styles can be specified as an option. An array of shapes will be returned if the GeoJSON object is a multipart geometry, such as MultiPoint, MultiLineString, MultiLineString, GeometryCollection or FeatureCollection.
readFromUrl(url: string, callback: function(data: IPrimitive or IPrimitive[]), jsonpQueryParam?: string, styles?: StylesOptions) Takes a URL to GeoJSON file and downloads and parses it into Bing Maps shapes and then returns them through a callback function. If the GeoJSON file is hosted on a different domain, and CORS is not enabled, but does support JSONP, you will need to specify the name of JSONP URL parameter that can be used to download the file across different domains. Default styles can be specified as an option.
write(data: IPrimitive or IPrimitive[]) IGeoJsonObject Takes a Bing Maps shape or array of shapes and converts them into a GeoJSON object.

Examples