fix(layer) active feature bug

This commit is contained in:
thinkinggis 2019-03-06 16:39:18 +08:00
parent d943d54a41
commit 51c35700a3
5 changed files with 44 additions and 31 deletions

View File

@ -96,7 +96,7 @@ scene.on('loaded', () => {
}) })
.render(); .render();
console.log(citylayer);
citylayer.on('click',(e)=>{ citylayer.on('click',(e)=>{
$("#info").css({'left': e.pixel.x,'top':e.pixel.y, display:'block'}); $("#info").css({'left': e.pixel.x,'top':e.pixel.y, display:'block'});
$("#info").html(`<p>${e.feature.properties.area || e.feature.properties.name }<span">${e.feature.properties.pm2_5_24h || 0}</span></p>`); $("#info").html(`<p>${e.feature.properties.area || e.feature.properties.name }<span">${e.feature.properties.pm2_5_24h || 0}</span></p>`);

View File

@ -98,7 +98,7 @@ class Picking {
} }
const item = { const item = {
layerId, layerId,
featureId: id - 1, featureId: id,
point2d: _point2d, point2d: _point2d,
point3d: _point3d, point3d: _point3d,
intersects intersects

View File

@ -280,12 +280,12 @@ export default class Layer extends Base {
const { featureId } = e; const { featureId } = e;
if (featureId < 0) return; if (featureId < 0) return;
const activeStyle = this.get('activedOptions'); const activeStyle = this.get('activedOptions');
// const selectFeatureIds = this.layerSource.getSelectFeatureId(featureId); const selectFeatureIndexId = this.layerSource.getSeletFeatureIndex(featureId);
// 如果数据不显示状态则不进行高亮 // 如果数据不显示状态则不进行高亮
if (this.layerData[featureId].hasOwnProperty('filter') && this.layerData[featureId].filter === false) { return; } if (this.layerData[featureId].hasOwnProperty('filter') && this.layerData[featureId].filter === false) { return; }
const style = Util.assign({}, this.layerData[featureId]); const style = Util.assign({}, this.layerData[selectFeatureIndexId]); // 要素ID 和dataId不是对应关系
style.color = ColorUtil.toRGB(activeStyle.fill).map(e => e / 255); style.color = ColorUtil.toRGB(activeStyle.fill).map(e => e / 255);
this.updateStyle([ featureId ], style); this.updateStyle([ featureId ], style, selectFeatureIndexId);
} }
@ -351,6 +351,7 @@ export default class Layer extends Base {
const attr = attrs[k]; const attr = attrs[k];
attr.needUpdate = false; attr.needUpdate = false;
const names = attr.names; const names = attr.names;
const values = self._getAttrValues(attr, record); const values = self._getAttrValues(attr, record);
if (names.length > 1) { // position 之类的生成多个字段的属性 if (names.length > 1) { // position 之类的生成多个字段的属性
for (let j = 0; j < values.length; j++) { for (let j = 0; j < values.length; j++) {
@ -509,9 +510,9 @@ export default class Layer extends Base {
const pickingId = this.layerMesh.geometry.attributes.pickingId.array; const pickingId = this.layerMesh.geometry.attributes.pickingId.array;
const color = style.color; const color = style.color;
const colorAttr = this.layerMesh.geometry.attributes.a_color; const colorAttr = this.layerMesh.geometry.attributes.a_color;
const firstId = pickingId.indexOf(featureStyleId[0] + 1); const firstId = pickingId.indexOf(featureStyleId[0]);
for (let i = firstId; i < pickingId.length; i++) { for (let i = firstId; i < pickingId.length; i++) {
if (pickingId[i] === featureStyleId[0] + 1) { if (pickingId[i] === featureStyleId[0]) {
colorAttr.array[i * 4 + 0] = color[0]; colorAttr.array[i * 4 + 0] = color[0];
colorAttr.array[i * 4 + 1] = color[1]; colorAttr.array[i * 4 + 1] = color[1];
colorAttr.array[i * 4 + 2] = color[2]; colorAttr.array[i * 4 + 2] = color[2];
@ -583,14 +584,14 @@ export default class Layer extends Base {
* 重置高亮要素 * 重置高亮要素
*/ */
_resetStyle() { _resetStyle() {
const pickingId = this.layerMesh.geometry.attributes.pickingId.array; const pickingId = this.layerMesh.geometry.attributes.pickingId.array;
const colorAttr = this.layerMesh.geometry.attributes.a_color; const colorAttr = this.layerMesh.geometry.attributes.a_color;
this._activeIds.forEach(index => { this._activeIds.forEach(index => {
const color = this.layerData[index].color; const selectFeatureIndexId = this.layerSource.getSeletFeatureIndex(index);
const firstId = pickingId.indexOf(index + 1); const color = this.layerData[selectFeatureIndexId].color;
const firstId = pickingId.indexOf(index);
for (let i = firstId; i < pickingId.length; i++) { for (let i = firstId; i < pickingId.length; i++) {
if (pickingId[i] === index + 1) { if (pickingId[i] === index) {
colorAttr.array[i * 4 + 0] = color[0]; colorAttr.array[i * 4 + 0] = color[0];
colorAttr.array[i * 4 + 1] = color[1]; colorAttr.array[i * 4 + 1] = color[1];
colorAttr.array[i * 4 + 2] = color[2]; colorAttr.array[i * 4 + 2] = color[2];

View File

@ -1,4 +1,3 @@
import Base from './base'; import Base from './base';
import Controller from './controller/index'; import Controller from './controller/index';
import { getTransform, getParser } from '../source'; import { getTransform, getParser } from '../source';
@ -11,8 +10,7 @@ export default class Source extends Base {
defs: {}, defs: {},
parser: {}, parser: {},
transforms: [], transforms: [],
scales: { scales: {},
},
options: {} options: {}
}; };
} }
@ -84,7 +82,6 @@ export default class Source extends Base {
return geo; return geo;
} }
_coordProject(geo) { _coordProject(geo) {
if (Array.isArray(geo[0][0])) { if (Array.isArray(geo[0][0])) {
return geo.map(coor => { return geo.map(coor => {
return this._coordProject(coor); return this._coordProject(coor);
@ -92,22 +89,36 @@ export default class Source extends Base {
} }
if (!Array.isArray(geo[0])) { if (!Array.isArray(geo[0])) {
return this._coorConvert(geo); return this._coorConvert(geo);
} }
return geo.map(coor => { return geo.map(coor => {
return this._coorConvert(coor); return this._coorConvert(coor);
}); });
} }
_coorConvert(geo) { _coorConvert(geo) {
const ll = this.projectFlat(geo); const ll = this.projectFlat(geo);
return [ ll.x, ll.y, geo[2] || 0 ]; return [ ll.x, ll.y, geo[2] || 0 ];
} }
getSelectFeature(featureId) { getSelectFeature(featureId) {
const data = this.get('data'); const data = this.get('data');
// 如果是GeoJSON 数据返回原数 // 如果是GeoJSON 数据返回原数
return data.features ? data.features[featureId] : this.data.dataArray[featureId]; // 颜色编码从1开始要素索引从0开始所以颜色转变要素需要减1
return data.features
? data.features[featureId - 1]
: this.data.dataArray[featureId - 1];
}
getSeletFeatureIndex(featureId) {
const data = this.get('data');
if (!data.features) {
return featureId - 1;
}
let featureIndex = 0;
for (let i = 0; i < this.data.dataArray.length; i++) {
const item = this.data.dataArray[i];
if (item._id === featureId) {
break;
}
featureIndex++;
}
return featureIndex;
} }
} }

View File

@ -7,18 +7,19 @@ export function extent(data) {
const extent = [ Infinity, Infinity, -Infinity, -Infinity ]; const extent = [ Infinity, Infinity, -Infinity, -Infinity ];
data.forEach(item => { data.forEach(item => {
const { coordinates } = item; const { coordinates } = item;
if (Array.isArray(coordinates[0])) { calcExtent(extent, coordinates);
coordinates.forEach(coord => { });
if (extent[0] > coord[0]) extent[0] = coord[0]; return extent;
if (extent[1] > coord[1]) extent[1] = coord[1]; }
if (extent[2] < coord[0]) extent[2] = coord[0]; function calcExtent(extent, coords) {
if (extent[3] < coord[1]) extent[3] = coord[1]; coords.forEach(coord => {
}); if (Array.isArray(coord[0])) {
calcExtent(extent, coord);
} else { } else {
if (extent[0] > coordinates[0]) extent[0] = coordinates[0]; if (extent[0] > coord[0]) extent[0] = coord[0];
if (extent[1] > coordinates[1]) extent[1] = coordinates[1]; if (extent[1] > coord[1]) extent[1] = coord[1];
if (extent[2] < coordinates[0]) extent[2] = coordinates[0]; if (extent[2] < coord[0]) extent[2] = coord[0];
if (extent[3] < coordinates[1]) extent[3] = coordinates[1]; if (extent[3] < coord[1]) extent[3] = coord[1];
} }
}); });
return extent; return extent;