地理数据多使用GeoJSON形式存储表达,百度地图JS API GL提供两种不同的方式支持开发者加载自己的GeoJSON数据: 1.提供GeoJSONParse类,满足用户将geojson数据解析为符合百度地图坐标的Overlay数据,用户可得到Overlay属性、坐标数据,进行覆盖物实例化或者其他处理。 2.提供GeoJSONLayer类,满足用户将geojson数据直接解析为覆盖物的图层,并进行交互操作。
GeoJSONParse类定义如下:
构造函数:
options参数:
方法:
BMapGL.Map添加以下方法:
addGeoJSONLayer(layer:GeoJSONLayer)
removeGeoJSONLayer(layer:GeoJSONLayer)
GeoJSONLayer类定义如下:
构造函数:
options参数:
方法:
事件
通过addEventListener方法注册事件,通过removeEventListener移除事件。
1、GeoJSONParse覆盖物模式
var label = new BMapGL.Label();label.setStyle({color: 'blue',borderRadius: '5px',borderColor: '#ccc',padding: '10px',fontSize: '16px',fontFamily: '微软雅黑',transform: 'translateX(-50%) translateY(calc(-100% - 10px))',});function popup(e) {if (e.target) {label.setPosition(e.latLng);label.setContent(`<h4 style='margin:0 0 5px 0;'>${e.target.properties.name}</h4><p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>adcode:${e.target.properties.adcode}</p></div>`);map.addOverlay(label);}}var region = {type: 'FeatureCollection',features: [{type: 'Feature',properties: {adcode: 110101,name: '东城区',join: 0,center: [116.418757, 39.917544],centroid: [116.416718, 39.912934],childrenNum: 0,level: 'district',parent: { adcode: 110000 },subFeatureIndex: 0,acroutes: [100000, 110000],},geometry: {type: 'MultiPolygon',coordinates: [[[[116.44364, 39.87284], [116.445648, 39.879283], [116.444059, 39.890038], [116.450939, 39.890249], [116.450876, 39.894088], [116.447154, 39.894186], [116.446819, 39.900042], [116.448722, 39.903246], [116.436488, 39.902042], [116.434983, 39.913964], [116.434314, 39.92868], [116.443682, 39.928664], [116.443703, 39.936663], [116.446338, 39.946205], [116.440566, 39.945295], [116.442239, 39.9497], [116.435422, 39.952121], [116.436698, 39.949245], [116.429483, 39.950155], [116.429002, 39.957274], [116.424861, 39.962279], [116.414196, 39.962182], [116.411415, 39.964928], [116.411101, 39.97146], [116.407504, 39.973995], [116.40788, 39.962182], [116.389498, 39.96314], [116.387658, 39.96093], [116.38678, 39.957014], [116.393346, 39.957355], [116.394266, 39.940629], [116.396169, 39.94006], [116.396692, 39.928306], [116.399474, 39.923574], [116.392175, 39.92242], [116.392259, 39.907881], [116.395563, 39.907995], [116.396086, 39.89944], [116.397612, 39.898675], [116.399097, 39.872205], [116.38059, 39.871148], [116.380632, 39.866054], [116.387888, 39.867372], [116.394956, 39.862734], [116.3955, 39.858682], [116.406856, 39.859967], [116.41246, 39.858942], [116.41589, 39.863645], [116.413652, 39.871148], [116.423209, 39.872824], [116.442574, 39.87188], [116.44364, 39.87284]]]]},},{type: 'Feature',properties: {adcode: 110102,name: '西城区',join: 4,center: [116.366794, 39.915309],centroid: [116.36567, 39.912028],childrenNum: 0,level: 'district',parent: { adcode: 110000 },subFeatureIndex: 1,acroutes: [100000, 110000],},geometry: {type: 'MultiPolygon',coordinates: [[[[116.325799, 39.896789], [116.32582, 39.891111], [116.320759, 39.881512], [116.321324, 39.875199], [116.326636, 39.876859], [116.335273, 39.875183], [116.341567, 39.876159], [116.344286, 39.873653], [116.349472, 39.873588], [116.35058, 39.86869], [116.38059, 39.871148], [116.399097, 39.872205], [116.397612, 39.898675], [116.396086, 39.89944], [116.395563, 39.907995], [116.392259, 39.907881], [116.392175, 39.92242], [116.399474, 39.923574], [116.396692, 39.928306], [116.396169, 39.94006], [116.394266, 39.940629], [116.393346, 39.957355], [116.38678, 39.957014], [116.387658, 39.96093], [116.390084, 39.968406], [116.394162, 39.969397], [116.394099, 39.972858], [116.380903, 39.972712], [116.380401, 39.968178], [116.370384, 39.967902], [116.371974, 39.948594], [116.356206, 39.944092], [116.352023, 39.950854], [116.352421, 39.943832], [116.341442, 39.941979], [116.332889, 39.944092], [116.327953, 39.942369], [116.333056, 39.938565], [116.334645, 39.922664], [116.335356, 39.898448], [116.337301, 39.89739], [116.325799, 39.896789]]]]},},],}; // GeoJSON数据var colorBand = ['darkolivegreen', 'cadetblue', 'orange', 'red', 'tan'];var gParse = new BMapGL.GeoJSONParse({ reference: 'GCJ02' });var fs = gParse.readFeaturesFromObject(region,{ fillColor: 'red' },function (overlay) {var index = overlay.properties.join || 0;overlay.setFillColor(colorBand[index]);overlay.setZIndex(-2);overlay.addEventListener('onclick', popup);map.addOverlay(overlay);},);
2、GeoJSONParse点序列模式
var region = {type: 'FeatureCollection',features: [{type: 'Feature',properties: {adcode: 110101,name: '东城区',join: 0,center: [116.418757, 39.917544],centroid: [116.416718, 39.912934],childrenNum: 0,level: 'district',parent: { adcode: 110000 },subFeatureIndex: 0,acroutes: [100000, 110000],},geometry: {type: 'MultiPolygon',coordinates: [[[[116.44364, 39.87284], [116.445648, 39.879283], [116.444059, 39.890038], [116.450939, 39.890249], [116.450876, 39.894088], [116.447154, 39.894186], [116.446819, 39.900042], [116.448722, 39.903246], [116.436488, 39.902042], [116.434983, 39.913964], [116.434314, 39.92868], [116.443682, 39.928664], [116.443703, 39.936663], [116.446338, 39.946205], [116.440566, 39.945295], [116.442239, 39.9497], [116.435422, 39.952121], [116.436698, 39.949245], [116.429483, 39.950155], [116.429002, 39.957274], [116.424861, 39.962279], [116.414196, 39.962182], [116.411415, 39.964928], [116.411101, 39.97146], [116.407504, 39.973995], [116.40788, 39.962182], [116.389498, 39.96314], [116.387658, 39.96093], [116.38678, 39.957014], [116.393346, 39.957355], [116.394266, 39.940629], [116.396169, 39.94006], [116.396692, 39.928306], [116.399474, 39.923574], [116.392175, 39.92242], [116.392259, 39.907881], [116.395563, 39.907995], [116.396086, 39.89944], [116.397612, 39.898675], [116.399097, 39.872205], [116.38059, 39.871148], [116.380632, 39.866054], [116.387888, 39.867372], [116.394956, 39.862734], [116.3955, 39.858682], [116.406856, 39.859967], [116.41246, 39.858942], [116.41589, 39.863645], [116.413652, 39.871148], [116.423209, 39.872824], [116.442574, 39.87188], [116.44364, 39.87284]]]]},},{type: 'Feature',properties: {adcode: 110102,name: '西城区',join: 4,center: [116.366794, 39.915309],centroid: [116.36567, 39.912028],childrenNum: 0,level: 'district',parent: { adcode: 110000 },subFeatureIndex: 1,acroutes: [100000, 110000],},geometry: {type: 'MultiPolygon',coordinates: [[[[116.325799, 39.896789], [116.32582, 39.891111], [116.320759, 39.881512], [116.321324, 39.875199], [116.326636, 39.876859], [116.335273, 39.875183], [116.341567, 39.876159], [116.344286, 39.873653], [116.349472, 39.873588], [116.35058, 39.86869], [116.38059, 39.871148], [116.399097, 39.872205], [116.397612, 39.898675], [116.396086, 39.89944], [116.395563, 39.907995], [116.392259, 39.907881], [116.392175, 39.92242], [116.399474, 39.923574], [116.396692, 39.928306], [116.396169, 39.94006], [116.394266, 39.940629], [116.393346, 39.957355], [116.38678, 39.957014], [116.387658, 39.96093], [116.390084, 39.968406], [116.394162, 39.969397], [116.394099, 39.972858], [116.380903, 39.972712], [116.380401, 39.968178], [116.370384, 39.967902], [116.371974, 39.948594], [116.356206, 39.944092], [116.352023, 39.950854], [116.352421, 39.943832], [116.341442, 39.941979], [116.332889, 39.944092], [116.327953, 39.942369], [116.333056, 39.938565], [116.334645, 39.922664], [116.335356, 39.898448], [116.337301, 39.89739], [116.325799, 39.896789]]]]},},],}; // GeoJSON数据var colorBand = ['darkolivegreen', 'cadetblue', 'orange', 'red', 'tan'];var gParse = new BMapGL.GeoJSONParse({ reference: 'GCJ02' });gParse.readFeaturesFromObject(region, { isPoints: true }, function (overlay) {var index = overlay.properties.join || 0;var prism = new BMapGL.Prism(overlay.points, 3000, {topFillColor: colorBand[index],topFillOpacity: 0.9,sideFillColor: colorBand[index],sideFillOpacity: 0.9,});prism.properties = overlay.properties;prism.setZIndex(-1);prism.addEventListener('onclick', popup);map.addOverlay(prism);});
3、GeoJSONLayer示例
// 样例数据var region = {type: 'FeatureCollection',features: [{type: 'Feature',properties: {adcode: 110101,name: '东城区',join: 0,center: [116.418757, 39.917544],centroid: [116.416718, 39.912934],childrenNum: 0,level: 'district',parent: { adcode: 110000 },subFeatureIndex: 0,acroutes: [100000, 110000],},geometry: {type: 'MultiPolygon',coordinates: [[[[116.44364, 39.87284], [116.445648, 39.879283], [116.444059, 39.890038], [116.450939, 39.890249], [116.450876, 39.894088], [116.447154, 39.894186], [116.446819, 39.900042], [116.448722, 39.903246], [116.436488, 39.902042], [116.434983, 39.913964], [116.434314, 39.92868], [116.443682, 39.928664], [116.443703, 39.936663], [116.446338, 39.946205], [116.440566, 39.945295], [116.442239, 39.9497], [116.435422, 39.952121], [116.436698, 39.949245], [116.429483, 39.950155], [116.429002, 39.957274], [116.424861, 39.962279], [116.414196, 39.962182], [116.411415, 39.964928], [116.411101, 39.97146], [116.407504, 39.973995], [116.40788, 39.962182], [116.389498, 39.96314], [116.387658, 39.96093], [116.38678, 39.957014], [116.393346, 39.957355], [116.394266, 39.940629], [116.396169, 39.94006], [116.396692, 39.928306], [116.399474, 39.923574], [116.392175, 39.92242], [116.392259, 39.907881], [116.395563, 39.907995], [116.396086, 39.89944], [116.397612, 39.898675], [116.399097, 39.872205], [116.38059, 39.871148], [116.380632, 39.866054], [116.387888, 39.867372], [116.394956, 39.862734], [116.3955, 39.858682], [116.406856, 39.859967], [116.41246, 39.858942], [116.41589, 39.863645], [116.413652, 39.871148], [116.423209, 39.872824], [116.442574, 39.87188], [116.44364, 39.87284]]]]},},{type: 'Feature',properties: {adcode: 110102,name: '西城区',join: 4,center: [116.366794, 39.915309],centroid: [116.36567, 39.912028],childrenNum: 0,level: 'district',parent: { adcode: 110000 },subFeatureIndex: 1,acroutes: [100000, 110000],},geometry: {type: 'MultiPolygon',coordinates: [[[[116.325799, 39.896789], [116.32582, 39.891111], [116.320759, 39.881512], [116.321324, 39.875199], [116.326636, 39.876859], [116.335273, 39.875183], [116.341567, 39.876159], [116.344286, 39.873653], [116.349472, 39.873588], [116.35058, 39.86869], [116.38059, 39.871148], [116.399097, 39.872205], [116.397612, 39.898675], [116.396086, 39.89944], [116.395563, 39.907995], [116.392259, 39.907881], [116.392175, 39.92242], [116.399474, 39.923574], [116.396692, 39.928306], [116.396169, 39.94006], [116.394266, 39.940629], [116.393346, 39.957355], [116.38678, 39.957014], [116.387658, 39.96093], [116.390084, 39.968406], [116.394162, 39.969397], [116.394099, 39.972858], [116.380903, 39.972712], [116.380401, 39.968178], [116.370384, 39.967902], [116.371974, 39.948594], [116.356206, 39.944092], [116.352023, 39.950854], [116.352421, 39.943832], [116.341442, 39.941979], [116.332889, 39.944092], [116.327953, 39.942369], [116.333056, 39.938565], [116.334645, 39.922664], [116.335356, 39.898448], [116.337301, 39.89739], [116.325799, 39.896789]]]]},},],}; // GeoJSON数据// 颜色表var colorBand = ['darkolivegreen', 'cadetblue', 'orange', 'red', 'tan'];var label = new BMapGL.Label();label.setStyle({color: 'blue',borderRadius: '5px',borderColor: '#ccc',padding: '10px',fontSize: '16px',fontFamily: '微软雅黑',transform: 'translateX(-50%) translateY(calc(-100% - 10px))',});function popup(e) {if (e.features && e.features.length > 0) {var overlay = e.features[0];if (overlay.layerName === 'bj-child') {// 重置数据的样式resetGeoLayer();if (overlay.toString() === 'Polygon') {overlay.setFillColor('yellow');}if (overlay.toString() === 'Polyline') {overlay.setStrokeColor('yellow');}label.setPosition(e.latLng);label.setContent(`<h4 style='margin:0 0 5px 0;'>${overlay.properties.name}</h4><p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>adcode:${overlay.properties.adcode}</p></div>`);map.addOverlay(label);}}}var bjRegionLayer = new BMapGL.GeoJSONLayer('bj-child', {reference: 'GCJ02',dataSource: region,level: -10,polygonStyle: function (properties) {var index = properties.join || 0;return {fillColor: colorBand[index],};},polylineStyle: function (properties) {return {strokeColor: 'blue',};},markerStyle: function (properties) {return {};},});bjRegionLayer.addEventListener('click', function (e) {if (e.features) {popup(e);} else {resetGeoLayer();map.removeOverlay(label);}});map.addGeoJSONLayer(bjRegionLayer);function resetGeoLayer() {bjRegionLayer && bjRegionLayer.resetStyle();}
4、添加addOverlay(Array<Overlay | GeoJSON> | Overlay | GeoJSON)、removeOverlay(Array<Overlay> | Overlay)方法
bjRegionLayer.addOverlay(marker1);bjRegionLayer.addOverlay([polyline,polygon]);bjRegionLayer.addOverlay(marker1.toGeoJSON());bjRegionLayer.addOverlay([polyline.toGeoJSON(),polygon.toGeoJSON()]);bjRegionLayer.removeOverlay(marker1);bjRegionLayer.removeOverlay([polyline,polygon]);
上一篇
下一篇