Skip to content

GeoJSONParse

此类满足用户将geojson数据解析为符合百度地图坐标的Overlay数据,用户可得到Overlay属性、坐标数据,进行覆盖物实例化或者其他处理。

构造函数

构造函数描述
BMapGL.GeoJSONParse(options: GeoJSONParseOptions)构建GeoJSON解析类。输入坐标支持options的reference设置类型,输出统一为百度09经纬度

GeoJSONParseOptions

属性类型描述
referencestring来源数据的坐标系,可选择:BD09LL|BD09MC|EPSG3857|GCJ02|WGS84,默认是:BD09LL

实例方法

方法参数返回值描述
readFeatureFromgeojson_feature: Feature, options: ObjectObject解析单个GeoJSON Feature对象。如果isPoints为false,返回实例化的覆盖物;如果是muti几何要素,会返回多个覆盖物Object,否则返回单个覆盖物Object。如果isPoints为true,返回Object的points为坐标序列点,属性存储在Object.properties中
readFeaturesFromgeojson: FeatureCollection, options: Object, callback: FunctionObject解析GeoJSON FeatureCollection对象。如果isPoints为false,返回实例化的覆盖物Overlay集合。如果isPoints为true,返回Object的集合,Object.points为坐标序列点,Object.properties为属性

示例

ts
// 创建GeoJSON解析器
const parser = new BMapGL.GeoJSONParse({
  reference: 'WGS84'
});

// 解析单个Feature
const feature = {
  type: 'Feature',
  geometry: {
    type: 'Point',
    coordinates: [116.404, 39.915]
  },
  properties: {
    name: '测试点'
  }
};

const result = parser.readFeatureFrom(feature);

// 解析FeatureCollection
const featureCollection = {
  type: 'FeatureCollection',
  features: [feature]
};

parser.readFeaturesFrom(featureCollection, {}, (overlay) => {
  map.addOverlay(overlay);
});

注意事项

  1. 坐标系转换:

    • 输入坐标支持多种坐标系(BD09LL、BD09MC、EPSG3857、GCJ02、WGS84)
    • 输出统一为百度09经纬度
    • 注意坐标系转换的准确性
  2. 样式设置:

    • 函数形式可以动态设置样式
    • 对象形式适合静态样式
    • 样式优先级:类型样式 > 统一样式 > 默认样式
  3. 性能优化:

    • 大量数据时避免频繁创建样式对象
    • 合理使用样式缓存
    • 注意内存占用

基于 MIT 许可发布